30

emailhtml

emailhtml
SYNOPSIS

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

DESCRIPTION

emailhtml sends $text and $html to $to with the subject $subject from $from.

$html is a rich text in HTML. If it contains <img> tags, the content of the image files are automatically attached to the email. For security reasons, most email viewers hide an <img> tag with an src attribute which points to another document. An embedded image content will normally be displayed.

$text is a plain text version provided for a viewer which is not able or not allowed to display an HTML document.

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. }
SEE ALSO

filemimetype, emailcrypto, emailme, emailtext, sendmail

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[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]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].