laravel5.2 使用smtp 发送邮件
一、 邮箱配置(打开163邮箱的官网,登录你的邮箱)
打开SMTP服务之后,还需要开启授权密码并设置。
打开服务和配置授权密码
1.首选在laravel的config目录下找到mail.php文件,配置如下:
<?php return [ 'driver' => env('MAIL_DRIVER', 'smtp'), 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 'port' => env('MAIL_PORT', 587), 'from' => ['address' => 'xxxxxx@163.com','name' => '激活邮件'], //全局的 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'), 'sendmail' => '/usr/sbin/sendmail -bs', ];
2.在env文件配置一下代码
MAIL_DRIVER=smtp MAIL_HOST=smtp.163.com MAIL_PORT=465 MAIL_USERNAME=xxxxxx@163.com MAIL_PASSWORD= //此处密码为163的授权码 MAIL_ENCRYPTION=ssl
三、 编码
新建一个或在需要调用发送邮件的controller,里面需要发邮件的函数增加:
use Illuminate\Support\Facades\Mail; public function email(Request $request) { $data = $request->all(); $too = $data['certifycode']; //此处为您要发送的邮箱账号 Mail::send('home.email',['name' => 'xxxx,'account'=>$too,'uid'=>$this->user['id']],function($message)use($too){ $message->to($too)->subject('Tcsurvey邮件激活');; }); $data = array('code'=>3,'msg'=>'邮件已发送至邮箱,请注意查收'); return $data; }
四、 邮箱模板
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> </head> <body> 尊敬的 {{ $name }} 用户, <br> <a href="{{url('/doemail?email='.$account.'&uid='.$uid)}}"> 请点击此处激活{{ $account }}账号邮件 </a> </body> </html>