The example in this article describes how thinkphp uses Phpmailer to send messages. Share to everyone for your reference. The specific analysis is as follows:
Phpmailer Send mail is the PHP developers preferred a mail-sending plug-in, I will introduce how to integrate Phpmailer to the thinkphp framework, interested friends can refer to.
Phpmailer Send the Mail function is very powerful, today real experience, here first simple to say configuration, I am in thinkphp use.
Configuration steps:
1. Background configuration send mail class, location admin/common/common.php, code as follows:
Copy Code code as follows:
function SendMail ($tomail, $title, $content)
{
/* Message Setup Information * *
$email _set = C (' Email_set ');
Vendor (' Phpmailer.class#phpmailer ');
Vendor ("Phpmailer.class#smtp"); Optional, otherwise it will be included in the class.phpmailer.php
$mail = new Phpmailer (true); Instantiate the Phpmailer class, true to throw an exception when an error occurs
$mail->issmtp (); Using SMTP
$mail->charset = "UTF-8";//Set message encoding
$mail->host = $email _set[' Host ']; SMTP Server
$mail->smtpdebug = 1; Enable SMTP Debugging 1 = errors 2 = messages
$mail->smtpauth = true; Server needs authentication
$mail->port = $email _set[' Port ']; Set port
$mail->smtpsecure = "SSL";
/*
$mail->smtpsecure = "SSL";
$mail->host = "smtp.gmail.com";
$mail->port = 465;
*/
$mail->username = $email _set[' Email_user ']; User account for SMTP server
$mail->password = $email _set[' email_pwd ']; User password for SMTP server
$mail->addreplyto ($email _set[' email '), $email _set[' email_name '); Reply to this mailbox when the recipient replies, you can execute the 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 an attachment and repeat the method if there are multiple attachments
$mail->subject = $title;
The following are the message content related
$mail->body = $content;
$mail->ishtml (TRUE);
$body = file_get_contents (' tpl.html '); Get HTML page Content
$mail->msghtml (Eregi_replace ("[]", ", $body));
return $mail->send ()? True:false;
}
2: Configuration parameters in the configuration file, the code is as follows:
Copy Code code 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 cloud-dwelling community ',
)
3. Test send code, code as follows:
Copy Code code as follows:
SendMail (' 11234@126.com ', ' hello ', ' here is content ');
I hope this article will help you with your PHP program design.