[Consolidate the PHP Basics] PHP sends an email (PHPMailer), consolidate phpmailer
Address of this Article
Reference address
Sharing outline:
1. Overview
2. write code to send an email
3. References
1. Overview
This article describes how to use the mail class library PHPMailer to send emails.
We often need the mail function when creating a project. In fact, the PHP language already has a mail () method, not to mention that this method has very few features, and wants to use mail () you must configure the smtp server by yourself when sending the mail by using the method. Here we will not talk about the use of mail () (function call is really simple ). Therefore, we recommend the second method: PHPMailer.
2. write code to send an email
1) [Download PHPMailer]
First, go to http://phpmailer.sourceforge.net/to download the latest phpmailerpackage. (This package must be used by the PHPMailer method. Currently, it depends on gitHub ).
Directly download the compressed package also line: https://github.com/wozhuzaisi/PHPMailer/archive/master.zip
2) [code implementation]
After the download is complete, decompress the package to the corresponding directory. You can see the class. phpmailer. php In the decompressed folder (you need to include this file when you call PHPMailer)
Sample Code:
<? Php // 1. [] PHPMailer: https://github.com/wozhuzaisi/PHPMailer/archive/master.zip // 2. [Mailbox configuration SMTP] This article implements the smtp protocol 163 mailbox to send. For other mailboxes and protocols, see: http://blog.wpjam.com/m/gmail-qmail-163mail-imap-smtp-pop3/ // 3. [Text Encoding] Ensure UTF-8 text encoding; otherwise, garbled characters may appear in the email. // 4. [run mode] Call the smtp_mail () function to test the email // parameter description (recipient email address, recipient name, email subject, email content, additional information, and sender username) smtp_mail ("receiveUser@haodf.com", 'receiveusername', "[title] 12.01 test mail", "[content] Test mail", "", $ fromUsername = "mail sender "); echo "<br> end <br>"; function smtp_mail ($ receiveEmailAddress, $ receiveUserName, $ subject, $ bod Y, $ extraHdrs = '', $ fromUsername) {$ path = 'phpmailer-master/'; require_once ($ path. "class. smtp. php "); require ($ path. "class. phpmailer. php "); $ mail = new PHPMailer (); $ mail-> IsSMTP (); // send via SMTP // use the 163 email here $ mail-> Host = "smtp.163.com"; // SMTP servers $ mail-> SMTPAuth = true; // turn on SMTP authentication $ mail-> Username = "yourEmailUserName"; // SMTP username Note: For normal mail authentication, you do not need to add @ domain name. Here is my 163 mailbox $ ma Il-> Password = "yourEmailPassWord"; // enter the password of the mailbox here $ mail-> From = $ fromMailAddress = "yourName@163.com "; // sender email $ mail-> FromName = $ fromUsername; // sender $ mail-> CharSet = "UTF-8"; // specify the character set here! After the UTF-8 is specified, the mail title and sender will not be garbled, if it is GB2312 title will be garbled $ mail-> Encoding = "base64"; $ mail-> AddAddress ($ receiveEmailAddress, $ receiveUserName); // recipient's email address and name $ mail-> AddReplyTo ($ fromMailAddress, $ fromUsername); // $ mail-> WordWrap = 50; // set word wrap newline words // $ mail-> AddAttachment ("/var/tmp/file.tar.gz "); // attachment // $ mail-> AddAttachment ("/tmp/image.jpg", "new.jpg"); // $ mail-> IsHTML (true ); // send as HTML // Email Subject $ Mail-> Subject = $ subject; // email content $ mail-> Body = $ body; // $ mail-> AltBody = "text/html"; if (! $ Mail-> Send () {echo "error <p>"; echo "error:". $ mail-> ErrorInfo; exit;} else {echo "success! ";}}
That's all. Thank you for your criticism.
3. References
1) Use PHPMailer to send emails
2) PHPMailer-FTD2012-blog