From f08086ec453c4529ae7361f9540b430ee61238f0 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sun, 13 Nov 2011 12:37:45 +0100
Subject: [PATCH] Added HTML wrapper for mails

The mailwrap.html adds the standard headers and footers to all HTML
mails. The signature present in the text body will be removed before
inserting in the wrapper, allowing a nicer footer for HTML.

Users can overwrite the file with their own to create HTML mails in
corporate design. However, a way to automatically embed referenced
images is missing currently.
---
 inc/Mailer.class.php      | 15 +++++++++++----
 inc/lang/en/mailwrap.html | 13 +++++++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)
 create mode 100644 inc/lang/en/mailwrap.html

diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php
index 09e457243..cdd4b266a 100644
--- a/inc/Mailer.class.php
+++ b/inc/Mailer.class.php
@@ -130,13 +130,13 @@ class Mailer {
      * If you pass the HTML part or HTML replacements yourself you have to make
      * sure you encode all HTML special chars correctly
      *
-     * @fixme the HTML head and body still needs to be set
      * @param string $text     plain text body
      * @param array  $textrep  replacements to apply on the text part
      * @param array  $htmlrep  replacements to apply on the HTML part, leave null to use $textrep
      * @param array  $html     the HTML body, leave null to create it from $text
+     * @param bool   $wrap     wrap the HTML in the default header/Footer
      */
-    public function setBody($text, $textrep=null, $htmlrep=null, $html=null){
+    public function setBody($text, $textrep=null, $htmlrep=null, $html=null, $wrap=true){
         global $INFO;
         global $conf;
         $htmlrep = (array) $htmlrep;
@@ -147,6 +147,12 @@ class Mailer {
             $html = hsc($text);
             $html = nl2br($text);
         }
+        if($wrap){
+            $wrap = rawLocale('mailwrap','html');
+            $html = preg_replace('/\n-- \n.*$/m','',$html); //strip signature
+            $html = str_replace('@HTMLBODY@',$html,$wrap);
+        }
+
         // copy over all replacements missing for HTML (autolink URLs)
         foreach($textrep as $key => $value){
             if(isset($htmlrep[$key])) continue;
@@ -159,11 +165,12 @@ class Mailer {
 
         // prepare default replacements
         $ip   = clientIP();
+        $cip  = gethostsbyaddrs($ip);
         $trep = array(
             'DATE'        => dformat(),
             'BROWSER'     => $_SERVER['HTTP_USER_AGENT'],
             'IPADDRESS'   => $ip,
-            'HOSTNAME'    => gethostsbyaddrs($ip),
+            'HOSTNAME'    => $cip,
             'TITLE'       => $conf['title'],
             'DOKUWIKIURL' => DOKU_URL,
             'USER'        => $_SERVER['REMOTE_USER'],
@@ -175,7 +182,7 @@ class Mailer {
             'DATE'        => '<i>'.hsc(dformat()).'</i>',
             'BROWSER'     => hsc($_SERVER['HTTP_USER_AGENT']),
             'IPADDRESS'   => '<code>'.hsc($ip).'</code>',
-            'HOSTNAME'    => '<code>'.hsc(gethostsbyaddrs($ip)).'</code>',
+            'HOSTNAME'    => '<code>'.hsc($cip).'</code>',
             'TITLE'       => hsc($conf['title']),
             'DOKUWIKIURL' => '<a href="'.DOKU_URL.'">'.DOKU_URL.'</a>',
             'USER'        => hsc($_SERVER['REMOTE_USER']),
diff --git a/inc/lang/en/mailwrap.html b/inc/lang/en/mailwrap.html
new file mode 100644
index 000000000..d67644c95
--- /dev/null
+++ b/inc/lang/en/mailwrap.html
@@ -0,0 +1,13 @@
+<html>
+<head>
+    <title>@TITLE@</title>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+</head>
+<body>
+
+@HTMLBODY@
+
+<br /><br /><hr />
+<small>This mail was generated by DokuWiki at @DOKUWIKIURL@.</small>
+</body>
+</html>
-- 
GitLab