19

emailhtml

emailhtml
SYNOPSIS

emailhtml($text, $html, $to, $subject, $from=false)

DESCRIPTION

emailhtml envoie les messages $text et $html à $to avec le sujet $subject de la part de $from.

$html est un texte riche en HTML. S'il contient des balises <img>, les contenus des fichiers des images sont automatiquement attachés à l'email. Pour des raisons de sécurité, la plupart des afficheurs d'emails cachent une balise <img> avec un attribut src qui pointent sur un autre document. Le contenu d'une image insérée est normalement affiché.

$text est une version en texte simple destinée à un afficheur qui ne peut pas ou n'est pas autorisé à afficher un document HTML.

CODE
  1. require_once 'sendmail.php';
  2. require_once 'filemimetype.php';
  1. function emailhtml($text, $html, $css, $to, $subject, $from=false) {
  2.     global $mailer, $webmaster, $sitename;
  3.  
  4.     if (!$from) {
  5.         $from = $webmaster;
  6.     }
  7.  
  8.     $textheader=$textbody=$htmlheader=$htmlbody=false;
  9.  
  10.     if ($text) {
  11.         $textheader = <<<_SEP_
  12. Content-Type: text/plain; charset=UTF-8
  13. Content-Transfer-Encoding: 8bit
  14. _SEP_;
  15.         $textbody = <<<_SEP_
  16. $text
  17.  
  18. _SEP_;
  19.     }
  20.  
  21.     $related=false;
  22.  
  23.     if ($html) {
  24.         $related=array();
  25.         if (preg_match_all('#<img[^>]+src="([^"]*)"[^>]*>#is', $html, $matches)) {
  26.             $pattern=array();
  27.             $replacement=array();
  28.             foreach ($matches[1] as $url) {
  29.                 if ($url[0] != '/')
  30.                     continue;
  31.                 if (array_key_exists($url, $related))
  32.                     continue;
  33.                 $fname=ROOT_DIR . $url;
  34.                 $filetype=file_mime_type($fname, false);
  35.                 if (!$filetype or strpos($filetype, 'image') !== 0)
  36.                     continue;
  37.                 $data=@file_get_contents($fname);
  38.                 if (!$data)
  39.                     continue;
  40.                 $base64=chunk_split(base64_encode($data));
  41.                 $cid=md5(uniqid('cid', true));
  42.                 $qfname=preg_quote($url);
  43.                 $pattern[]='#(<img[^>]+src=)"' . $qfname . '"([^>]*>)#is';
  44.                 $replacement[]='${1}"cid:' . $cid . '"${2}';
  45.                 $related[$url]=array(basename($fname), $filetype, $cid, $base64);
  46.             }
  47.  
  48.             $html=preg_replace($pattern, $replacement, $html);
  49.         }
  50.  
  51.         $title=htmlspecialchars($sitename, ENT_COMPAT, 'UTF-8');
  52.  
  53.         $htmlheader = <<<_SEP_
  54. Content-Type: text/html; charset=UTF-8
  55. Content-Transfer-Encoding: 8bit
  56. _SEP_;
  57.         $htmlbody = <<<_SEP_
  58. <html>
  59. <head>
  60. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  61. <title>$title</title>
  62. <style type="text/css">
  63. $css
  64. </style>
  65. </head>
  66. <body>
  67. $html
  68. </body>
  69. </html>
  70.  
  71. _SEP_;
  72.     }
  73.  
  74.     $headers = <<<_SEP_
  75. From: $from
  76. MIME-Version: 1.0
  77. X-Mailer: $mailer
  78.  
  79. _SEP_;
  80.  
  81.     $body='';
  82.  
  83.     if ($related) {
  84.         if ($textbody) {
  85.             $sep=md5(uniqid('sep', true));
  86.  
  87.             $body .= <<<_SEP_
  88. Content-Type: multipart/alternative; boundary="$sep"
  89.  
  90. --$sep
  91. $textheader
  92.  
  93. $textbody
  94. --$sep
  95. $htmlheader
  96.  
  97. $htmlbody
  98. --$sep--
  99.  
  100.  
  101. _SEP_;
  102.         }
  103.         else {
  104.             $body .= <<<_SEP_
  105. $htmlheader
  106.  
  107. $htmlbody
  108.  
  109. _SEP_;
  110.         }
  111.  
  112.         $sep=md5(uniqid('sep', true));
  113.  
  114.         $headers .= <<<_SEP_
  115. Content-Type: multipart/related; boundary="$sep"
  116. _SEP_;
  117.  
  118.         foreach ($related as $url => $r) {
  119.             list($filename, $filetype, $cid, $base64)=$r;
  120.             $body .= <<<_SEP_
  121. --$sep
  122. Content-Type: $filetype
  123. Content-Transfer-Encoding: base64
  124. Content-Disposition: inline; filename="$filename"
  125. Content-ID: <$cid>
  126.  
  127. $base64
  128.  
  129. _SEP_;
  130.         }
  131.  
  132.         $body = <<<_SEP_
  133. --$sep
  134. $body
  135. --$sep--
  136. _SEP_;
  137.     }
  138.     else if ($textbody and $htmlbody) {
  139.         $sep=md5(uniqid('sep', true));
  140.  
  141.         $headers .= <<<_SEP_
  142. Content-Type: multipart/alternative; boundary="$sep"
  143. _SEP_;
  144.         $body .= <<<_SEP_
  145. --$sep
  146. $textheader
  147.  
  148. $textbody
  149. --$sep
  150. $htmlheader
  151.  
  152. $htmlbody
  153. --$sep--
  154. _SEP_;
  155.     }
  156.     else if ($textbody) {
  157.         $headers .= $textheader;
  158.         $body=$textbody;
  159.     }
  160.     else if ($htmlbody) {
  161.         $headers .= $htmlheader;
  162.         $body=$htmlbody;
  163.     }
  164.  
  165.     return sendmail($to, $subject, $body, $headers, $from);
  166. }
VOIR AUSSI

filemimetype, emailcrypto, emailme, emailtext, sendmail

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].