完整laravel5.3案例,包含导出excel,发送附件邮件
1、配置参数
修改.env配置文件
MAIL_DRIVER=smtp
MAIL_HOST=smtp.exmail.qq.com
MAIL_PORT=25
MAIL_USERNAME=xx@xx.cn
MAIL_PASSWORD=密码
MAIL_ENCRYPTION=是否加密
修改config/mail.php文件
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '发送者邮箱'),
'name' => env('MAIL_FROM_NAME', '发送者名称'),
],
注:mail_host和mail_port具体参数根据mail_username和mail_encryption参数来定
QQ邮箱mail_host=smtp.qq.com,加密mail_port=465 默认
QQ邮件发送需要开启QQ邮箱smtp和imap功能开启方法自行百度
2、发送文本邮件
创建一个视图文件:resources\views\mail.blade.php
然后在控制器合适的位置书写如下代码,即可
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class CartController extends Controller
{
public function send_mail(){
$name = '数据';
$flag = Mail::send('mail',['name'=>$name],function($message){
$to = 'zhoushitong@kemis.cn';
$message ->to($to)->subject('测试邮件');
});
}
}
3、发送邮件附件
创建一个视图文件:resources\views\mail.blade.php
然后在控制器合适的位置书写如下代码,即可
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class CartController extends Controller
{
public function send_mail(){
$attachments=['file'=>storage_path('excel/exports').'/cart.xlsx'];//文件路径
$subject='标题';
$cc = '抄送';
Mail::send('mail', ['content' => '附件'], function ($message) use ($to_email, $attachments, $subject,$cc) {
$message->to($to_email)->subject($subject);
foreach ($attachments as $alias => $attachment) {
$ext = pathinfo($attachment)['extension'];
$message->attach($attachment, ['as' => "=?UTF-8?B?" . base64_encode($alias) . "?=." . $ext]);
}
});
}
}