This article uses the thinkphp version 2.1 and Phpmailer 5.1. (the latter suggests you download it directly from this blog because we cannot guarantee that the following code will work correctly in all versions of Phpmailer)
The following are the specific steps:
First step, add Phpmailer class Library
Click here to download
Unzip the downloaded file and move the Phpmail directory to the vendor in the thinkphp directory. (Please make sure the class.phpmailer.php file is in ThinkPHPVendorPHPMailerclass.phpmailer.php)
Step two, add the Send mail function
Add the following code to the common.php file (if not created) in the common folder in the project directory:
Copy Code code as follows:
<?php
/**********
* Send message *
**********/
Funct Ion SendMail ($address, $title, $message)
{
vendor (' Phpmailer.class#phpmailer ');
$mail =new phpmailer ();
//Set Phpmailer use SMTP server to send email
$mail->issmtp ();
//Set the character encoding of the message, if not specified, then ' UTF-8 '
$mail->charset= ' UTF-8 ';
//Add a recipient address that can be used multiple times to add multiple recipients
$mail->addaddress ($address);
//Set message body
$mail->body= $message;
//Set the From field of the headers.
$mail->from=c (' mail_address ');
//Set sender name
$mail->fromname= ' lilyrecruit ';
//Set mail title
$mail->subject= $title;
//Set up the SMTP server.
&NBSP;&NBSP;&NBsp $mail->host=c (' mail_smtp ');
//set to require validation
$mail->smtpauth=true;
//Set user name and password.
$mail->username=c (' mail_loginname ');
$mail->password=c (' Mail_password ');
//Send mail.
return ($mail->send ());
}
?>
Third Step, configure mailbox information
Edit the config.php in the Conf directory and add the following in return array
Copy Code code as follows:
' Mail_address ' => ' xxx@126.com ',//email address
' Mail_smtp ' => ' smtp.126.com ',//mailbox SMTP Server
' Mail_loginname ' => ' xxx ',//e-Mail login account
' Mail_password ' => ' xxx ',//e-mail password
The mailbox login account may need to include the contents of the @ behind, please try ^_^
Step fourth, send mail in action
Because thinkphp automatically loads the functions in common.php, you only need to use the following code when you need to send a message.
SendMail ("xxx@xxx.com", "Mail title", "Message body");
So far, this tutorial is over. Sprinkle flowers ~ ~ ~
Welcome to the successful configuration of the classmate SendMail ("dreamrunner@foxmail.com", "I will also send mail with thinkphp", "Blah blah ~ ~");
There are also some explanations
What if you need to change lines in the body of the message? In fact, the easiest way to do this is--
SendMail ("xxx@xxx.com", "Mail title", "Message body")
I'm changing, ~~!. ");
QQ mailbox (including Foxmail) and NetEase 126, 163 are tested successfully. Because the recent visit to Gmail is difficult, so there is no test. Heard that Gmail requirements must use SSL, there is a need for students to Google Baidu Phpmailer method.