使用的版本:PHP7.2+laravel5.8 第一种:使用phpmailer 通过composer安装phpmailer: composer require phpmailer/phpmailer 在控制器中定一个函数用于发送邮件: 控制器引入: use PHPMailer\PHPMailer\PHPMailer; public function sendmail(){ // 实例化PHPMailer核心类 $mail = new PHPMailer(); // 使用smtp鉴权方式发送邮件 $mail->isSMTP(); // smtp需要鉴权 这个必须是true $mail->SMTPAuth = true; // 链接qq域名邮箱的服务器地址 $mail->Host = 'smtp.exmail.qq.com'; // 设置使用ssl加密方式登录鉴权 $mail->SMTPSecure = 'ssl'; // 设置ssl连接smtp服务器的远程服务器端口号 $mail->Port = 465; // 设置发送的邮件的编码 $mail->CharSet = 'UTF-8'; // 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名 $mail->FromName = '北京基云惠康有限公司'; // smtp登录的账号 QQ邮箱即可 $mail->Username = '邮箱名称'; // smtp登录的密码 使用生成的授权码 $mail->Password = '登录密码'; // 设置发件人邮箱地址 同登录账号 $mail->From = '设置发送邮件邮箱'; // 邮件正文是否为html编码 注意此处是一个方法 $mail->isHTML(true); // 设置收件人邮箱地址 $mail->addAddress(接受邮件邮箱); // 添加多个收件人 则多次调用方法即可 // 添加该邮件的主题 $mail->Subject = "测试案例"; // 添加邮件正文 $mail->Body = " <html> <head> <meta http-equiv=\"Content-Language\" content=\"zh-cn\"> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\"> </head> <body> 测试案例 <p>This message contains information which may be confidential and privileged. Unless you are the addressee (or authorized to receive for the addressee), you may not use, copy or disclose to anyone the message or any information contained in the message.</p> </body> </html>"; // 发送邮件 返回状态 $status = $mail->send(); return $status; } 第二种方式采用laraveL自带---可配置模板 首先配置 .env文件 MAIL_DRIVER=smtp MAIL_HOST=smtp.163.com MAIL_PORT=465 MAIL_USERNAME=用户名 MAIL_PASSWORD=密码 MAIL_ENCRYPTION=邮箱名称 MAIL_FROM_ADDRESS=发送邮箱----需要新增(/config/mail.php从环境变量中获取--不配置获取不到) MAIL_FROM_NAME=名称 ---需要新增 (/config/mail.php从环境变量中获取--不配置获取不到) MAIL_ENCRYPTION=ssl 引入:use Illuminate\Support\Facades\Mail; 发送邮件: public function sendmail(){ $name = '发送邮件'; #index控制器针对的模板文件夹 index:模板 Mail::send('index.index',['name'=>$name],function($message){ $to = '接受邮件@qq.com'; $message->to($to)->subject('案例测试'); }); if(count(Mail::failures()) < 1){ echo '发送邮件成功,请查收!'; }else{ echo '发送邮件失败,请重试!'; } } 页面: <html> <head> <meta http-equiv=\"Content-Language\" content=\"zh-cn\"> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\"> </head> <body> {{$name}} </body> </html> 第三种: 发送纯文本: Mail::raw('发送内容', function ($message) { $to = '接收邮箱@qq.com'; $message ->to($to)->subject('纯文本信息邮件测试'); }); if(count(Mail::failures()) < 1){ echo '发送邮件成功,请查收!'; }else{ echo '发送邮件失败,请重试!'; }
laravel发送邮件案例
最新推荐文章于 2022-04-06 21:41:23 发布