The first use of the thinkphp framework, development of a mail sending function, due to the framework is not familiar with the toss for several hours finally succeeded, the following is the code record.
This function can only be used in thinkphp and requires support for phpmailer extensions;
Phpmail's:
Https://code.google.com/a/apache-extras.org/p/phpmailer
Place the Phpmailer decompression post extension in the third-party class Library extension directory: thinkphp/extend/vendor/folder, and use the Vendor method to import. For more detailed information, refer to: Http://document.thinkphp.cn/manual_3_2.html#lib_extend
Step 1: Create the Think_send_mail method
/** * System mail Send function * @param string $to incoming mail person mailbox * @param string $name Recipient name * @param string $subject message subject * @param str ING $body message content * @return Boolean*/functionThink_send_mail ($to,$name,$subject= ",$body= ' ') {Vendor (' Phpmailer.class#phpmailer ');//Guide class.phpmailer.php class files from Phpmailer directory $mail=New\phpmailer;//the "\" number must be added here otherwise error//Inform class to use SMTP $mail-issmtp (); //Enable this for testing $mail->smtpdebug = 2; //Enable SMTP Authentication $mail->smtpauth =true; //Host of the SMTP Server $mail->host = ' smtp.126.com ';//SMTP server username//Port of the SMTP server $mail->port = 25;//SMTP Server port//SMTP User Name $mail->username = "[email protected]";//Email address//SMTP User Password $mail->password = "********";//Email password//Set from email address $mail->setfrom ("[Email protected]",$name); //ADD Subject $mail->subject=$subject; //ADD the body for mail $mail->msghtml ($body); //Add to Address $mail->addaddress ($to,$name); //Finally Send the Mail return $mail->send ()?true:$mail-errorinfo;}
Step 2: Call the Controller
namespace Account\controller; UseThink\controller;classAccountControllerextendsController {//calling the Send mail feature Public functiongetpwd () {$toEmail=$_post[' Email ']; $subJect= ' Mail Subject '; $body= ' message content '; $name= ' Sender name '; $result=$this->think_send_mail ($toEmail, ' Sever ',$subJect,$body); if($result) Echo"OK"; Else Echo"Failed"; } }