多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
>[info]类库地址:https://github.com/PHPMailer/PHPMailer ## :-: **使用第3方类库的常见思路** 1、各种引入(use也是引入) 2、new一个实例 3、通过new出来的对象,调用各种属性赋值和方法完成功能 >[warning]注意:我只是给官方demo拉过来跑了一遍,给注释改成中文了,代码中另附了3个小案例 ~~~ php <?php /** * 发送邮件 * 使用第3方类库的常见思路 * 1、各种引入(use也是引入) * 2、new一个实例 * 3、通过new出来的对象,调用各种属性赋值和方法完成功能 */ //1、各种引入(use也是引入) use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $rootPath = dirname(__FILE__); require $rootPath.'/vendor/autoload.php'; $mail = new PHPMailer(true); //2、实体化类 //3、通过new出来的对象,调用各种属性赋值,方法完成功能 try { //配置 $mail->SMTPDebug = 2; // 调试模式和telnet发邮件一模一样 $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.qq.com'; //使用腾讯的 163有毒老是什么反垃圾 $mail->SMTPAuth = true; //启用SMTP身份验证 $mail->CharSet = 'UTF-8'; //设置邮件编码 $mail->Username = '692391332@qq.com'; //SMTP username $mail->Password = 'bhrrcasxxxxxxxxx'; // SMTP password //$mail->SMTPSecure = 'tls'; //启用TLS加密,“ssl”也接受 //$mail->Port = 587; //连接到的tcp端口 $mail->setFrom('692391332@qq.com', 'Mailer'); //发件人 $mail->addAddress('134xxxxxxxx@163.com', 'Joe User'); //收件人,//第2个参数可选,发给多个人直接写foreach里 $mail->addReplyTo('692391332@qq.com', 'Information'); //假设收件人回复,发给谁 //$mail->addCC('cc@example.com'); //抄送 //$mail->addBCC('bcc@example.com'); //密送 $mail->isHTML(true); //$mail->Body带html标签,就true $mail->Subject = 'Here is the subject'; //主题 //$mail->AltBody = '不带html标签的'; //不知道这干嘛用 //发送实例:1、官网demo发送的内容带html标签。注意:需要给isHTML打开设为true //$mail->Body = '带html<b>标签的</b>'; //发送实例:2、发送附件 //$mail->addAttachment('img/1.png'); //发送带图片的 //$mail->Body = '单独发送附件不可以,要带内容'; //发送实例:3、使用邮件模板发送内容。注意:要先读取文件和不要使用$mail->Body $mail->msgHTML(file_get_contents('img/emiltpl.html')); $mail->send(); //发送 echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; } ~~~ **emiltpl.html发邮件用的 邮件模板demo** ~~~ html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> #div{width:100px;height:100px;background:red;} /*内连样式写法*/ </style> </head> <body> <div id="div"> test </div> </body> </html> ~~~