首先使用composer安装phpmailer
composer require phpmailer/phpmailer
然后就可以在thinkphp项目中使用phpmailer来发送邮件了
<?php
namespace app\index\controller;
use PHPMailer\PHPMailer\PHPMailer;
class Index
{
public function index()
{
$host = 'smtp.qq.com';//SMTP服务器
$port = 465;//端口
$email = 'xxxxx@qq.com';//发件人邮箱
$password = '123456';//发件人邮箱密码或授权码
$name = '发件人昵称';//发件人昵称
$title = '邮件标题';//邮件标题
$body = '邮件正文';//邮件正文,支持html
$recipient_email = 'xxx@qq.com';//收件人邮箱
//发送邮件
$res = $this->sendMail($host,$port,$email,$password,$name,$title,$body,$recipient_email);
if($res){
return ['status'=>1,'msg'=>'发送成功'];
}else{
return ['status'=>0,'msg'=>'发送失败'];
}
}
public function sendMail($host,$port,$email,$password,$name,$title,$body,$recipient_email)
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0; //2开启debug调试模式 // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $email; // SMTP username
$mail->Password = $password; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $port; // TCP port to connect to
$mail->CharSet = 'UTF-8';//编码
//Recipients
$mail->setFrom($email,$name);//发送者邮箱
$mail->addAddress($recipient_email,'');//接收者邮箱 // Add a recipient
// $mail->addAddress('ellen@example.com'); // Name is optional
// $mail->addReplyTo('1662866246@qq.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// 附件
// $mail->addAttachment('pic.png');//附件
// $mail->addAttachment('/var/tmp/file.tar.gz'); //附件 // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// 内容
$mail->isHTML(true);//是否是HTML邮件内容
$mail->Subject = $title;//邮件标题
$mail->Body = $body;//HTML邮件内容
$mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';//普通文本邮件内容
$res = $mail->send();
return $res;
}
}
namespace app\index\controller;
use PHPMailer\PHPMailer\PHPMailer;
class Index
{
public function index()
{
$host = 'smtp.qq.com';//SMTP服务器
$port = 465;//端口
$email = 'xxxxx@qq.com';//发件人邮箱
$password = '123456';//发件人邮箱密码或授权码
$name = '发件人昵称';//发件人昵称
$title = '邮件标题';//邮件标题
$body = '邮件正文';//邮件正文,支持html
$recipient_email = 'xxx@qq.com';//收件人邮箱
//发送邮件
$res = $this->sendMail($host,$port,$email,$password,$name,$title,$body,$recipient_email);
if($res){
return ['status'=>1,'msg'=>'发送成功'];
}else{
return ['status'=>0,'msg'=>'发送失败'];
}
}
public function sendMail($host,$port,$email,$password,$name,$title,$body,$recipient_email)
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0; //2开启debug调试模式 // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $email; // SMTP username
$mail->Password = $password; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $port; // TCP port to connect to
$mail->CharSet = 'UTF-8';//编码
//Recipients
$mail->setFrom($email,$name);//发送者邮箱
$mail->addAddress($recipient_email,'');//接收者邮箱 // Add a recipient
// $mail->addAddress('ellen@example.com'); // Name is optional
// $mail->addReplyTo('1662866246@qq.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// 附件
// $mail->addAttachment('pic.png');//附件
// $mail->addAttachment('/var/tmp/file.tar.gz'); //附件 // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// 内容
$mail->isHTML(true);//是否是HTML邮件内容
$mail->Subject = $title;//邮件标题
$mail->Body = $body;//HTML邮件内容
$mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';//普通文本邮件内容
$res = $mail->send();
return $res;
}
}