Laravel邮件服务

时间:2016-05-12 04:36:31

标签: laravel email

对不起,我是新手,我正在使用laravel 5.1项目,我想使用电子邮件服务发送电子邮件。

之前我使用过mandrill,但它现在已经通过mailchimp连接了付费邮件服务,

所以我搜索另一个解决方案是mailgun,当我创建mailgun帐户时,它需要被激活,我不能用它发送电子邮件。

我在我的本地项目中设置了gmail smtp并且它的成功发送了电子邮件,但当我将设置推送到我的服务器时显示此错误:

Connection could not be established with host smtp.gmail.com [ #0]

我混淆了我必须使用哪种易于设置且易于使用的电子邮件服务?

1 个答案:

答案 0 :(得分:0)

出于安全原因,文件mail.php有env变量形式的键值,你必须在一个名为.env的单独文件中设置(我在下面包括一个样本),当然,你可以使用您拥有帐户的任何免费邮件提供商,如gmail,hotmail,yandex等。

首先这是config

中的mail.php文件
'driver' => env('MAIL_DRIVER', $_ENV['MAIL_DRIVER']),

/*
|--------------------------------------------------------------------------
| 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', $_ENV['MAIL_HOST']),

/*
|--------------------------------------------------------------------------
| 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',$_ENV['MAIL_PORT']),

/*
|--------------------------------------------------------------------------
| 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' => 'beauty@beauty.com', 'name' => 'My App'],

/*
|--------------------------------------------------------------------------
| 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' => env('MAIL_ENCRYPTION', 'ssl'),

/*
|--------------------------------------------------------------------------
| 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('MAIL_USERNAME', $_ENV['MAIL_USERNAME']),

/*
|--------------------------------------------------------------------------
| 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('MAIL_PASSWORD', $_ENV['MAIL_PASSWORD']),

/*
|--------------------------------------------------------------------------
| 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',

];

这是.env文件

MAIL_DRIVER=smtp
MAIL_HOST='smtp.gmail.com'
MAIL_PORT='465'
MAIL_USERNAME='beauty@beauty.com'
MAIL_PASSWORD='yourpassword'
MAIL_ENCRYPTION=null

然后在您的控制器中,您需要编写邮件代码,就像这样。我已经包含了一张图片,正如您所看到的,名称取自从表单发送的变量,但您可以通过硬编码字符串替换它们以进行测试。

enter image description here

您需要的最后一件事是实际发送到收件人的电子邮件的视图,在这种情况下我们已经调用了,#34;邮件程序"并且看起来像这样:

enter image description here