PHP uses Phpmailer package to send custom mail

Source: Internet
Author: User
Tags server port ssl connection

First make sure that socket expansion support and OpenSSL are installed in PHP, if not installed with Phpize

Then download the Phpmailer file Https://github.com/PHPMailer/PHPMailer unzip on GitHub and copy the Phpmailer, exception, and SMTP files from SRC to the appropriate location. Change the namespace inside, and then write the code that sends the message

/* Send mail method * @param $to: Recipient $title: Title $content: Message content * @return bool true: Send succeeded false: Send failed */function sendMail ($to, $title, $     Content) {//Introducing Phpmailer's core file using require_once contains warning require_once ("phpmailer/class.phpmailer.php") to avoid duplicate definitions of phpmailer classes;    Require_once ("phpmailer/class.smtp.php");    Instantiate the Phpmailer core class $mail = new Phpmailer ();    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 to send mail $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 the login authentication using SSL encryption $mail->smtpsecure = ' SSL ';    Set the SSL connection to the SMTP server's remote server port number, the previous default is 25, but now the new seems to be unavailable optional 465 or 587 $mail->port = 465;    Set SMTP Helo message header This optional content 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 = ' http://www.lsgogroup.com ';    Set encoding for sent messages optional GB2312 I like Utf-8 said UTF8 in some clients will be garbled $mail->charset = ' UTF-8 '; Set the sender name (nickname) any content that appears in theThe sender's name before the sender's email address of the recipient's message $mail->fromname = ' Lsgo lab ';    SMTP login account here to fill in the string format QQ number can $mail->username = ' 12345678@qq.com ';    The password for the SMTP login uses the generated authorization code (just the latest authorization code that you saved) $mail->password = ' Sqyofzbqlfkntbncl ';    Set the sender's email address here fill in the "sender's mailbox" mentioned above $mail->from = ' 12345678@qq.com ';     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 different mailbox system that is set to that address. The second argument here is not very meaningful $mail->addaddress ($to, ' LSGO online notification ')    ;    Adding multiple recipients calls the method multiple times//$mail->addaddress (' xxx@163.com ', ' LSGO online notification ');    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 = $content;    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 ');    $status = $mail->send (); Simple judgment and hint information if ($status) {return TRUe    }else{return false; }}
Related Article

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.