Laravel 5.2 教程 - 邮件

一、简介

Laravel 的邮件功能基于热门的 SwiftMailer 函数库之上,提供了一个简洁的 API。Laravel为SMTP、Mailgun、Mandrill、Amazon SES、PHP的mail函数、以及sendmail提供了驱动,从而允许你快速通过本地或云服务发送邮件。

本文通过介绍国内常用的SMTP方式来介绍 Laravel 中邮件功能的使用。

二、配置

邮件的配置文件在config/mail.php文件中,配置项及说明如下:

<?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' => "json_vip@163.com", '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
MAIL_USERNAME=json_vip@163.com
MAIL_PASSWORD=对应账号的密码
MAIL_ENCRYPTION=ssl      

三、发送邮件

使用时记得 use Mail;

1. 发送纯文本邮件

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

    $message->from('json_vip@163.com', '发件人名称');
    $message->subject('邮件主题');
    $message->to('849291170@qq.com');
});        

2. 发送HTML视图

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

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

    $message->to('849291170@qq.com');
});

对应的模板为:

<h1> Welcome, {{ $name }} ! </h>

交友互动:

本文首发于马燕龙个人博客,欢迎分享,转载请标明出处。
马燕龙个人博客:https://www.mayanlong.com
马燕龙个人微博:http://weibo.com/imayanlong
马燕龙Github主页:https://github.com/yanlongma

标签: laravel

不错,不错,对我有帮助! 我要打赏他!GO ->

仅有一条评论

  1. Laravel学习手册

添加新评论