TP5.1 发送邮件
插件合集
安装
composer require phpmailer/phpmailer
配置
QQ邮箱 --> 设置 --> 账户 --> 往下拉 --> 打开它
直接上代码
<?php /** * Created by PhpStorm. * User: Zhangyongfeng * Date: 2020/12/2 * Time: 13:15 * * ━━━━━━━━━神兽出没━━━━━━━━━ * * ┏┓ ┏┓+ + * ┏┛┻━━━┛┻┓ + + * ┃ ┃ * ┃ ━ ┃ ++ + + + * ████━████ ┃+ * ┃ ┃ + * ┃ ┻ ┃ * ┃ ┃ + + * ┗━┓ ┏━┛ * ┃ ┃ * ┃ ┃ + + + + * ┃ ┃ Code is far away from bug with the animal protecting * ┃ ┃ + 神兽保佑,代码无bug * ┃ ┃ * ┃ ┃ + * ┃ ┗━━━┓ + + * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ + + + + * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛+ + + + * * ━━━━━━━━━感觉萌萌哒━━━━━━━━━ */ namespace app\base\controller; use PHPMailer\PHPMailer\PHPMailer; class Mailer extends Base { /* * 发送邮件 * @param $toemail //收件人地址 * @param $toname //收件人名称 * @param string $subject //主题 * @param string $body //内容 * @param null $attachment //附件 * @return bool **/ function send_mail($toemail, $toname, $subject = '', $body = '', $attachment = null) { $username = $this->config['cfg_mailUserName']; $password = $this->config['cfg_mailPassword']; $mail = new PHPMailer(); //实例化PHPMailer对象 $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $mail->IsSMTP(); // 设定使用SMTP服务 $mail->SMTPDebug = 0; // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息 $mail->SMTPAuth = true; // 启用 SMTP 验证功能 $mail->SMTPSecure = 'ssl'; // 使用安全协议 $mail->Host = "smtp.qq.com"; // 企业邮局域名 $mail->Port = 465; //设置ssl连接smtp服务器的远程服务器端口号 可选465或587 $mail->Username = $username; //邮件发送人的用户名(请填写完整的email地址) $mail->Password = $password; // 邮件发送人的 密码 (授权码) $mail->SetFrom($username, $username); $replyEmail = ''; //留空则为发件人EMAIL $replyName = ''; //回复名称(留空则为发件人名称) $mail->AddReplyTo($replyEmail, $replyName); //回复的地址 $mail->Subject = $subject; //邮件标题 $mail->MsgHTML($body); //邮件内容 $mail->AddAddress($toemail, $toname); //收件人地址,("收件人email","收件人姓名") if (is_array($attachment)) { // 添加附件 foreach ($attachment as $file) { is_file($file) && $mail->AddAttachment($file); } } return $mail->Send() ? true : $mail->ErrorInfo; } }
后端
// 邮箱发送 public function mailer() { if(request()->post()){ $toemail = $this->param['toemail']; $toname = $this->param['toname']; $subject = $this->param['subject']; $content = $this->param['content']; dump($this->mailer->send_mail($toemail, $toname, $subject, $content)); } return $this->fetch(); }
前端
<!--包含头部文件--> <{include file="public/header" /}> </head> <body> <div class="layui-fluid"> <div class="layui-row"> <form class="layui-form" method="post" action="<{:url('mailer')}>" onsubmit="return getHtml()"> <div class="layui-form-item"> <label class="layui-form-label">接收地址:</label> <div class="layui-input-block"> <input type="text" id="toemail" name="toemail" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">接收人:</label> <div class="layui-input-block"> <input type="text" id="toname" name="toname" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">标题:</label> <div class="layui-input-block"> <input type="text" id="subject" name="subject" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">内容:</label> <div class="layui-input-block"> <input type="hidden" id="content" name="content" value=""> <script id="editor" type="text/plain"></script> </div> </div> <div class="layui-form-item"> <label for="L_repass" class="layui-form-label"></label> <button class="layui-btn" type="submit">提交</button> </div> </form> </div> </div> <!--包含footer文件--> <{include file="public/footer" /}> <!--请在下方写此页面业务相关的脚本--> <script type="text/javascript"> $(function(){ /*实例化表单元素*/ layui.use(['form', 'layer'], function() { $ = layui.jquery; var form = layui.form, layer = layui.layer; }); getEditor() }); function getHtml() { $('#content').val(UE.getEditor('editor').getContent()); } </script> <!--/请在上方写此页面业务相关的脚本--> </body> </html>
页面展示
发送成功左上角会有个bool(true),
出现错误:
一,没有定义发送邮箱$mail->From或格式不正确,错误提示:Language string failed to load: recipients_failed test@test.com,注意,这个配置一定要正确,而且是正确的邮箱
二,没有定义邮件服务主机$mail->Host或连接失败,错误提示:Language string failed to load: connect_host
三,没有定义发送邮箱$mail->AddAddress或邮箱格式不正确,错误提示:Language string failed to load: provide_address
四,没有定义邮箱发送用户名$mail->Username,错误提示:Language string failed to load: connect_host
五,没有定义邮箱发送密码$mail->Password,错误提示:Language string failed to load: connect_host,这类错误非常明显,一般都是邮箱服务器配置不正确不能边接。