"Deetom" Project development history < five > PHP mail

Source: Internet
Author: User
Tags ssl connection

The Message Class library was downloaded from GitHub.

Simple and useful;

 protected functionSendEmail ($emailAddress,$realname,$token,$title,$do){        include_onceApp_root. ' Doitphp/modules/phpmailer/phpmailerautoload.php '; //Exemplary Phpmailer Core class            $mail=NewPhpmailer (); //whether to enable debug for SMTP debugging development environment It is recommended to turn on the production environment comments off can be turned off by default Debug debug mode//$mail->smtpdebug = 1; Use SMTP authentication method to send mail, of course you can choose Pop mode SendMail way and so on not to do the detailed//can refer to http://phpmailer.github.io/PHPMailer/in detail            $mail-issmtp (); //SMTP needs authentication This must be true            $mail->smtpauth=true; //the server address of the link QQ domain mailbox            $mail->host = ' smtp.qq.com '; //Set login authentication using SSL encryption            $mail->smtpsecure = ' SSL '; //set the remote server port number for the SSL connection to the SMTP server optional 465 or 587            $mail->port = 465; //set SMTP Helo message header This optional content is arbitrary            $mail->helo = ' Hello smtp.qq.com Server '; //Setting the sender's host domain is optional by default to localhost content, it is recommended to use your domain name            $mail->hostname = ' csince.com '; //set the encoding of sent messages optional GB2312 I like utf-8. UTF8 is said to be garbled in some client's inbox.            $mail->charset = ' UTF-8 '; //Set the sender's name (nickname) any content that displays the sender's name before the sender 's e-mail address of the recipient's message            $mail->fromname = ' New Lion technology '; //SMTP login account here to fill in the string format QQ number can be            $mail->username = ' [email protected] '; //the password for the SMTP login is filled in with "Independent password", if the password is set "Independent password", then fill in QQ passwords recommended setting "Independent password"            $mail->password = ' '; //set the sender's e-mail address here fill in the "sender's mailbox" mentioned above            $mail->from = ' [email protected] '; //whether the message body is HTML-encoded note here is a method that is no longer a property of true or false            $mail->ishtml (true); //to set a recipient's mailbox address this method has two parameters the first parameter is the recipient's mailbox address the second parameter is a nickname that is set to that address. The second parameter has a small meaning.            $mail->addaddress ($emailAddress,$realname); //adding multiple recipients calls the method multiple times//$mail->addaddress (' [Email protected]/.com ', ' online users '); Add the subject of the message            $mail->subject =$title; //adding ishtml to true on top of the message body can be a complete HTML string such as: Use the file_get_contents function to read the local HTML file            $mail->body = "Dear".$realname." :<br/> Thank you for your strong support. <br/> Please login to the low Sugar Life Member Center and click on the link (otherwise invalid)$do. <br/> <a href= '/HTTP".$_server["SERVER_NAME"]. " /usercenter/checkemail?verify= ".$token."' target= ' _blank ' >http://".$_server["SERVER_NAME"]. " /usercenter/checkemail?verify= ".$token."</a><br/> If the above link cannot be clicked, please copy it into your browser's address bar to access it, the link is valid within 24 hours. "; //add an attachment to the message the method also has two parameters the first parameter is the directory where the attachment is stored (either relative to the directory, or the absolute directory) The second parameter is the name of the attachment in the message attachment//$mail->addattachment ('./d.jpg ', ' mm.jpg ')    ;    The same method can be called multiple times to upload several attachments//$mail->addattachment ('./jlib-1.1.0.js ', ' jlib.js '); The Send command returns a Boolean value of//ps: tested, if the recipient does not exist, and if the error does not appear to still return true that is, before sending, you need to implement a method to detect whether the mailbox is true and valid    $status=$mail-Send (); ////Simple judgment and prompt information    if($status) {        returnWe have sent the email to the email address you filled out, please check it. ‘; }Else{        return' Send message failed, error message: '.$mail-errorinfo; }    }

This is how the message is sent.

Include_once App_root. ' doitphp/modules/phpmailer/phpmailerautoload.php '; I need customization.

The project is to write an Activation mailbox feature:
  Public functioncheckemailaction () {$model=$this->model ("Csuser"); //View database Whether this user has been activated for a mailbox        $res=$model->getone ("id =?",$this->userinfo[' ID ']); $token=$_get[' Verify ']; $startdate=$res[' Email_token_time_ck '];//get from Database        $enddate=Date(' Y-m-d h:i:s ', Time());//Current Time        $date= Floor((Strtotime($enddate)-Strtotime($startdate))/86400*24*60*60); if(!Empty($token)){            //The user clicks inside the mailbox to connect to this controller, does the processing. Verify token            if($this->token ($res["username"],$res[' Password ']) ==$token ){                if($res["checkemail"] = = 1){                    $str= "Mailbox activation is successful, no need to activate again." "; }Else{                    if($date< 86400){                        $id=$this->userinfo[' ID ']; $sql= "Update induser set ' checkemail ' = 1 where ' id ' =$id"; $model->execute ($sql); $str= "Congratulations, email verification is successful. "; }Else{                        $str= "The link has expired, please reactivate the mailbox and get a new activation email." "; }                }            }            //Enter the next level if the last email verification mailbox time is greater than 24 hours or has never been verified}Else if(($date> 86400 | |$startdate= = ") &&$res[' checkemail '] = = 0){            //representative never activated             $token=$this->token ($res["username"],$res[' Password ']); if(Empty($res[' Realname '])){                 $str=$this->sendemail ($res[' Email '], ' user ',$token, "Activate Mailbox", "Activate Mailbox"); }Else{                 $str=$this->sendemail ($res[' Email '],$res[' Realname '],$token, "Activate Mailbox", "Activate Mailbox"); }             //Modify Database             $time=Date(' Y-m-d h:i:s ', Time()); $id=$this->userinfo[' ID ']; $sql= "Update induser set ' email_token_time_ck ' = '$time' WHERE ' id ' =$id"; $model->execute ($sql); }Else{            $str= "The mailbox is activated, no need to verify again." ";//The mailbox has passed validation         }        $this->assign (Array(            ' Res ' =$str        )); $this->display ("Checkemail"); }

This is the business code ...

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.