PHP mail sending class PHPMailer usage example details, mail sent phpmailer. PHP mail sending class PHPMailer usage example details, Mail sending phpmailer this article describes the PHP mail class PHPMailer usage, and describes the specific operation steps. Details about PHPMailer usage examples for sending mails to PHP, and phpmailer for sending mails
The example in this article describes PHPMailer usage of PHP mail sending class and detailed the Operation steps. 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.
For php System mail sending, I use phpmailer class to write, according to the method described on the Internet to achieve, but encountered problems in the operation
The error message is returned to you. you can handle the problem based on the feedback.
1. cocould not authenticate. it means that you cannot pass authentication. maybe you have not enabled SMTP for email.
2. cannot connect SMTP host indicates that the SMTP server cannot be connected.
In fact, you don't have to pay attention to anything. you mainly configure SMTP information correctly and enable SMTP connections in your mailbox, so there will be no errors. Phpmailer provides demonstration cases. you can check the configuration and usage
Php failed to send emails using phpmailer
The server does not have SMTP
Examples in this article describes the PHP mail sending class PHPMailer usage, and describes the specific operation steps in detail. Share with me...