note 113161 added to function.mail

From: Date: Fri, 06 Sep 2013 15:01:07 +0000
Subject: note 113161 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-196186@lists.php.net to get a copy of this message
Hello, it's sometime hard to include multiple attachment, or to include pictures inside body. Please find these 2 functions allowing sending email with attachment. usage : <?php echo date("H:i:s"); echo mail::sendMail("to@domain.com", "Test Attach ". date("H:i:s"), "Contenu du mail <a href=3D'domain.com'>domain.com</a>", __FILE__, "xx@domain.com",'' , true); ?> source : <?php class mail { public static function prepareAttachment($path) { $rn = "\r\n"; if (file_exists($path)) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $ftype = finfo_file($finfo, $path); $file = fopen($path, "r"); $attachment = fread($file, filesize($path)); $attachment = chunk_split(base64_encode($attachment)); fclose($file); $msg = 'Content-Type: \'' . $ftype . '\'; name="' . basename($path) . '"' . $rn; $msg .= "Content-Transfer-Encoding: base64" . $rn; $msg .= 'Content-ID: <' . basename($path) . '>' . $rn; // $msg .= 'X-Attachment-Id: ebf7a33f5a2ffca7_0.1' . $rn; $msg .= $rn . $attachment . $rn . $rn; return $msg; } else { return false; } } public static function sendMail($to, $subject, $content, $path = '', $cc = '', $bcc = '', $_headers = false) { $rn = "\r\n"; $boundary = md5(rand()); $boundary_content = md5(rand()); // Headers $headers = 'From: Mail System PHP <no-reply@domain.com>' . $rn; $headers .= 'Mime-Version: 1.0' . $rn; $headers .= 'Content-Type: multipart/related;boundary=' . $boundary . $rn; //adresses cc and ci if ($cc != '') { $headers .= 'Cc: ' . $cc . $rn; } if ($bcc != '') { $headers .= 'Bcc: ' . $cc . $rn; } $headers .= $rn; // Message Body $msg = $rn . '--' . $boundary . $rn; $msg.= "Content-Type: multipart/alternative;" . $rn; $msg.= " boundary=\"$boundary_content\"" . $rn; //Body Mode text $msg.= $rn . "--" . $boundary_content . $rn; $msg .= 'Content-Type: text/plain; charset=ISO-8859-1' . $rn; $msg .= strip_tags($content) . $rn; //Body Mode Html $msg.= $rn . "--" . $boundary_content . $rn; $msg .= 'Content-Type: text/html; charset=ISO-8859-1' . $rn; $msg .= 'Content-Transfer-Encoding: quoted-printable' . $rn; if ($_headers) { $msg .= $rn . '<img src=3D"cid:template-H.PNG" />' . $rn; } //equal sign are email special characters. =3D is the = sign $msg .= $rn . '<div>' . nl2br(str_replace("=", "=3D", $content)) . '</div>' . $rn; if ($_headers) { $msg .= $rn . '<img src=3D"cid:template-F.PNG" />' . $rn; } $msg .= $rn . '--' . $boundary_content . '--' . $rn; //if attachement if ($path != '' && file_exists($path)) { $conAttached = self::prepareAttachment($path); if ($conAttached !== false) { $msg .= $rn . '--' . $boundary . $rn; $msg .= $conAttached; } } //other attachement : here used on HTML body for picture headers/footers if ($_headers) { $imgHead = dirname(__FILE__) . '/../../../../modules/notification/ressources/img/template-H.PNG'; $conAttached = self::prepareAttachment($imgHead); if ($conAttached !== false) { $msg .= $rn . '--' . $boundary . $rn; $msg .= $conAttached; } $imgFoot = dirname(__FILE__) . '/../../../../modules/notification/ressources/img/template-F.PNG'; $conAttached = self::prepareAttachment($imgFoot); if ($conAttached !== false) { $msg .= $rn . '--' . $boundary . $rn; $msg .= $conAttached; } } // Fin $msg .= $rn . '--' . $boundary . '--' . $rn; // Function mail() mail($to, $subject, $msg, $headers); } } ?> ---- Server IP: 195.221.21.36 Probable Submitter: 194.51.110.132 ---- Manual Page -- http://php.net/manual/en/function.mail.php Edit -- https://master.php.net/note/edit/113161 Del: integrated -- https://master.php.net/note/delete/113161/integrated Del: useless -- https://master.php.net/note/delete/113161/useless Del: bad code -- https://master.php.net/note/delete/113161/bad+code Del: spam -- https://master.php.net/note/delete/113161/spam Del: non-english -- https://master.php.net/note/delete/113161/non-english Del: in docs -- https://master.php.net/note/delete/113161/in+docs Del: other reasons-- https://master.php.net/note/delete/113161 Reject -- https://master.php.net/note/reject/113161 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196186) next »