thinkphp uses phpmailer to send mail
- 2021-08-05 09:20:35
- OfStack
This article illustrates how thinkphp uses phpmailer to send mail. Share it for your reference. The specific analysis is as follows:
phpmailer sending mail is the first choice for php developers to send mail plug-in. Below I will introduce how to integrate phpmailer to thinkphp framework. Interested friends can refer to 1.
phpmailer send mail function is very powerful, today's real experience 1, here is a simple 1 configuration, I am used in thinkphp.
Configuration steps:
1. Configure the sending mail class in the background, located in admin/common/common. php, with the following code:
function sendmail($tomail,$title,$content)
{
/* Mail Setup Information */
$email_set = C('EMAIL_SET');
Vendor('phpmailer.class#phpmailer');
Vendor("phpmailer.class#smtp"); // Optional , Otherwise, it will be in class.phpmailer.php Include in
$mail = new PHPMailer(true); // Instantiation PHPMailer Class ,true Indicates that an exception is thrown when an error occurs
$mail->IsSMTP(); // Use SMTP
$mail->CharSet ="UTF-8";// Set mail code
$mail->Host = $email_set['Host']; // SMTP server
$mail->SMTPDebug = 1; // Enable SMTP Debugging 1 = errors 2 = messages
$mail->SMTPAuth = true; // Server requires authentication
$mail->Port = $email_set['port']; // Setting Port
// $mail->SMTPSecure = "ssl";
/*
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
*/
$mail->Username = $email_set['email_user']; //SMTP User account of the server
$mail->Password = $email_set['email_pwd']; //SMTP User password for server
$mail->AddReplyTo($email_set['email'],$email_set['email_name']); // Recipient replies to this mailbox , You can execute this method multiple times
if (is_array($tomail)){
foreach ($tomail as $m){
$mail->AddAddress($m, 'user');
}
}else{
$mail->AddAddress($tomail, 'user');
}
$mail->SetFrom($email_set['email'],$email_set['email_name']);
// $mail->AddAttachment('./img/phpmailer.gif'); // Add attachments , Repeat this method if there are multiple attachments
$mail->Subject = $title;
// The following is related to the content of the email
$mail->Body = $content;
$mail->IsHTML(true);
//$body = file_get_contents('tpl.html'); // Get html Web page content
// $mail->MsgHTML(eregi_replace("[]",'',$body));
return $mail->Send()? true:false;
}
2: Configuration parameters in the configuration file, the code is as follows:
/* Mail Settings */
'EMAIL_SET'=>array(
'Host'=> "smtp.163.com",
'Port'=>'25',
'email_user'=>'liuying',
'email_pwd'=>'123456',
'email'=>'jb51@163.com',
'email_name'=>'jb51 Script House ',
)
3. Test the sending code, which is as follows:
sendmail('11234@126.com',' How do you do ',' Here is the content ');
I hope this article is helpful to everyone's PHP programming.