[dokuwiki] mail_send_html() - simple interface for sending html messages

  • From: Mike Frysinger <vapier@xxxxxxxxxx>
  • To: DokuWiki Mailinglist <dokuwiki@xxxxxxxxxxxxx>
  • Date: Mon, 9 Mar 2009 02:47:19 -0500

this patch uses ideas from this page:
http://www.dokuwiki.org/tips:betteremailnotifications

the idea though is to generalize and merge with mainline.  ive added a new 
function mail_send_html() that works just like mail_send() except that it has 
an additional argument -- the html version of the message.  all the other 
pieces are unified in _mail_send_action().

ive tested this with the last dokuwiki release, but the changes apply cleanly 
to latest darcs repo (afaict)
-mike
diff -rN -u old-dokuwiki/inc/mail.php new-dokuwiki/inc/mail.php
--- old-dokuwiki/inc/mail.php   2009-03-09 03:43:42.386731536 -0400
+++ new-dokuwiki/inc/mail.php   2009-03-09 03:43:42.958727601 -0400
@@ -58,12 +58,19 @@
   return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action');
 }
 
+function mail_send_html($to, $subject, $body, $htmlbody, $from='', $cc='', 
$bcc='', $headers=null, $params=null){
+
+  $message = 
compact('to','subject','body','htmlbody','from','cc','bcc','headers','params');
+  return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action');
+}
+
 function _mail_send_action($data) {
 
   // retrieve parameters from event data, $to, $subject, $body, $from, $cc, 
$bcc, $headers, $params
   $to = $data['to'];
   $subject = $data['subject'];
   $body = $data['body'];
+  $htmlbody = isset($data['htmlbody']) ? $data['htmlbody'] : null;
 
   // add robustness in case plugin removes any of these optional values
   $from = isset($data['from']) ? $data['from'] : '';
@@ -95,17 +102,36 @@
   $header .= mail_encode_address($cc,'Cc');
   $header .= mail_encode_address($bcc,'Bcc');
   $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
-  $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
-  $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
+  if ($htmlbody != null) {
+    $mime_boundary = 'dokuwiki-' . md5(date('U') . rand());
+    $header .= 'Content-Type: multipart/alternative; 
boundary='.$mime_boundary.MAILHEADER_EOL;
+  } else {
+    $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
+    $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
+  }
   $header .= $headers;
   $header  = trim($header);
 
   $body = mail_quotedprintable_encode($body);
+  if ($htmlbody != null) {
+    $htmlbody = mail_quotedprintable_encode($htmlbody);
+    $message = '--'.$mime_boundary.MAILHEADER_EOL;
+    $message .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
+    $message .= 'Content-Transfer-Encoding: 
quoted-printable'.MAILHEADER_EOL.MAILHEADER_EOL;
+    $message .= $body.MAILHEADER_EOL.MAILHEADER_EOL;
+    $message .= '--'.$mime_boundary.MAILHEADER_EOL;
+    $message .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL;
+    $message .= 'Content-Transfer-Encoding: 
quoted-printable'.MAILHEADER_EOL.MAILHEADER_EOL;
+    $message .= $htmlbody.MAILHEADER_EOL.MAILHEADER_EOL;
+    $message .= '--'.$mime_boundary.'--'.MAILHEADER_EOL;
+  } else {
+    $message = $body;
+  }
 
   if($params == null){
-    return @mail($to,$subject,$body,$header);
+    return @mail($to,$subject,$message,$header);
   }else{
-    return @mail($to,$subject,$body,$header,$params);
+    return @mail($to,$subject,$message,$header,$params);
   }
 }
 

Other related posts:

  • » [dokuwiki] mail_send_html() - simple interface for sending html messages - Mike Frysinger