PHP mail sending function -- ThinkPHP3.2.3, thinkphp send mail

Source: Internet
Author: User

PHP mail sending function -- ThinkPHP3.2.3, thinkphp send mail

First step: in the internet down a PHPMailer plug-in, plug-in address "https://github.com/PHPMailer/PHPMailer download unzip, Here we only need to use two of the files, as shown in:

 

Place the class. phpmailer. php and class. smtp. php files
ThinkPHP/Library/Vendor/PHPMailer/class. phpmailer. php (case sensitive)
ThinkPHP/Library/Vendor/PHPMailer/class. smtp. php

 

Note: The default third-party class library directory of thinkPHP is placed. php defines define ('vendor _ path', APP_PATH. 'Common/Vendor/'); the path of the file must be the same, so that the class 'phpmailer' not found is avoided.

 

2. Create the User-Defined function file Application/Home/Common/function. php and place the following functions:

1/** 2 * function: email sending function 3 * @ param string $ to target email address 4 * @ param string $ subject (title) 5 * @ param string $ to mail content 6 * @ return bool true 7 */8 function sendMail ($ to, $ subject, $ content) {9 vendor ('phpmailer. class # smtp '); 10 vendor ('phpmailer. class # phpmailer '); // note the case here. Otherwise, the class cannot be found. PHPMailer is the folder name, and class # phpmailer represents the class. phpmailer. php file name 11 $ mail = new PHPMailer (); 12 // assemble the email server 13 if (C ('mail _ SMTP ') {1 4 $ mail-> IsSMTP (); 15} 16 $ mail-> Host = C ('mail _ host '); // for parameter explanations, see the following configuration information. Note 17 $ mail-> SMTPAuth = C ('mail _ SMTPAUTH '); 18 $ mail-> Username = C ('mail _ username'); 19 $ MAIL-> Password = C ('mail _ password '); 20 $ mail-> SMTPSecure = C ('mail _ SECURE '); 21 $ MAIL-> CharSet = C ('mail _ charset '); 22 // assemble the mail header information 23 $ MAIL-> From = C ('mail _ username'); 24 $ mail-> AddAddress ($ ); 25 $ mail-> FromName = C ('mail _ fromname'); 26 $ MAIL-> Is HTML (C ('mail _ ISHTML '); 27 // assemble the MAIL Body information 28 $ mail-> Subject = $ subject; 29 $ mail-> Body = $ content; 30 // send email 31 if (! $ Mail-> Send () {32 return FALSE; 33} else {34 return TRUE; 35} 36}

3. The C method is used in the above functions to load some configuration information, so we have to go to the configuration file (default/Application/Home/Conf/config. php) Add the following configuration information:

1 <? Php 2 return array (3 // other configuration items are omitted ...... 4 // configure the email sending Server 5 'mail _ SMTP '=> TRUE, 6 'mail _ host' => 'smtp .163.com ', // send an email to SMTP server 7'mail _ SMTPAUTH '=> TRUE, 8' MAIL _ username' => '2017 *** @ 163.com ', // SMTP Server login username 9 'mail _ password' => '123456abc', // SMTP Server login PASSWORD 10' MAIL _ SECURE '=> 'tls ', 11 'mail _ charset' => 'utf-8', 12 'mail _ ISHTML '=> TRUE, 13 'mail _ fromname' => 'xx website customer ', 14 );

4. Start calling. Assume that the URL /? M = home & c = index & a = send access, then we add the following method to the Application/Home/Controller/IndexController. class. php file:

 

1 <? Php 2 namespace Home \ Controller; 3 use Think \ Controller; 4 class IndexController extends Controller {5 public function index () {6 7} 8 public function send () {9 if (sendMail ('vsiryxm @ qq.com ', 'Hello! Email Subject ',' This is the body of the test email! ') {10 echo' sent successfully! '; 11} 12 else {13 echo' failed to send! '; 14} 15} 16}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.