一、简介

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>

交友互动:

本文首发于马燕龙个人博客,欢迎分享,转载请标明出处。

马燕龙个人博客:http://www.mayanlong.com

马燕龙个人微博:http://weibo.com/imayanlong

马燕龙Github主页:https://github.com/yanlongma

Laravel 5.2 教程 - 邮件的更多相关文章

  1. Laravel 5.2 教程 - 队列

    一.简介 Laravel 队列组件提供一个统一的 API 集成了许多不同的队列服务,队列允许你延后执行一个耗时的任务,例如延后至指定的时间才发送邮件,进而大幅的加快了应用程序处理请求的速度. 由于本例 ...

  2. Laravel自带SMTP邮件组件实现发送邮件(QQ、163、企业邮箱都可)

    Laravel自带SMTP邮件组件实现发送邮件(QQ.163.企业邮箱都可)     laravel自带SMTP邮件配置和遇到的坑 laravel自带SwiftMailer库,集成了多种邮件API,可 ...

  3. 2016 版 Laravel 系列入门教程

    2016 版 Laravel 系列入门教程 (1) - (5) http://www.golaravel.com/post/2016-ban-laravel-xi-lie-ru-men-jiao-ch ...

  4. 2016 版 Laravel 系列入门教程(五)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本文是本系列教程的完结篇,我们将一起给 Arti ...

  5. 2016 版 Laravel 系列入门教程(四)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本篇文章中,我将跟大家一起实现 Article ...

  6. 2016 版 Laravel 系列入门教程(二)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本篇文章中,我将跟宝宝们一起学习 Laravel ...

  7. 2016 版 Laravel 系列入门教程(三)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 在本篇文章中,我们将尝试构建一个带后台的简单博客 ...

  8. 2016 版 Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本文基于 Laravel 5.2 版本,无奈 5 ...

  9. [转]Laravel 数据库实例教程 —— 使用查询构建器实现对数据库的高级查询

    本文转自:https://laravelacademy.org/post/920.html 上一节我们简单介绍了如何使用查询构建器对数据库进行基本的增删改查操作,这一节我们来探讨如何使用查询构建器实现 ...

随机推荐

  1. jQuery(20161108)

    jQuery的引入包必须要在其他引入包的最上方,否则会出错.因为如果在页面加载完成后还未执行jQuery包,那么jQuery的引入包就没用了 如果浏览器报错:Uncaught Error: Boots ...

  2. 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  3. linux上执行 xhost unable to open display

    linux下执行xhost命令报错:unable to open display,解决方法,linux 下通过xhost进入图形界面,经常会出现报错"unable to  open disp ...

  4. [项目记录] 用c语言完成的一个学生成绩管理系统

    一.要求: 学生成绩管理系统 某班有最多不超过30人(具体人数由键盘输入)参加期末考试,最多不超过6门(具体门数由键盘输入).使用链表编程实现如下菜单驱动的学生成绩管理系统. 从文件读入每个学生个人信 ...

  5. C#_扩展方法

    这里我先引用一个实例,需求是这样:我们要将一个字符串中的字幕大小写变换,即大写变小写,小写变大写. 通常,我们首先会考虑在当前类中写一个方法,将字符串传进去,然后返回变换后的字符串.这样写当然不会错, ...

  6. Java Applet实现五子棋游戏

    从谷歌的AlphaGo到腾讯的绝艺,从人脸识别到无人驾驶,从谷歌眼镜到VR的兴起,人工智能领域在不断的向前迈进,也在不断深入的探索.但背后错综复杂的技术和利益成本也是很多企业亟待解决的难题.对于人工智 ...

  7. PMP和PRINCE2应该选择哪个?光环国际项目管理认证

    对于项目管理课程的选择,我们不能盲目地做选择,一定要从自身实际出发.从来都没有更好的课程,只有更合适自己的课程. 那么,如何选择合适自己的项目管理课程呢? 让我们从PMP与PRINCE2之间的差异开始 ...

  8. 【C++】浅谈三大特性之一继承(三)

    四,派生类的六个默认成员函数 在继承关系里,如果我们没有显示的定义这六个成员函数,则编译系统会在适合场合为我们自动合成. 继承关系中构造函数和析构函数的调用顺序: class B { public: ...

  9. 【2017-03-31】JS-DOM操作:操作属性、彩虹导航栏、定时器、操作内容、创建元素并添加、操作相关元素

    一.操作属性 1.什么是属性: <div class="div" id="div1" style="" ></div> ...

  10. Intellj IDEA 简易教程

    Intellj IDEA 简易教程 目录 JDK 安装测试 IDEA 安装测试 调试 单元测试 重构 Git Android 其他 参考资料 Java开发IDE(Integrated Developm ...