Home >Backend Development >PHP Tutorial >ThinkPHP uses PHPMailer to implement email sending implementation code_PHP tutorial
This article uses ThinkPHP version 2.1 and PHPMailer version 5.1. (The latter is recommended that you download it directly from this blog, because we cannot guarantee that the following code will run normally in all versions of PHPMailer)
Here are the specific steps:
The first step is to add the 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)
The second step is to add the send email function
Add the following code to the common.php file in the Common folder in the project directory (please create it if it does not exist):
Edit config.php in the Conf directory and add the following content in the return array
The email login account may need to include the content after @, please try it yourself^_^
The fourth step is to send the email in Action
Because ThinkPHP will automatically load the function in common.php, so When you need to send an email, just use the following code.
SendMail("xxx@xxx.com","Email title","Email text");
At this point, this tutorial has ended. Sprinkle flowers~~~
Welcome students who have successfully configured SendMail("dreamrunner@foxmail.com","I can also use ThinkPHP to send emails","Wow wow wow~~");
A few more instructions
What should I do if I need to break the line in the email body? In fact, the simplest way is——
SendMail("xxx@xxx.com","Email title","Email text
I changed the line~~!");
QQ mailboxes (including foxmail) and NetEase’s 126 and 163 were tested successfully. Because it has been difficult to access GMail recently, I did not test it. I heard that GMail requires the use of SSL. Students who need it can Google Baidu PHPMailer on their own.