ThinkPHP5 uses PHPMailer to send emails
phpMailer is a very powerful php email class that can set the sending email address and reply Address, email subject, html web page, upload attachments, and it is very convenient to use.
Features of phpMailer:
1. Contain multiple TO, CC, BCC and REPLY-TO in the email.
2. The platform is widely used and supported SMTP servers include Sendmail, qmail, Postfix, Gmail, Imail, Exchange, etc.
3. Support embedded images, attachments, and HTML emails.
4. Reliable and powerful debugging function.
5. Support SMTP authentication.
6. Customize the email header.
7. Supports 8bit, base64, binary and quoted-printable encoding.
phpMailer installation or download method:
1. Download from github: https://github.com/PHPMailer/PHPMailer/
2. Use composer to install:
composer require phpmailer/phpmailer
or
Add
"phpmailer/phpmailer": "~6.0"
in your composer.json file. You need to have your own mail server before sending. , when testing, it is actually most convenient to use the free mailbox you applied for. You do not need to build a server yourself. You may need to configure the SMTP service of the mailbox. Most public mailboxes (163, qq, etc.) are closed by default for security reasons.
NetEase mailbox configuration is as shown below:
## QQ mailbox related configuration is as shown below:
POP3 Server (Port 995) | smtp server (port 465 or 587) | |
smtp.qq .com |
<?php namespace app\api\controller; use think\Controller; use think\Cache; use think\Db; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class Test extends Controller { public function email(){ $mail = new PHPMailer(true); // Passing `true` enables exceptions try { //服务器配置 $mail->CharSet ="UTF-8"; //设定邮件编码 $mail->SMTPDebug = 0; // 调试模式输出 $mail->isSMTP(); // 使用SMTP $mail->Host = 'smtp.163.com'; // SMTP服务器 $mail->SMTPAuth = true; // 允许 SMTP 认证 $mail->Username = 'liqingbo27@163.com'; // SMTP 用户名 即邮箱的用户名 $mail->Password = ''; // SMTP 密码 部分邮箱是授权码(例如163邮箱,不明白看下面有说明) $mail->SMTPSecure = 'ssl'; // 允许 TLS 或者ssl协议 $mail->Port = 465; // 服务器端口 25 或者465 具体要看邮箱服务器支持 $mail->setFrom('liqingbo27@163.com', 'Mailer'); //发件人 $mail->addAddress('252588119@qq.com', 'Joe'); // 收件人 //$mail->addAddress('ellen@example.com'); // 可添加多个收件人 $mail->addReplyTo('liqingbo27@163.com', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致 //$mail->addCC('cc@example.com'); //抄送 //$mail->addBCC('bcc@example.com'); //密送 //发送附件 // $mail->addAttachment('../xy.zip'); // 添加附件 // $mail->addAttachment('../thumb-1.jpg', 'new.jpg'); // 发送附件并且重命名 //Content $mail->isHTML(true); // 是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容 $mail->Subject = '这里是邮件标题' . time(); $mail->Body = '<h1>这里是邮件内容</h1>' . date('Y-m-d H:i:s'); $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容'; $mail->send(); echo '邮件发送成功'; } catch (Exception $e) { echo '邮件发送失败: ', $mail->ErrorInfo; } } }
##Recommended tutorial: "
PHP Video TutorialThe above is the detailed content of Send email in ThinkPHP5 using PHPMailer. For more information, please follow other related articles on the PHP Chinese website!