thinkphp Send mail under SMTP ____php

Source: Internet
Author: User
Tags server port

Two ways to send mail, bloggers have personally tested the success of the 1.TP official website Method

(http://www.thinkphp.cn/extend/248.html)
a). Introducing function Libraries: Email.class.php placed under the component folder
b). configuration file: common/conf/config.php, add the following:

    ' Smtp_server ' => ' smtp.163.com ',//                 mail server
        ' smtp_port ' => ',                             //mail server Port
        ' smtp_user_email ' = > ' @163.com ',     //SMTP server's user mailbox (general senders also have to use this mailbox)
        ' Smtp_user ' => ' @163.com ',            //smtp server account name
        ' Smtp_ PWD ' => ' authorized password ',                         //smtp server account password
        ' smtp_mail_type ' => ' HTML ',                       //Send Message type: Html,txt (note all uppercase)
        ' Smtp_time_out ' =>30,                            //Timeout time
        ' Smtp_auth ' =>true,  
c). Actual call: controller.php, can be applied directly in the controller, as follows
        $mail = new \component\email ();
        $data [' mailto ']  =   "123456@qq,.com";//Recipient
        $data [' subject '] =   ' Welcome to register website ';    The message title
        $data [' body ']    =   "message content";    Message body Content
        if ($mail->send ($data))
           {
                //message sent successfully ...
           }
           else
           {
                //message sent failed ...
           }
2. Class Library
a). Introducing the function library, Phpmailer moved to Library\vendor b in thinkphp
. configuration file, common/conf/config.php add as follows:
Configure mail sending server
        ' mail_host ' => ' smtp.163.com ',//SMTP server name smtp.exmail.qq.com
        ' Mail_smtpauth ' =>true ,//Enable SMTP authentication renxiaole66@126.com
        ' mail_username ' => ' @163.com ',//Your mailbox name ' Mail_from ' => ' @163.com ',//
        Sender's address
        ' mail_fromname ' => ' MR PK ',//Sender's name
        ' Mail_password ' => ' authorized password ',//mailbox Authorization Password
        ' mail_charset ' = > ' utf-8 ',//Set mail encoding
        ' mail_ishtml ' =>true,//whether HTML format mail
 c). Introduce a function, common/common/function.php, create this PHP file, and then the frame will automatically load 
function SendMail ($to, $title, $content) {vendor (' phpmailer.phpmailerautoload '); $mail = new Phpmailer (); Instantiate $mail->issmtp (); Enable SMTP $mail->host=c (' mail_host '); The name of the SMTP server (here, for example, QQ mailbox) $mail->smtpauth = C (' Mail_smtpauth '); Enable SMTP authentication $mail->username = C (' Mail_username '); Your mailbox name $mail->password = C (' Mail_password '); Mailbox password $mail->from = C (' Mail_from '); The sender's address (i.e. your email address) $mail->fromname = C (' Mail_fromname ');
              Sender name $mail->addaddress ($to, "respected customer"); $mail->wordwrap = 50; Set the length of each line of characters $mail->ishtml (' mail_ishtml '); Whether the HTML format mail $mail->charset=c (' Mail_charset '); Set the message encoding $mail->subject = $title; Mail subject $mail->body = $content; Message content $mail->altbody = "This is a plain text body in the non-profit HTML email client"; The message body does not support alternate display of HTML RetuRN ($mail->send ()); }
d). Actual call: controller.php, can be applied directly in the controller, as follows
   if (SendMail ("$_post[user_email]", "Welcome to register Website", "$message")) {
                    $this->success (' send successfully, login after activation ', U (' login '));
                } else{
                    $this->error (' send failed ');
                }
            Else{//add () Failure
                //echo "error";
                $this->error (' register failed ', U (' Register '));
            }
Note:

1. When configuring the account password for the mailbox, the password is SMTP "authorized password", and your login password is not the same,
Otherwise hint: 535 authentication failed
2. SMTP service must be turned on, with 163 mailboxes as an example:

In this way, the user registration activation, user retrieve password and so on, can not be achieved. O (∩_∩) o

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.