【问题标题】:Laravel Mail ServiceLaravel 邮件服务
【发布时间】:2016-09-07 17:23:15
【问题描述】:

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

我以前使用过 mandrill,但现在与 mailchimp 连接是付费邮件服务,

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

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

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

我不知道我必须使用哪种电子邮件服务才能如此易于设置和使用?

【问题讨论】:

标签: laravel email


【解决方案1】:

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

首先这是配置中的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

然后在您的控制器中,您需要编写邮件代码,如下所示。如您所见,我附上了一张图片,其中名称取自从表单发送的变量,但您可以用硬编码字符串替换它们以进行测试。

您最不需要的是实际发送到收件人电子邮件的视图,在这种情况下,我们称之为“邮件程序”,看起来像这样:

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 2019-02-22
    • 2019-09-08
    • 1970-01-01
    • 2017-05-31
    • 2021-09-18
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多