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中排错
数据库链接问题 表名是否有错
邮箱授权码和邮箱密码不是一个东西,这点很重要
推荐:《最新的10个thinkphp视频教程》
以上就是详解tp怎么配合phpmailer实现发邮件功能的详细内容。
