PHP mail sending PHPMailer usage example

Source: Internet
Author: User
Tags learn php programming
This article mainly introduces PHP mail sending PHPMailer usage, describes in detail the installation and configuration methods and function implementation code, which is very useful, for more information about PHPMailer in PHP mail sending, see the example in this article. Share it with you for your reference. The procedure is as follows:

1. install sendmail on the server

sudo apt-get install sendmail 

2. start sendmail

sudo /etc/init.d/sendmail start 

3. modify php. ini

[mail function] SMTP = localhost smtp_port = 25 sendmail_from = me@example.com 

4. Function sendMail Function:

<? Php/* call PHPMailer to send an email * @ param String $ receiver recipient * @ param String $ sender * @ param String $ sender_name if the sender name is null, use the sender address instead of * @ param string $ subject email subject * @ param String $ content email content * @ param boolean $ whether ishtml is an html email * @ param Array $ attachements attachment * @ return boolean */function sendMail ($ receiver, $ sender, $ sender_name, $ subject, $ content, $ ishtml = true, $ attachments = array () {include_once "cl Ass-phpmailer.php "; if (empty ($ receiver) | empty ($ sender) | empty ($ subject) | empty ($ content) {return false ;} $ mail = new PHPMailer (); // $ mail-> IsSMTP (); // sent by smtp // $ mail-> Host = "smtp.gmail.com "; // SMTP server // $ mail-> Port = 465; // SMTP Port // $ mail-> SMTPSecure = 'SSL '; // encryption method // $ mail-> SMTPAuth = true; // enable SMTP Authentication // $ mail-> Username = "username "; // username // $ mail-> Password = "password"; // Password $ Mail-> IsMail (); // using PHP mail () function may indicate that this component is not sent by the following user: $ mail-> From = $ sender; // sender $ mail-> FromName = $ sender_name; // sender alias $ mail-> AddReplyTo ($ sender); // Reply to $ mail-> AddAddress ($ sender er ); // recipient // send if ($ ishtml) {$ mail-> IsHTML (true);} // send the attachment if ($ attachments) in html format) {if (is_array ($ attachments) {$ send_attachments = array (); $ tmp_attachments = array_slice ($ attachments, 0, 1); if (! Is_array (array_pop ($ tmp_attachments) {if (isset ($ attachments ['path']) {array_push ($ send_attachments, $ attachments );} else {foreach ($ attachments as $ attachment) {array_push ($ send_attachments, array ('path' => $ attachment) ;}} else {$ send_attachments = $ attachments ;} foreach ($ send_attachments as $ attachment) {$ attachment ['name'] = isset ($ attachment ['name'])? $ Attachment ['name']: null; $ attachment ['encoding'] = isset ($ attachment ['encoding'])? $ Attachment ['encoding']: 'base64'; $ attachment ['type'] = isset ($ attachment ['type'])? $ Attachment ['type']: 'application/octet-stream'; if (isset ($ attachment ['path']) & file_exists ($ attachment ['path']) {$ mail-> AddAttachment ($ attachment ['path'], $ attachment ['name'], $ attachment ['encoding'], $ attachment ['type']) ;}} elseif (is_string ($ attachments) {if (file_exists ($ attachments )) {$ mail-> AddAttachment ($ attachments) ;}}$ mail-> Subject = $ subject; // mail Title $ mail-> Body = $ content; // mail Content return $ mail-> Send ();} // DEMO example: $ author ER = 'author er @ test.com '; $ sender = 'sender @ test.com '; $ sender_name = 'sender name'; $ subject = 'subjecct '; $ content = 'content'; // You Can $ attachments = 'attachment1.jpg' in all four formats '; $ attachments = array ('path' => 'attachment1.jpg ', 'name' => 'attachment 1.jpg'); $ attachments = done'); $ attachments = array ('path '=> 'Attachment1.jpg', 'name' => 'attachment 1.jpg '), array ('path' => 'attachment2.jpg', 'name' => 'attachment 2.jpg '), array ('path' => 'attachment3.jpg ', 'name' => 'attachment 3.jpg'),); $ flag = sendMail ($ sender er, $ sender, $ sender_name, $ subject, $ content, true, $ attachments); echo $ flag;?>

Click here to download the source code.

I hope this article will help you learn PHP programming.

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.