详解thinkphp怎么配合phpmailer实现发邮件功能
3689
下面给大家讲解TP怎么配合phpmailer实现发邮件功能,希望对需要的朋友有所帮助!
TP配合phpmailer发邮件功能
在https://packagist.org查找phpmailer
使用composer下载phpmailer下载到项目中
- composer require phpmailer/phpmailer
把phpmailer配置代码
- //将PHPMailer类导入全局名称空间
- //这些必须在脚本的顶部,而不是在函数内部
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception;
- // Load Composer的自动加载器
- function send_email($to,$subject='',$content=''){
- //实例化并传递`true`会启用异常
- $mail = new PHPMailer(true);
- //服务器设置
- try {
- //Server settings
- $mail->SMTPDebug = 2; //启用详细调试输出 2详细 1简单 0不显示
- $mail->isSMTP(); //使用SMTP
- $mail->Host = 'smtp.qq.com'; //将SMTP服务器设置为通过
- $mail->SMTPAuth = true; //启用SMTP验证
- $mail->Username = '1758604817@qq.com'; // SMTP用户名
- $mail->Password = 'uzbslzhwjbjqejic'; // 邮箱的授权码,不是邮箱密码
- $mail->SMTPSecure = 'ssl'; //启用TLS加密;`的PHPMailer :: ENCRYPTION_SMTPS`鼓励
- $mail->Port = 465; //要连接的TCP端口,对于上面的`PHPMailer :: ENCRYPTION_SMTPS`使用465
- //收件人
- $mail->setFrom('1758604817@qq.com', 'pigment');
- $mail->addAddress($to); //添加收件人
- //$mail->addAddress('ellen@example.com'); //名称是可选的
- //$mail->addReplyTo('info@example.com', 'Information');
- //$mail->addCC('cc@example.com');
- //$mail->addBCC('bcc@example.com');
- //附件
- //$mail->addAttachment('/var/tmp/file.tar.gz'); //添加附件
- //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //可选名称
- //内容
- $mail->isHTML(true); //将电子邮件格式设置为HTML
- $mail->Subject = $subject;
- $mail->Body = $content;
- return $mail->send();
- } catch (Exception $e) {
- return $mail->ErrorInfo;
- }
- }
把该方法添加到application的common文件中,把它封装成一个方法,这样在任何地方都可以调用
注意事项
注意项学会在debug中排错
数据库链接问题 表名是否有错
邮箱授权码和邮箱密码不是一个东西,这点很重要
本文网址:https://www.zztuku.com/detail-9853.html
站长图库 - 详解thinkphp怎么配合phpmailer实现发邮件功能
申明:如有侵犯,请 联系我们 删除。
您还没有登录,请 登录 后发表评论!
提示:请勿发布广告垃圾评论,否则封号处理!!