/* 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. * @ Param String $ subject * @ Param String $ 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 "class-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"; // User name // $ Mail-> Password = "password"; // Password $ Mail-> IsMail (); // The using PHP mail () function may prompt that this email may not be sent by the following users. $ Mail-> From = $ sender; // sender $ Mail-> FromName = $ sender_name; // sender alias $ Mail-> AddReplyTo ($ sender); // reply $ Mail-> AddAddress ($ email er); // recipient // Send in html format If ($ ishtml ){ $ Mail-> IsHTML (true ); } // Send the attachment If ($ attachments ){ 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; // email content Return $ mail-> Send (); } // DEMO example: $ Author ER = 'author er @ test.com '; $ Sender = 'sender @ test.com '; $ Sender_name = 'sender name '; $ Subject = 'subjecct '; $ Content = 'content '; // All four formats are supported $ Attachments = 'attachment1.jpg '; $ Attachments = array ('path' => 'attachment1.jpg ', 'name' => 'attachment 1.jpg '); $ Attachments = array('attachment1.jpg ', 'attachment2.jpg '); $ Attachments = array ( 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; ?> |