博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Laravel 5.2 教程 - 邮件
阅读量:6690 次
发布时间:2019-06-25

本文共 4307 字,大约阅读时间需要 14 分钟。

hot3.png

一、简介

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

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

二、配置

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

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=smtpMAIL_HOST=smtp.163.comMAIL_PORT=465MAIL_USERNAME=json_vip@163.comMAIL_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');});

对应的模板为:

Welcome, {
{ $name }} !


交友互动:

本文首发于,欢迎分享,转载请标明出处。

马燕龙个人博客:
马燕龙个人微博:
马燕龙Github主页:

转载于:https://my.oschina.net/imayanlong/blog/782171

你可能感兴趣的文章
第十周作业
查看>>
win10常用快捷键
查看>>
vmware搭建vSAN提示磁盘不合格或者看不到磁盘的解决办法
查看>>
ubuntu 无法解析主机的解决方法
查看>>
Codeforces Round #321 (Div. 2)
查看>>
Spring MVC标签<mvc: annotation-driven />小结 原
查看>>
HashMap和Hashtable的区别
查看>>
Oracle EBS-SQL (INV-5):检查期间拉式物料领用记录数.sql
查看>>
Python之with语句原理
查看>>
在Window环境下多线程与CPU资源分配原则
查看>>
20170303新的开始
查看>>
Python--day25--复习(单继承和多继承的总结)
查看>>
Python--day39--进程池原理及效率测试
查看>>
@Html.EditFor()不能添加“只读”html属性;以及disable属性的坑
查看>>
Logger日志级别说明及设置方法、说明
查看>>
7-1 列出连通集 (25 分)
查看>>
Mybatis之Mapper动态代理
查看>>
【转】楼天城楼教主的acm心路历程(作为励志用)
查看>>
vw、vh、vmin、vmax 的含义
查看>>
04.设计模式_抽象工厂模式
查看>>