php - 试图让电子邮件在 Laravel 5 中工作

标签 php email laravel laravel-5

好吧,首先让我说我是 laravel 5 的新手。我一直在谷歌上搜索,试图通过输入适当的 URL 来发送一封简单的电子邮件,但没有成功。不幸的是,我发现那里的文档并没有那么有用,只是给出了一个宽泛的外观(我知道 laravel 5 是新的,但仍然令人沮丧哈哈)。我想做的事情没有什么特别的,我只是想在做其他事情之前把它也工作好。我现在正尝试使用 gmail 来让它工作,但一旦我把它记下来,我当然会尝试 Mailgun 之类的东西。这是我现在拥有的代码 第一个在 mail.php 中:

return [

/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
|
*/

'driver' => env('smtp'),



/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/

'host' => env('MAIL_HOST', 'smtp.gmail.com'),



/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/

'port' => env('MAIL_PORT', 587),

/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/

'from' => ['address' =>"exmaple@gmail.com" , 'name' => "example_name"],

/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/

'encryption' => 'tls',


/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/

'username' => env('example@gmail.com'),

/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/

'password' => env('example'),

/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/

'sendmail' => '/usr/sbin/sendmail -bs',


/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/

'pretend' => false,

];

这是在我的 route :

Route::get('test', function()
{
Mail::send('Email.test', function ($message)
{
    $message->to('example@gmail.com', 'example_name')->subject('Welcome!');
});
});

我还尝试了 MailController@Sending_Email 作为路径。 这是在我的 MailController 中:

class MailController extends Controller{
public function Sending_Email()
{
   $this->call('GET','Email.test');
    return View('Email.test');
}

}

我的观点是这个简单的代码:

<html>
<head>

</head>
<body>
    <h1>hey this is a test to see if my email system works</h1>
</body>
</html>

这是我的错误: Illuminate\Support\Manager::createDriver() 缺少参数 1,在/vagrant/leonis/vendor/laravel/framework/src/Illuminate/Support/Manager.php 第 89 行调用并定义

最佳答案

您遇到了几个问题。首先:

'driver' => env('smtp'),

env 方法查看您的 .env 文件。我猜你的 .env 文件中没有“smtp”条目。我只想将其更改为:

'driver' => 'smtp',

这应该可以解决 createDriver() 错误。

如果您仍然对驱动程序有疑问,或者稍后对您的 SMTP 服务器进行身份验证时遇到问题,请在运行时快速检查您的配置:

dd(Config::get("mail"));

由于您有 env() 检查 .env 设置,然后回退到默认值,因此查看生成的配置是什么样子会很有帮助。

现在您仍然对如何调用 Mail::send 有疑问。这是您的代码:

Mail::send('Email.test', function ($message)

这是来自 Laravel documentation :

Mail::send('emails.welcome', ['key' => 'value'], function($message)

注意第二个参数是一个数组。回调函数应该是第三个​​参数。

来自文档:

The second is the data to be passed to the view, often as an associative array where the data items are available to the view by $key.

所以做这样的事情:

Mail::send('Email.test', [], function ($message) { 
    $message->to('example@gmail.com', 'example_name')->subject('Welcome!');
});

关于php - 试图让电子邮件在 Laravel 5 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29083520/

相关文章:

php - MySQL 插入双引号 PHP

c# - 在设定的时间自动发送电子邮件

laravel - 如何在 Laravel MIX 上创建 svg Sprite ?

php mail() 语句

php - 在 PHP/MySQL 中计算页面浏览量的最佳方法是什么?

php - ZeroMQ 的安装在 php 5.6 Ubuntu 14.04 上不起作用

amazon-web-services - Amazon SES 535 身份验证凭据尝试轮换访问 key 无效

使用 CSS 的 HTML 电子邮件

mysql - 计算数据透视表内的标准差

php - Laravel 条件语句 when 用于过滤值