分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

thinkphp5中如何正确使用phpmailer发送邮件

发布时间:2023-09-06 02:33责任编辑:苏小强关键词:thinkphp邮件

步骤1、先下载phpmailer放到vendor目录,如下图

步骤2、在公共函数文件common.php中加入以下代码:

function send_email($to,$subject=‘‘,$content=‘‘){ ???vendor(‘phpmailer.PHPMailerAutoload‘); ////require_once vendor/phpmailer/PHPMailerAutoload.php‘; ???$mail = new PHPMailer; ???$mail->CharSet ?= ‘UTF-8‘; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 ???$mail->isSMTP(); ???//Enable SMTP debugging ???// 0 = off (for production use) ???// 1 = client messages ???// 2 = client and server messages ???$mail->SMTPDebug = 0; ???//调试输出格式 ???//$mail->Debugoutput = ‘html‘; ???//smtp服务器 ???$mail->Host = config(‘email.smtp_server‘); ???//端口 - likely to be 25, 465 or 587 ???$mail->Port = 465; ???if($mail->Port === 465) $mail->SMTPSecure = ‘ssl‘;// 使用安全协议 ???//Whether to use SMTP authentication ???$mail->SMTPAuth = true; ???//发送邮箱 ???$mail->Username = config(‘email.smtp_user‘); ???//密码 ???$mail->Password = config(‘email.smtp_pwd‘); ???//Set who the message is to be sent from ???$mail->setFrom(config(‘email.smtp_user‘),‘尊敬的用户‘); ???//回复地址 ???//$mail->addReplyTo(‘replyto@example.com‘, ‘First Last‘); ???//接收邮件方 ???if(is_array($to)){ ???????foreach ($to as $v){ ???????????$mail->addAddress($v); ???????} ???}else{ ???????$mail->addAddress($to); ???} ???$mail->isHTML(true);// send as HTML ???//标题 ???$mail->Subject = $subject; ???//HTML内容转换 ???$mail->msgHTML($content); ???//Replace the plain text body with one created manually ???//$mail->AltBody = ‘This is a plain-text message body‘; ???//添加附件 ???//$mail->addAttachment(‘images/phpmailer_mini.png‘); ???//send the message, check for errors ???return $mail->Send();}

 步骤3、在config.php中增加以下代码:

//email ???‘email‘ ?=> [ ???????‘smtp_server‘ ??=> ‘邮箱服务地址‘, ???????‘smtp_port‘ ????=> ‘端口号‘, ???????‘smtp_user‘ ????=> ‘用户名‘, ???????‘smtp_pwd‘ ?????=> ‘密码‘, ???],

  步骤4、调用

send_email($to, $title, $content);//$to ?要发送的地址//$title 邮件标题//$content 邮件内容

  

thinkphp5中如何正确使用phpmailer发送邮件

原文地址:https://www.cnblogs.com/bbee888/p/10440925.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved