tips:betteremailnotifications
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tips:betteremailnotifications [2008-09-09 22:45] – 80.98.128.134 | tips:betteremailnotifications [2011-12-10 00:25] (current) – Rincewind updates 74.93.109.180 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Better Email Notifications with HTML formatting ====== | ||
+ | This hack was in part inspired by the beautiful page change notifications sent out by other wiki software such as PBWiki, which uses HTML formatting instead of plain-text, and in part inspired by the existing diff rendering system in DokuWiki. I figured if DokuWiki is already capable of generating nicely formatted diff of page changes, I could tap into the system to send out HTML-formatted page notifications. | ||
+ | |||
+ | ===== UPDATEs: ===== | ||
+ | * Instructions updated for DokuWiki 2008-04-11 RC2. | ||
+ | * edited hack for notification on new pages | ||
+ | * tested and working on DokuWiki 2008-05-05 | ||
+ | * tested and working on DokuWiki 2009-12-25c | ||
+ | * tested and working on DokuWiki 2011-05-25a | ||
+ | |||
+ | ===== Screenshots of before and after ===== | ||
+ | |||
+ | |||
+ | {{http:// | ||
+ | |||
+ | //please host this last image on your own website... i can't guarantee the [[http:// | ||
+ | |||
+ | |||
+ | ===== What you need ====== | ||
+ | |||
+ | * Plain text editor((I used TextWrangler on the Mac. You should probably use Wordpad if you're on Windows because Notepad doesn' | ||
+ | * [[http:// | ||
+ | * DokuWiki files you'll be editing: | ||
+ | * inc/ | ||
+ | * inc/ | ||
+ | |||
+ | ===== Applying the hack ===== | ||
+ | |||
+ | ==== inc/ | ||
+ | |||
+ | First, we need to create a new function that can handle our HTML e-mails. We'll do so by creating one based on mail_send() in mail.php. Open up the file and locate the mail_send() function | ||
+ | |||
+ | <code php> | ||
+ | function mail_send($to, | ||
+ | </ | ||
+ | |||
+ | at line 36((Line 67 of Rincewind)) of an unmodified mail.php. Insert the following code just **above** the mail_send() function. | ||
+ | |||
+ | <code php> | ||
+ | / | ||
+ | * HTML Mail functions | ||
+ | * | ||
+ | * Sends HTML-formatted mail | ||
+ | * By Lin Junjie (mail [dot] junjie [at] gmail [dot] com) | ||
+ | * | ||
+ | | ||
+ | function mail_send_html($to, | ||
+ | if(defined(' | ||
+ | $subject = utf8_deaccent($subject); | ||
+ | $subject = utf8_strip($subject); | ||
+ | } | ||
+ | |||
+ | if(!utf8_isASCII($subject)) { | ||
+ | $subject = ' | ||
+ | // Spaces must be encoded according to rfc2047. Use the " | ||
+ | $subject = preg_replace('/ | ||
+ | } | ||
+ | |||
+ | $header | ||
+ | |||
+ | $usenames = (strtoupper(substr(PHP_OS, | ||
+ | |||
+ | $random_hash = md5(date(' | ||
+ | |||
+ | $to = mail_encode_address($to,'', | ||
+ | $header .= mail_encode_address($from,' | ||
+ | $header .= mail_encode_address($cc,' | ||
+ | $header .= mail_encode_address($bcc,' | ||
+ | $header .= ' | ||
+ | $header .= " | ||
+ | $header .= $headers; | ||
+ | $header | ||
+ | |||
+ | $body = mail_quotedprintable_encode($body); | ||
+ | $bodyhtml = mail_quotedprintable_encode($bodyhtml); | ||
+ | | ||
+ | $message = " | ||
+ | " | ||
+ | " | ||
+ | $body." | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | $bodyhtml." | ||
+ | " | ||
+ | |||
+ | if($params == null){ | ||
+ | return @mail($to, | ||
+ | }else{ | ||
+ | return @mail($to, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The function has the added advantage of sending along the standard plain-text version for mail clients that cannot display HTML formatting. That's all for mail.php. | ||
+ | |||
+ | ==== inc/ | ||
+ | |||
+ | There are three parts of hack to apply to common.php. | ||
+ | |||
+ | **Part 1** | ||
+ | |||
+ | First, we need to intercept the mailing action and generate a HTML-formatted version of the notification. Locate the following code | ||
+ | |||
+ | <code php> | ||
+ | $diff = $dformat-> | ||
+ | </ | ||
+ | |||
+ | in the notify() function at line 877((1148 Rincewind)) of an unmodified common.php and insert the following code right **after** it: | ||
+ | |||
+ | <code php> | ||
+ | / | ||
+ | * Generate HTML Notification Hack (Part 1) | ||
+ | * | ||
+ | * inc/ | ||
+ | * By Lin Junjie (mail [dot] junjie [at] gmail [dot] com) | ||
+ | * | ||
+ | | ||
+ | | ||
+ | $df = new Diff(explode(" | ||
+ | explode(" | ||
+ | $left = '< | ||
+ | $id.' | ||
+ | $right = '< | ||
+ | $id.' | ||
+ | $lang[' | ||
+ | |||
+ | $tdf = new TableDiffFormatter(); | ||
+ | |||
+ | $diffHTML = "< | ||
+ | "</ | ||
+ | $_SERVER[' | ||
+ | " | ||
+ | " | ||
+ | $left." | ||
+ | " | ||
+ | "</ | ||
+ | " | ||
+ | "</ | ||
+ | |||
+ | $diffHTML = str_replace(' | ||
+ | $diffHTML = str_replace(' | ||
+ | $diffHTML = str_replace(' | ||
+ | $diffHTML = str_replace(' | ||
+ | $diffHTML = str_replace(' | ||
+ | $diffHTML = str_replace('< | ||
+ | $diffHTML = str_replace('</ | ||
+ | $diffHTML = str_replace('< | ||
+ | |||
+ | / | ||
+ | * End Generate HTML Notification Hack (Part 1) | ||
+ | | ||
+ | </ | ||
+ | |||
+ | **Part 2** | ||
+ | |||
+ | Then intercept the mailing action and generate a HTML-formatted version of the notification on new page creation. Locate the following code | ||
+ | |||
+ | <code php> | ||
+ | $diff = rawWiki($id); | ||
+ | </ | ||
+ | |||
+ | in the notify() function of inc/ | ||
+ | |||
+ | <code php> | ||
+ | / | ||
+ | * Generate HTML Notification Hack (Part 2) | ||
+ | * | ||
+ | * inc/ | ||
+ | * By Berteh (berteh [at] hotmail [dot] com) | ||
+ | * | ||
+ | | ||
+ | $diffHTML = "< | ||
+ | "</ | ||
+ | $_SERVER[' | ||
+ | " | ||
+ | " | ||
+ | "</ | ||
+ | / | ||
+ | * End Generate HTML Notification Hack (Part 2) | ||
+ | | ||
+ | </ | ||
+ | |||
+ | **Part 3** | ||
+ | |||
+ | Next, we will need to call our mail_send_html() function to send out the HTML-formatted email that we have generated. Locate the following code | ||
+ | |||
+ | <code php> | ||
+ | mail_send($to, | ||
+ | </ | ||
+ | |||
+ | right before the notify() function ends. **REPLACE** this line of code with the following code: | ||
+ | |||
+ | <code php> | ||
+ | / | ||
+ | * Generate HTML Notification Hack (Part 3) | ||
+ | * | ||
+ | * inc/ | ||
+ | * By Lin Junjie (mail [dot] junjie [at] gmail [dot] com) | ||
+ | * | ||
+ | | ||
+ | if ($diffHTML) { | ||
+ | mail_send_html($to, | ||
+ | }else { | ||
+ | mail_send($to, | ||
+ | } | ||
+ | / | ||
+ | * End Generate HTML Notification Hack (Part 3) | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Note that part 3 of the hack has the added functionality of changing the sender of the mail to the name of the user that made the change.((Adapted from http:// | ||
+ | |||
+ | If you want to **show the mail of the user** that made the change as mail sender rather than a no-reply address use | ||
+ | <code php> | ||
+ | $sendermail = $INFO[' | ||
+ | if ($diffHTML) { | ||
+ | mail_send_html($to, | ||
+ | }else { | ||
+ | mail_send($to, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== That's all folks ==== | ||
+ | |||
+ | That's it! We're done! Leave a message here or contact me at mail [dot] junjie [at] gmail [dot] com. If you have any way of integrating this into a plugin, please do so and let us know here! | ||
+ | |||
+ | ===== history ===== | ||
+ | |||
+ | * 2008.10.23: update to handle notifications on new pages ---[[berteh@hotmail.com|berteh]] | ||
+ | * 2008.10.23: update to get absolute links in notifications titles, using '' | ||
+ | |||
+ | ===== Questions and problems ===== | ||
+ | * The HTML version of the e-mail works fine in my web mail, but MS Outlook shows the plain text version. | ||
+ | * I tried this hack on version 2011-05-25a, |