Takeaway:
This article will teach you how to use PHPMailer extension to send emails. Let’s get started!
Official download address (need to organize themselves) :
Github.com/PHPMailer/P…
CSDN Download address (already tested) :
Download.csdn.net/download/qq…
1. Add extensions
1, download the good phpmail. zip decompress, put in Thinkphp extend directory, decompress folder named: PHPMailer
Set the namespace of all files in PHPMailer to namespace PHPMailer.
2. Encapsulate the method of sending mail
1. Add the PHPMailer extension to the application/common.php file
use PHPMailer\PHPMailer;
Copy the code
SendMail (); sendMail (); sendMail ();
/**
* 邮件发送
*
* @paramString $to receiver *@paramString $title title *@paramString $content * *@returnBool Send status */
function sendMail($to.$title.$content){
$mail=new PHPMailer();
$mail->IsSMTP(); // Set up PHPMailer to use SMTP server to send Email
$mail->CharSet='UTF-8'; // Set the character encoding of the mail, this is very important, otherwise Chinese characters are garbled
$mail->SMTPAuth = true; // Specifies whether SMTP requires password authentication. True indicates that SMTP requires password authentication
$mail->Host = "smtp.qq.com"; // Set the mail server address
$mail->Port = 465; // Set the mail server port. The default port is 25
$mail->From = "[email protected]"; // Set the sender email address
$mail->FromName = "Easy card"; // Set the sender's name
$mail->SMTPSecure = "ssl";
$mail->Username = "[email protected]";
$mail->Password = "Authorization Code";
$mail->AltBody = "text/html"; // optional, comment out and test
$mail->IsHTML(true); // Sets whether the content is HTML type
$mail->Subject = $title; // Set the subject of the message
$mail->Body = $content;
$mail->AddAddress(trim($to), ' '); // Set the receiver address
if (!$mail->Send()) {
// Send an email
$mail->ErrorInfo;
return false;
} else {
// echo "Sent successfully ";
return true; }}Copy the code
3, call send mail is very simple, as follows:
$to ="[email protected]";// Enter the email address to send to
$title = "Mail Test";/ / title
$content ="Testing phpMailer's ability to send mail";// The content of the email
$falg = sendMail($to.$title.$content);
if($falg) {echo "Sent successfully";
}else{
echo "Send failed";
}
Copy the code