一聚教程网:一个值得你收藏的教程网站

热门教程

Laravel 5.2 邮件功能使用详解

时间:2022-06-25 00:37:57 编辑:袖梨 来源:一聚教程网


配置

邮件的配置文件在config/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",
    |            "ses", "sparkpost", "log"
    |
    */

    // 默认使用 smtp 驱动, 支持 "smtp", "mail", "sendmail", "mailgun", "mandrill", "ses", "sparkpost", "log"
    'driver' => env('MAIL_DRIVER', '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.
    |
    */

    // smtp 服务器的主机地址
    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

    /*
    |--------------------------------------------------------------------------
    | 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' => "[email protected]", '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' => env('MAIL_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('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'),

    /*
    |--------------------------------------------------------------------------
    | 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文件中配置,这里使用的是163邮箱的SMTP服务,配置如下:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=对应账号的密码
MAIL_ENCRYPTION=ssl    
三、发送邮件

使用时记得 use Mail;

1. 发送纯文本邮件

$num = Mail::raw('邮件内容', function($message) {

    $message->from('[email protected]', '发件人名称');
    $message->subject('邮件主题');
    $message->to('[email protected]');
});    
2. 发送HTML视图

使用mails目录下的welcome模板,传递的参数用于给模板中的变量赋值:

$num = Mail::send('mails.welcome', ['name' => 'sean'], function($message) {

    $message->to('[email protected]');
});
对应的模板为:

Welcome, {{ $name }} !

热门栏目