e-mail sending function in thinkphp

Source: Internet
Author: User

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"; }   } 

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.