6

emailhtml

emailhtml
SYNOPSIS

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

DESCRIPTION

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

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

filemimetype, emailcrypto, emailme, emailtext

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].