4

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 (get_magic_quotes_runtime()) {
  36.                     $data = stripslashes($data);
  37.                 }
  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 = 'Content-Type: text/html; charset=utf-8';
  54.         $htmlbody = <<<_SEP_
  55. <html>
  56. <head>
  57. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  58. <title>$title</title>
  59. <style type="text/css">
  60. $css
  61. </style>
  62. </head>
  63. <body>
  64. $html
  65. </body>
  66. </html>
  67.  
  68. _SEP_;
  69.     }
  70.  
  71.     $headers = <<<_SEP_
  72. From: $sender
  73. Return-Path: $sender
  74. X-Mailer: $mailer
  75.  
  76. _SEP_;
  77.  
  78.     $body='';
  79.  
  80.     if ($related) {
  81.         if ($textbody) {
  82.             $sep=md5(uniqid('sep', true));
  83.  
  84.             $body .= <<<_SEP_
  85. Content-Type: multipart/alternative; boundary="$sep"
  86.  
  87. --$sep
  88. $textheader
  89.  
  90. $textbody
  91. --$sep
  92. $htmlheader
  93.  
  94. $htmlbody
  95. --$sep--
  96.  
  97.  
  98. _SEP_;
  99.         }
  100.         else {
  101.             $body .= <<<_SEP_
  102. $htmlheader
  103.  
  104. $htmlbody
  105.  
  106. _SEP_;
  107.         }
  108.  
  109.         $sep=md5(uniqid('sep', true));
  110.  
  111.         $headers .= <<<_SEP_
  112. Content-Type: multipart/related; boundary="$sep"
  113. _SEP_;
  114.  
  115.         foreach ($related as $url => $r) {
  116.             list($filename, $filetype, $cid, $base64)=$r;
  117.             $body .= <<<_SEP_
  118. --$sep
  119. Content-Type: $filetype
  120. Content-Transfer-Encoding: base64
  121. Content-Disposition: inline; filename="$filename"
  122. Content-ID: <$cid>
  123.  
  124. $base64
  125.  
  126. _SEP_;
  127.         }
  128.  
  129.         $body = <<<_SEP_
  130. --$sep
  131. $body
  132. --$sep--
  133. _SEP_;
  134.     }
  135.     else if ($textbody and $htmlbody) {
  136.         $sep=md5(uniqid('sep', true));
  137.  
  138.         $headers .= <<<_SEP_
  139. Content-Type: multipart/alternative; boundary="$sep"
  140. _SEP_;
  141.         $body .= <<<_SEP_
  142. --$sep
  143. $textheader
  144.  
  145. $textbody
  146. --$sep
  147. $htmlheader
  148.  
  149. $htmlbody
  150. --$sep--
  151. _SEP_;
  152.     }
  153.     else if ($textbody) {
  154.         $headers .= $textheader;
  155.         $body=$textbody;
  156.     }
  157.     else if ($htmlbody) {
  158.         $headers .= $htmlheader;
  159.         $body=$htmlbody;
  160.     }
  161.  
  162.     return @mail($to, $subject, $body, $headers);
  163. }
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].