From bb01c27c6c2c8e83091764cebadeef0489985b4b Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Wed, 2 Nov 2011 08:59:18 +0100 Subject: Added Mailer class --- inc/Mailer.class.php | 324 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 324 insertions(+) create mode 100644 inc/Mailer.class.php diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php new file mode 100644 index 000000000..991ec6c3e --- /dev/null +++ b/inc/Mailer.class.php @@ -0,0 +1,324 @@ + + */ + + +// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?) +// think different +if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n"); +#define('MAILHEADER_ASCIIONLY',1); + + +class Mailer { + + private $headers = array(); + private $attach = array(); + private $html = ''; + private $text = ''; + + private $boundary = ''; + private $partid = ''; + private $sendparam= ''; + + function __construct(){ + $this->partid = md5(uniqid(rand(),true)).'@'.$_SERVER['SERVER_NAME']; + $this->boundary = '----------'.md5(uniqid(rand(),true)); + } + + /** + * Attach a file + * + * @param $path Path to the file to attach + * @param $mime Mimetype of the attached file + * @param $name The filename to use + * @param $embed Unique key to reference this file from the HTML part + */ + public function attachFile($path,$mime,$name='',$embed=''){ + if(!$name){ + $name = basename($path); + } + + $this->attach[] = array( + 'data' => file_get_contents($path), + 'mime' => $mime, + 'name' => $name, + 'embed' => $embed + ); + } + + /** + * Attach a file + * + * @param $path The file contents to attach + * @param $mime Mimetype of the attached file + * @param $name The filename to use + * @param $embed Unique key to reference this file from the HTML part + */ + public function attachContent($data,$mime,$name='',$embed=''){ + if(!$name){ + list($junk,$ext) = split('/',$mime); + $name = count($this->attach).".$ext"; + } + + $this->attach[] = array( + 'data' => $data, + 'mime' => $mime, + 'name' => $name, + 'embed' => $embed + ); + } + + /** + * Set the HTML part of the mail + * + * Placeholders can be used to reference embedded attachments + */ + public function setHTMLBody($html){ + $this->html = $html; + } + + /** + * Set the plain text part of the mail + */ + public function setTextBody($text){ + $this->text = $text; + } + + /** + * Ses an email address header with correct encoding + * + * Unicode characters will be deaccented and encoded base64 + * for headers. Addresses may not contain Non-ASCII data! + * + * Example: + * setAddress("föö , me@somewhere.com","TBcc"); + * + * @param string $address Multiple adresses separated by commas + * @param string $header Name of the header (To,Bcc,Cc,...) + */ + function mail_encode_address($address,$header){ + // No named recipients for To: in Windows (see FS#652) + $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; + + $headers = ''; + $parts = explode(',',$address); + foreach ($parts as $part){ + $part = trim($part); + + // parse address + if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ + $text = trim($matches[1]); + $addr = $matches[2]; + }else{ + $addr = $part; + } + // skip empty ones + if(empty($addr)){ + continue; + } + + // FIXME: is there a way to encode the localpart of a emailaddress? + if(!utf8_isASCII($addr)){ + msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); + continue; + } + + if(!mail_isvalid($addr)){ + msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); + continue; + } + + // text was given + if(!empty($text) && $names){ + // add address quotes + $addr = "<$addr>"; + + if(defined('MAILHEADER_ASCIIONLY')){ + $text = utf8_deaccent($text); + $text = utf8_strip($text); + } + + if(!utf8_isASCII($text)){ + //FIXME + // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" + /* + if (preg_match('/^"(.+)"$/', $text, $matches)) { + $text = '"=?UTF-8?Q?'.mail_quotedprintable_encode($matches[1], 0).'?="'; + } else { + $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text, 0).'?='; + } + */ + $text = '=?UTF-8?B?'.base64_encode($text).'?='; + } + }else{ + $text = ''; + } + + // add to header comma seperated + if($headers != ''){ + $headers .= ','; + $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers + } + $headers .= $text.' '.$addr; + } + + if(empty($headers)) return false; + + $this->headers[$header] = $headers; + return $headers; + } + + /** + * Add the To: recipients + * + * @see setAddress + * @param string $address Multiple adresses separated by commas + */ + public function to($address){ + $this->setAddress($address, 'To'); + } + + /** + * Add the Cc: recipients + * + * @see setAddress + * @param string $address Multiple adresses separated by commas + */ + public function cc($address){ + $this->setAddress($address, 'Cc'); + } + + /** + * Add the Bcc: recipients + * + * @see setAddress + * @param string $address Multiple adresses separated by commas + */ + public function bcc($address){ + $this->setAddress($address, 'Bcc'); + } + + /** + * Add the mail's Subject: header + * + * @param string $subject the mail subject + */ + public function subject($subject){ + if(!utf8_isASCII($subject)){ + $subject = '=?UTF-8?B?'.base64_encode($subject).'?='; + } + $this->headers['Subject'] = $subject; + } + + /** + * Prepare the mime multiparts for all attachments + * + * Replaces placeholders in the HTML with the correct CIDs + */ + protected function prepareAttachments(){ + $mime = ''; + $part = 1; + // embedded attachments + foreach($this->attach as $media){ + // create content id + $cid = 'part'.$part.'.'.$this->partid; + + // replace wildcards + if($media['embed']){ + $this->html = str_replace('%%'.$media['embed'].'%%','cid:'.$cid,$this->html); + } + + $mime .= '--'.$this->boundary.MAILHEADER_EOL; + $mime .= 'Content-Type: '.$media['mime'].';'.MAILHEADER_EOL; + $mime .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; + $mime .= "Content-ID: <$cid>".MAILHEADER_EOL; + if($media['embed']){ + $mime .= 'Content-Disposition: inline; filename="'.$media['name'].'"'.MAILHEADER_EOL; + }else{ + $mime .= 'Content-Disposition: attachment; filename="'.$media['name'].'"'.MAILHEADER_EOL; + } + $mime .= MAILHEADER_EOL; //end of headers + $mime .= chunk_split(base64_encode($media['data']),74,MAILHEADER_EOL); + + $part++; + } + return $mime; + } + + protected function createBody(){ + // check for body + if(!$this->text && !$this->html){ + return false; + } + + // add general headers + $this->headers['MIME-Version'] = '1.0'; + + if(!$this->html && !count($this->attach)){ // we can send a simple single part message + $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; + $this->headers['Content-Transfer-Encoding'] = 'base64'; + $body = chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); + }else{ // multi part it is + + // prepare the attachments + $attachments = $this->prepareAttachments(); + + // do we have alternative text content? + if($this->text && $this->html){ + $this->headers['Content-Type'] = 'multipart/alternative; boundary="'.$this->boundary.'XX"'; + $body = "This is a multi-part message in MIME format.".MAILHEADER_EOL; + $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; + $body .= MAILHEADER_EOL; + $body .= 'Content-Type: text/plain; charset=UTF-8'; + $body .= 'Content-Transfer-Encoding: base64'; + $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); + $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; + $body .= 'Content-Type: multipart/related; boundary="'.$this->boundary.'"'.MAILHEADER_EOL; + $body .= MAILHEADER_EOL; + }else{ + $this->headers['Content-Type'] = 'multipart/related; boundary="'.$this->boundary.'"'; + $body = "This is a multi-part message in MIME format.".MAILHEADER_EOL; + } + + $body .= '--'.$this->boundary."\n"; + $body .= "Content-Type: text/html; charset=UTF-8\n"; + $body .= "Content-Transfer-Encoding: base64\n"; + $body .= MAILHEADER_EOL; + $body = chunk_split(base64_encode($this->html),74,MAILHEADER_EOL); + $body .= MAILHEADER_EOL; + $body .= $attachments; + $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; + + // close open multipart/alternative boundary + if($this->text && $this->html){ + $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; + } + } + + return $body; + } + + /** + * Create a string from the headers array + */ + protected function prepareHeaders(){ + $headers = ''; + foreach($this->headers as $key => $val){ + $headers .= "$key: $val".MAILHEADER_EOL; + } + return $headers; + } + + /** + * return a full email with all headers + * + * This is mainly for debugging and testing + */ + public function dump(){ + $headers = $this->prepareHeaders(); + $body = $this->prepareBody(); + + return $headers.MAILHEADER_EOL.$body; + } +} -- cgit v1.2.3 From 1d045709e66a239d6a0933c0d07dfbb9fb257d48 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Wed, 2 Nov 2011 19:19:11 +0100 Subject: The Mailer class should work now It's still not real world tested but the output *looks* right. Plugin hook support is still missing. --- inc/Mailer.class.php | 173 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 148 insertions(+), 25 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 991ec6c3e..420277d9e 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -1,15 +1,19 @@ */ - // end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?) // think different if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n"); #define('MAILHEADER_ASCIIONLY',1); - class Mailer { private $headers = array(); @@ -19,10 +23,23 @@ class Mailer { private $boundary = ''; private $partid = ''; - private $sendparam= ''; + private $sendparam= null; + + private $validator = null; + + /** + * Constructor + * + * Initializes the boundary strings and part counters + */ + public function __construct(){ + if(isset($_SERVER['SERVER_NAME'])){ + $server = $_SERVER['SERVER_NAME']; + }else{ + $server = 'localhost'; + } - function __construct(){ - $this->partid = md5(uniqid(rand(),true)).'@'.$_SERVER['SERVER_NAME']; + $this->partid = md5(uniqid(rand(),true)).'@'.$server; $this->boundary = '----------'.md5(uniqid(rand(),true)); } @@ -69,24 +86,50 @@ class Mailer { ); } + /** + * Add an arbitrary header to the mail + * + * @param string $header the header name (no trailing colon!) + * @param string $value the value of the header + * @param bool $clean remove all non-ASCII chars and line feeds? + */ + public function setHeader($header,$value,$clean=true){ + $header = ucwords(strtolower($header)); // streamline casing + if($clean){ + $header = preg_replace('/[^\w \-\.\+\@]+/','',$header); + $value = preg_replace('/[^\w \-\.\+\@]+/','',$value); + } + $this->headers[$header] = $value; + } + + /** + * Set additional parameters to be passed to sendmail + * + * Whatever is set here is directly passed to PHP's mail() command as last + * parameter. Depending on the PHP setup this might break mailing alltogether + */ + public function setParameters($param){ + $this->sendparam = $param; + } + /** * Set the HTML part of the mail * * Placeholders can be used to reference embedded attachments */ - public function setHTMLBody($html){ + public function setHTML($html){ $this->html = $html; } /** * Set the plain text part of the mail */ - public function setTextBody($text){ + public function setText($text){ $this->text = $text; } /** - * Ses an email address header with correct encoding + * Sets an email address header with correct encoding * * Unicode characters will be deaccented and encoded base64 * for headers. Addresses may not contain Non-ASCII data! @@ -97,10 +140,13 @@ class Mailer { * @param string $address Multiple adresses separated by commas * @param string $header Name of the header (To,Bcc,Cc,...) */ - function mail_encode_address($address,$header){ + function setAddress($address,$header){ // No named recipients for To: in Windows (see FS#652) $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; + $header = ucwords(strtolower($header)); // streamline casing + $address = preg_replace('/[\r\n\0]+/',' ',$address); // remove attack vectors + $headers = ''; $parts = explode(',',$address); foreach ($parts as $part){ @@ -124,7 +170,11 @@ class Mailer { continue; } - if(!mail_isvalid($addr)){ + if(is_null($this->validator)){ + $this->validator = new EmailAddressValidator(); + $this->validator->allowLocalAddresses = true; + } + if(!$this->validator->check_email_address($addr)){ msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); continue; } @@ -140,7 +190,7 @@ class Mailer { } if(!utf8_isASCII($text)){ - //FIXME + //FIXME check if this is needed for base64 too // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" /* if (preg_match('/^"(.+)"$/', $text, $matches)) { @@ -199,6 +249,19 @@ class Mailer { $this->setAddress($address, 'Bcc'); } + /** + * Add the From: address + * + * This is set to $conf['mailfrom'] when not specified so you shouldn't need + * to call this function + * + * @see setAddress + * @param string $address from address + */ + public function from($address){ + $this->setAddress($address, 'From'); + } + /** * Add the mail's Subject: header * @@ -246,20 +309,33 @@ class Mailer { return $mime; } - protected function createBody(){ + /** + * Build the body and handles multi part mails + * + * Needs to be called before prepareHeaders! + * + * @return string the prepared mail body, false on errors + */ + protected function prepareBody(){ + global $conf; + // check for body if(!$this->text && !$this->html){ return false; } // add general headers + if(!isset($this->headers['From'])) $this->from($conf['mailfrom']); $this->headers['MIME-Version'] = '1.0'; + $body = ''; + if(!$this->html && !count($this->attach)){ // we can send a simple single part message $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; $this->headers['Content-Transfer-Encoding'] = 'base64'; - $body = chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); + $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); }else{ // multi part it is + $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; // prepare the attachments $attachments = $this->prepareAttachments(); @@ -267,25 +343,21 @@ class Mailer { // do we have alternative text content? if($this->text && $this->html){ $this->headers['Content-Type'] = 'multipart/alternative; boundary="'.$this->boundary.'XX"'; - $body = "This is a multi-part message in MIME format.".MAILHEADER_EOL; $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; + $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; + $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; $body .= MAILHEADER_EOL; - $body .= 'Content-Type: text/plain; charset=UTF-8'; - $body .= 'Content-Transfer-Encoding: base64'; $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; $body .= 'Content-Type: multipart/related; boundary="'.$this->boundary.'"'.MAILHEADER_EOL; $body .= MAILHEADER_EOL; - }else{ - $this->headers['Content-Type'] = 'multipart/related; boundary="'.$this->boundary.'"'; - $body = "This is a multi-part message in MIME format.".MAILHEADER_EOL; } - $body .= '--'.$this->boundary."\n"; - $body .= "Content-Type: text/html; charset=UTF-8\n"; - $body .= "Content-Transfer-Encoding: base64\n"; + $body .= '--'.$this->boundary.MAILHEADER_EOL; + $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; + $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; $body .= MAILHEADER_EOL; - $body = chunk_split(base64_encode($this->html),74,MAILHEADER_EOL); + $body .= chunk_split(base64_encode($this->html),74,MAILHEADER_EOL); $body .= MAILHEADER_EOL; $body .= $attachments; $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; @@ -301,6 +373,8 @@ class Mailer { /** * Create a string from the headers array + * + * @returns string the headers */ protected function prepareHeaders(){ $headers = ''; @@ -313,12 +387,61 @@ class Mailer { /** * return a full email with all headers * - * This is mainly for debugging and testing + * This is mainly intended for debugging and testing but could also be + * used for MHT exports + * + * @return string the mail, false on errors */ public function dump(){ - $headers = $this->prepareHeaders(); $body = $this->prepareBody(); + if($body === 'false') return false; + $headers = $this->prepareHeaders(); return $headers.MAILHEADER_EOL.$body; } + + /** + * Send the mail + * + * Call this after all data was set + * + * @fixme we need to support the old plugin hook here! + * @return bool true if the mail was successfully passed to the MTA + */ + public function send(){ + // any recipients? + if(trim($this->headers['To']) === '' && + trim($this->headers['Cc']) === '' && + trim($this->headers['Bcc']) === '') return false; + + // The To: header is special + if(isset($this->headers['To'])){ + $to = $this->headers['To']; + unset($this->headers['To']); + }else{ + $to = ''; + } + + // so is the subject + if(isset($this->headers['Subject'])){ + $subject = $this->headers['Subject']; + unset($this->headers['Subject']); + }else{ + $subject = ''; + } + + // make the body + $body = $this->prepareBody(); + if($body === 'false') return false; + + // cook the headers + $headers = $this->prepareHeaders(); + + // send the thing + if(is_null($this->sendparam)){ + return @mail($to,$subject,$body,$headers); + }else{ + return @mail($to,$subject,$body,$headers,$this->sendparam); + } + } } -- cgit v1.2.3 From a36fc3485fd92593133d56e9bb98d739d70ed94f Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 11:01:22 +0100 Subject: clean headers in a separate step this should make it easier to reintroduce a plugin hook compatible with the old one --- inc/Mailer.class.php | 179 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 115 insertions(+), 64 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 420277d9e..8f2992201 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -89,6 +89,8 @@ class Mailer { /** * Add an arbitrary header to the mail * + * If an empy value is passed, the header is removed + * * @param string $header the header name (no trailing colon!) * @param string $value the value of the header * @param bool $clean remove all non-ASCII chars and line feeds? @@ -99,7 +101,14 @@ class Mailer { $header = preg_replace('/[^\w \-\.\+\@]+/','',$header); $value = preg_replace('/[^\w \-\.\+\@]+/','',$value); } - $this->headers[$header] = $value; + + // empty value deletes + $value = trim($value); + if($value === ''){ + if(isset($this->headers[$header])) unset($this->headers[$header]); + }else{ + $this->headers[$header] = $value; + } } /** @@ -128,6 +137,58 @@ class Mailer { $this->text = $text; } + /** + * Add the To: recipients + * + * @see setAddress + * @param string $address Multiple adresses separated by commas + */ + public function to($address){ + $this->setHeader('To', $address, false); + } + + /** + * Add the Cc: recipients + * + * @see setAddress + * @param string $address Multiple adresses separated by commas + */ + public function cc($address){ + $this->setHeader('Cc', $address, false); + } + + /** + * Add the Bcc: recipients + * + * @see setAddress + * @param string $address Multiple adresses separated by commas + */ + public function bcc($address){ + $this->setHeader('Bcc', $address, false); + } + + /** + * Add the From: address + * + * This is set to $conf['mailfrom'] when not specified so you shouldn't need + * to call this function + * + * @see setAddress + * @param string $address from address + */ + public function from($address){ + $this->setHeader('From', $address, false); + } + + /** + * Add the mail's Subject: header + * + * @param string $subject the mail subject + */ + public function subject($subject){ + $this->headers['Subject'] = $subject; + } + /** * Sets an email address header with correct encoding * @@ -138,13 +199,12 @@ class Mailer { * setAddress("föö , me@somewhere.com","TBcc"); * * @param string $address Multiple adresses separated by commas - * @param string $header Name of the header (To,Bcc,Cc,...) + * @param string returns the prepared header (can contain multiple lines) */ - function setAddress($address,$header){ + public function cleanAddress($address){ // No named recipients for To: in Windows (see FS#652) $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; - $header = ucwords(strtolower($header)); // streamline casing $address = preg_replace('/[\r\n\0]+/',' ',$address); // remove attack vectors $headers = ''; @@ -207,72 +267,16 @@ class Mailer { // add to header comma seperated if($headers != ''){ - $headers .= ','; - $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers + $headers .= ', '; } $headers .= $text.' '.$addr; } if(empty($headers)) return false; - $this->headers[$header] = $headers; return $headers; } - /** - * Add the To: recipients - * - * @see setAddress - * @param string $address Multiple adresses separated by commas - */ - public function to($address){ - $this->setAddress($address, 'To'); - } - - /** - * Add the Cc: recipients - * - * @see setAddress - * @param string $address Multiple adresses separated by commas - */ - public function cc($address){ - $this->setAddress($address, 'Cc'); - } - - /** - * Add the Bcc: recipients - * - * @see setAddress - * @param string $address Multiple adresses separated by commas - */ - public function bcc($address){ - $this->setAddress($address, 'Bcc'); - } - - /** - * Add the From: address - * - * This is set to $conf['mailfrom'] when not specified so you shouldn't need - * to call this function - * - * @see setAddress - * @param string $address from address - */ - public function from($address){ - $this->setAddress($address, 'From'); - } - - /** - * Add the mail's Subject: header - * - * @param string $subject the mail subject - */ - public function subject($subject){ - if(!utf8_isASCII($subject)){ - $subject = '=?UTF-8?B?'.base64_encode($subject).'?='; - } - $this->headers['Subject'] = $subject; - } /** * Prepare the mime multiparts for all attachments @@ -325,7 +329,6 @@ class Mailer { } // add general headers - if(!isset($this->headers['From'])) $this->from($conf['mailfrom']); $this->headers['MIME-Version'] = '1.0'; $body = ''; @@ -342,14 +345,16 @@ class Mailer { // do we have alternative text content? if($this->text && $this->html){ - $this->headers['Content-Type'] = 'multipart/alternative; boundary="'.$this->boundary.'XX"'; + $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. + ' boundary="'.$this->boundary.'XX"'; $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; $body .= MAILHEADER_EOL; $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; - $body .= 'Content-Type: multipart/related; boundary="'.$this->boundary.'"'.MAILHEADER_EOL; + $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. + ' boundary="'.$this->boundary.'"'.MAILHEADER_EOL; $body .= MAILHEADER_EOL; } @@ -371,6 +376,47 @@ class Mailer { return $body; } + /** + * Cleanup and encode the headers array + */ + protected function cleanHeaders(){ + global $conf; + + // clean up addresses + if(empty($this->headers['From'])) $this->from($conf['mailfrom']); + $addrs = array('To','From','Cc','Bcc'); + foreach($addrs as $addr){ + if(isset($this->headers[$addr])){ + $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); + } + } + + if(isset($subject)){ + // add prefix to subject + if($conf['mailprefix']){ + $prefix = '['.$conf['mailprefix'].']'; + $len = strlen($prefix); + if(substr($this->headers['subject'],0,$len) != $prefix){ + $this->headers['subject'] = $prefix.' '.$this->headers['subject']; + } + } + + // encode subject + if(defined('MAILHEADER_ASCIIONLY')){ + $this->headers['subject'] = utf8_deaccent($this->headers['subject']); + $this->headers['subject'] = utf8_strip($this->headers['subject']); + } + if(!utf8_isASCII($this->headers['Subject'])){ + $subject = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; + } + } + + // wrap headers + foreach($this->headers as $key => $val){ + $this->headers[$key] = wordwrap($val,78,MAILHEADER_EOL.' '); + } + } + /** * Create a string from the headers array * @@ -393,6 +439,7 @@ class Mailer { * @return string the mail, false on errors */ public function dump(){ + $this->cleanHeaders(); $body = $this->prepareBody(); if($body === 'false') return false; $headers = $this->prepareHeaders(); @@ -409,6 +456,10 @@ class Mailer { * @return bool true if the mail was successfully passed to the MTA */ public function send(){ + // FIXME hook here + + $this->cleanHeaders(); + // any recipients? if(trim($this->headers['To']) === '' && trim($this->headers['Cc']) === '' && -- cgit v1.2.3 From 28d2ad801d5eafcfdfc9a122966458b2689a7ec6 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 11:35:17 +0100 Subject: added old plugin hook back into Mailer class it now passes the whole Mail object and also signals if the mail sending was successful to the AFTER event. A bunch of references should make it compatible with old plugins. --- inc/Mailer.class.php | 95 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 8f2992201..3b7762e77 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -452,47 +452,76 @@ class Mailer { * * Call this after all data was set * - * @fixme we need to support the old plugin hook here! + * @triggers MAIL_MESSAGE_SEND * @return bool true if the mail was successfully passed to the MTA */ public function send(){ - // FIXME hook here - - $this->cleanHeaders(); - - // any recipients? - if(trim($this->headers['To']) === '' && - trim($this->headers['Cc']) === '' && - trim($this->headers['Bcc']) === '') return false; + $success = false; + + // prepare hook data + $data = array( + // pass the whole mail class to plugin + 'mail' => $this, + // pass references for backward compatibility + 'to' => &$this->headers['To'], + 'cc' => &$this->headers['Cc'], + 'bcc' => &$this->headers['Bcc'], + 'from' => &$this->headers['From'], + 'subject' => &$this->headers['Subject'], + 'body' => &$this->text, + 'params' => &$this->sendparams, + 'headers' => '', // plugins shouldn't use this + // signal if we mailed successfully to AFTER event + 'success' => &$success, + ); - // The To: header is special - if(isset($this->headers['To'])){ - $to = $this->headers['To']; - unset($this->headers['To']); - }else{ - $to = ''; - } + // do our thing if BEFORE hook approves + $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); + if ($evt->advise_before(true)) { + // clean up before using the headers + $this->cleanHeaders(); + + // any recipients? + if(trim($this->headers['To']) === '' && + trim($this->headers['Cc']) === '' && + trim($this->headers['Bcc']) === '') return false; + + // The To: header is special + if(isset($this->headers['To'])){ + $to = $this->headers['To']; + unset($this->headers['To']); + }else{ + $to = ''; + } - // so is the subject - if(isset($this->headers['Subject'])){ - $subject = $this->headers['Subject']; - unset($this->headers['Subject']); - }else{ - $subject = ''; - } + // so is the subject + if(isset($this->headers['Subject'])){ + $subject = $this->headers['Subject']; + unset($this->headers['Subject']); + }else{ + $subject = ''; + } - // make the body - $body = $this->prepareBody(); - if($body === 'false') return false; + // make the body + $body = $this->prepareBody(); + if($body === 'false') return false; - // cook the headers - $headers = $this->prepareHeaders(); + // cook the headers + $headers = $this->prepareHeaders(); + // add any headers set by legacy plugins + if(trim($data['headers'])){ + $headers .= MAILHEADER_EOL.trim($data['headers']); + } - // send the thing - if(is_null($this->sendparam)){ - return @mail($to,$subject,$body,$headers); - }else{ - return @mail($to,$subject,$body,$headers,$this->sendparam); + // send the thing + if(is_null($this->sendparam)){ + $success = @mail($to,$subject,$body,$headers); + }else{ + $success = @mail($to,$subject,$body,$headers,$this->sendparam); + } } + // any AFTER actions? + $evt->advise_after(); + return $success; } } -- cgit v1.2.3 From 54f3075553a782ff5e32a5326f0e225f4437be29 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 11:54:51 +0100 Subject: mail prefix defaults to title when empty --- inc/Mailer.class.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 3b7762e77..7b09cf75e 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -393,12 +393,14 @@ class Mailer { if(isset($subject)){ // add prefix to subject - if($conf['mailprefix']){ + if(empty($conf['mailprefix'])){ + $prefix = '['.$conf['title'].']'; + }else{ $prefix = '['.$conf['mailprefix'].']'; - $len = strlen($prefix); - if(substr($this->headers['subject'],0,$len) != $prefix){ - $this->headers['subject'] = $prefix.' '.$this->headers['subject']; - } + } + $len = strlen($prefix); + if(substr($this->headers['subject'],0,$len) != $prefix){ + $this->headers['subject'] = $prefix.' '.$this->headers['subject']; } // encode subject -- cgit v1.2.3 From 8a215f0965e27ba6b9e62b955890f1acc9445883 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 12:20:46 +0100 Subject: shorten title when used as prefix --- inc/Mailer.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 7b09cf75e..ffacc661d 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -394,7 +394,11 @@ class Mailer { if(isset($subject)){ // add prefix to subject if(empty($conf['mailprefix'])){ - $prefix = '['.$conf['title'].']'; + if(utf8_strlen($conf['title']) < 20) { + $prefix = '['.$conf['title'].']'; + }else{ + $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; + } }else{ $prefix = '['.$conf['mailprefix'].']'; } -- cgit v1.2.3 From abbf08905d37ee82c6dda4c2afdbb54383c91517 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 13:02:00 +0100 Subject: Added setBody() to Mailer class This method makes it easy to send a HTML mail based on the default text mails. This is probably enough for simpler mails where no sophisticated HTML is needed except a bit of formatting and linked URLs. --- inc/Mailer.class.php | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index ffacc661d..b86a9d8f2 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -121,10 +121,80 @@ class Mailer { $this->sendparam = $param; } + /** + * Set the text and HTML body and apply replacements + * + * This function applies a whole bunch of default replacements in addition + * to the ones specidifed as parameters + * + * 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 + */ + public function setBody($text, $textrep=null, $htmlrep=null, $html=null){ + global $INFO; + global $conf; + + // create HTML from text if not given + if(is_null($html)){ + $html = hsc($text); + $html = nl2br($text); + } + if(!is_null($textrep) && is_null($htmlrep)){ + $htmlrep = array_map('hsc',$textrep); + } + + // prepare default replacements + $ip = clientIP(); + $trep = array( + 'DATE' => dformat(), + 'BROWSER' => $_SERVER['HTTP_USER_AGENT'], + 'IPADDRESS' => $ip, + 'HOSTNAME' => gethostsbyaddrs($ip), + 'TITLE' => $conf['title'], + 'DOKUWIKIURL' => DOKU_URL, + 'USER' => $_SERVER['REMOTE_USER'], + 'NAME' => $INFO['userinfo']['name'], + 'MAIL' => $INFO['userinfo']['mail'], + ); + $trep = array_merge($trep,(array) $textrep); + $hrep = array( + 'DATE' => ''.hsc(dformat()).'', + 'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']), + 'IPADDRESS' => ''.hsc($ip).'', + 'HOSTNAME' => ''.hsc(gethostsbyaddrs($ip)).'', + 'TITLE' => hsc($conf['title']), + 'DOKUWIKIURL' => ''.DOKU_URL.'', + 'USER' => hsc($_SERVER['REMOTE_USER']), + 'NAME' => hsc($INFO['userinfo']['name']), + 'MAIL' => ''. + hsc($INFO['userinfo']['mail']).'', + ); + $hrep = array_merge($hrep,(array) $htmlrep); + + // Apply replacements + foreach ($trep as $key => $substitution) { + $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); + } + foreach ($hrep as $key => $substitution) { + $html = str_replace('@'.strtoupper($key).'@',$substitution, $html); + } + + $this->setHTML($html); + $this->setText($text); + } + /** * Set the HTML part of the mail * * Placeholders can be used to reference embedded attachments + * + * You probably want to use setBody() instead */ public function setHTML($html){ $this->html = $html; @@ -132,6 +202,8 @@ class Mailer { /** * Set the plain text part of the mail + * + * You probably want to use setBody() instead */ public function setText($text){ $this->text = $text; -- cgit v1.2.3 From 76efd6d0637bb67b7dadf65f29a945fe2ce5fc25 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 17:00:25 +0100 Subject: Copy all text replacements to HTML replacements in Mailer With this change it's easy to just set the replacements for HTML that need special attention. --- inc/Mailer.class.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index b86a9d8f2..09e457243 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -139,14 +139,22 @@ class Mailer { public function setBody($text, $textrep=null, $htmlrep=null, $html=null){ global $INFO; global $conf; + $htmlrep = (array) $htmlrep; + $textrep = (array) $textrep; // create HTML from text if not given if(is_null($html)){ $html = hsc($text); $html = nl2br($text); } - if(!is_null($textrep) && is_null($htmlrep)){ - $htmlrep = array_map('hsc',$textrep); + // copy over all replacements missing for HTML (autolink URLs) + foreach($textrep as $key => $value){ + if(isset($htmlrep[$key])) continue; + if(preg_match('/^https?:\/\//i',$value)){ + $htmlrep[$key] = ''.hsc($value).''; + }else{ + $htmlrep[$key] = hsc($value); + } } // prepare default replacements -- cgit v1.2.3 From 6df843ee8dd8133de3e4e8d5cb742d2afa5f6761 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 17:02:02 +0100 Subject: Make use of new Mailer class in notify() It now uses inline diff format for diff HTML mails --- inc/common.php | 74 +++++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/inc/common.php b/inc/common.php index 0c769c50d..b624c334c 100644 --- a/inc/common.php +++ b/inc/common.php @@ -1087,7 +1087,7 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ global $conf; global $INFO; - // decide if there is something to do + // decide if there is something to do, eg. whom to mail if($who == 'admin'){ if(empty($conf['notify'])) return; //notify enabled? $text = rawLocale('mailtext'); @@ -1112,49 +1112,43 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ return; //just to be safe } - $ip = clientIP(); - $text = str_replace('@DATE@',dformat(),$text); - $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); - $text = str_replace('@IPADDRESS@',$ip,$text); - $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text); - $text = str_replace('@NEWPAGE@',wl($id,'',true,'&'),$text); - $text = str_replace('@PAGE@',$id,$text); - $text = str_replace('@TITLE@',$conf['title'],$text); - $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); - $text = str_replace('@SUMMARY@',$summary,$text); - $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); - $text = str_replace('@NAME@',$INFO['userinfo']['name'],$text); - $text = str_replace('@MAIL@',$INFO['userinfo']['mail'],$text); - - foreach ($replace as $key => $substitution) { - $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); - } + // prepare replacements (keys not set in hrep will be taken from trep) + $trep = array( + 'NEWPAGE' => wl($id,'',true,'&'), + 'PAGE' => $id, + 'SUMMARY' => $summary + ); + $trep = array_merge($trep,$replace); + $hrep = array(); + // prepare content if($who == 'register'){ - $subject = $lang['mail_new_user'].' '.$summary; + $subject = $lang['mail_new_user'].' '.$summary; }elseif($rev){ - $subject = $lang['mail_changed'].' '.$id; - $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true,'&'),$text); - $df = new Diff(explode("\n",rawWiki($id,$rev)), - explode("\n",rawWiki($id))); - $dformat = new UnifiedDiffFormatter(); - $diff = $dformat->format($df); + $subject = $lang['mail_changed'].' '.$id; + $trep['OLDPAGE'] = wl($id,"rev=$rev",true,'&'); + $df = new Diff(explode("\n",rawWiki($id,$rev)), + explode("\n",rawWiki($id))); + $dformat = new UnifiedDiffFormatter(); + $tdiff = $dformat->format($df); + $dformat = new InlineDiffFormatter(); + $hdiff = $dformat->format($df); }else{ - $subject=$lang['mail_newpage'].' '.$id; - $text = str_replace('@OLDPAGE@','none',$text); - $diff = rawWiki($id); - } - $text = str_replace('@DIFF@',$diff,$text); - if(empty($conf['mailprefix'])) { - if(utf8_strlen($conf['title']) < 20) { - $subject = '['.$conf['title'].'] '.$subject; - }else{ - $subject = '['.utf8_substr($conf['title'], 0, 20).'...] '.$subject; - } - }else{ - $subject = '['.$conf['mailprefix'].'] '.$subject; - } - mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc); + $subject = $lang['mail_newpage'].' '.$id; + $trep['OLDPAGE'] = '---'; + $tdiff = rawWiki($id); + $hdiff = nl2br(hsc($tdiff)); + } + $trep['DIFF'] = $tdiff; + $hrep['DIFF'] = $hdiff; + + // send mail + $mail = new Mailer(); + $mail->to($to); + $mail->bcc($bcc); + $mail->subject($subject); + $mail->setBody($text,$trep); + return $mail->send(); } /** -- cgit v1.2.3 From d7169d19cde6fc49e27e7444e424549212339ff9 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 17:50:47 +0100 Subject: Replaced mail_send calls with new Mailer class --- inc/auth.php | 42 ++++++++++++++++++++++-------------------- inc/media.php | 34 ++++++++++++++-------------------- inc/subscription.php | 18 ++++++++---------- 3 files changed, 44 insertions(+), 50 deletions(-) diff --git a/inc/auth.php b/inc/auth.php index eff984b36..49346a84f 100644 --- a/inc/auth.php +++ b/inc/auth.php @@ -667,16 +667,17 @@ function auth_sendPassword($user,$password){ if(!$userinfo['mail']) return false; $text = rawLocale('password'); - $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); - $text = str_replace('@FULLNAME@',$userinfo['name'],$text); - $text = str_replace('@LOGIN@',$user,$text); - $text = str_replace('@PASSWORD@',$password,$text); - $text = str_replace('@TITLE@',$conf['title'],$text); - - return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', - $lang['regpwmail'], - $text, - $conf['mailfrom']); + $trep = array( + 'FULLNAME' => $userinfo['name'], + 'LOGIN' => $user, + 'PASSWORD' => $password + ); + + $mail = new Mailer(); + $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); + $mail->subject($lang['regpwmail']); + $mail->setBody($text,$trep); + return $mail->send(); } /** @@ -906,16 +907,17 @@ function act_resendpwd(){ io_saveFile($tfile,$user); $text = rawLocale('pwconfirm'); - $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); - $text = str_replace('@FULLNAME@',$userinfo['name'],$text); - $text = str_replace('@LOGIN@',$user,$text); - $text = str_replace('@TITLE@',$conf['title'],$text); - $text = str_replace('@CONFIRM@',$url,$text); - - if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', - $lang['regpwmail'], - $text, - $conf['mailfrom'])){ + $trep = array( + 'FULLNAME' => $userinfo['name'], + 'LOGIN' => $user, + 'CONFIRM' => $url + ); + + $mail = new Mailer(); + $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); + $mail->subject($lang['regpwmail']); + $mail->setBody($text,$trep); + if($mail->send()){ msg($lang['resendpwdconfirm'],1); }else{ msg($lang['regmailfail'],-1); diff --git a/inc/media.php b/inc/media.php index 9d3e90a54..4e014877b 100644 --- a/inc/media.php +++ b/inc/media.php @@ -514,6 +514,7 @@ function media_contentcheck($file,$mime){ * Send a notify mail on uploads * * @author Andreas Gohr + * @fixme this should embed thumbnails of images in HTML version */ function media_notify($id,$file,$mime,$old_rev=false){ global $lang; @@ -521,31 +522,24 @@ function media_notify($id,$file,$mime,$old_rev=false){ global $INFO; if(empty($conf['notify'])) return; //notify enabled? - $ip = clientIP(); - $text = rawLocale('uploadmail'); - $text = str_replace('@DATE@',dformat(),$text); - $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); - $text = str_replace('@IPADDRESS@',$ip,$text); - $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text); - $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); - $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); - $text = str_replace('@MIME@',$mime,$text); - $text = str_replace('@MEDIA@',ml($id,'',true,'&',true),$text); - $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text); - if ($old_rev && $conf['mediarevisions']) { - $text = str_replace('@OLD@', ml($id, "rev=$old_rev", true, '&', true), $text); - } else { - $text = str_replace('@OLD@', '', $text); - } + $trep = array( + 'MIME' => $mime, + 'MEDIA' => ml($id,'',true,'&',true), + 'SIZE' => filesize_h(filesize($file)), + ); - if(empty($conf['mailprefix'])) { - $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id; + if ($old_rev && $conf['mediarevisions']) { + $trep['OLD'] = ml($id, "rev=$old_rev", true, '&', true); } else { - $subject = '['.$conf['mailprefix'].'] '.$lang['mail_upload'].' '.$id; + $trep['OLD'] = '---'; } - mail_send($conf['notify'],$subject,$text,$conf['mailfrom']); + $mail = new Mailer(); + $mail->to($conf['notify']); + $mail->subject($lang['mail_upload'].' '.$id); + $mail->setBody($text,$trep); + return $mail->send(); } /** diff --git a/inc/subscription.php b/inc/subscription.php index c94f17ad0..e9f17bc28 100644 --- a/inc/subscription.php +++ b/inc/subscription.php @@ -377,18 +377,16 @@ function subscription_send_list($subscriber_mail, $ids, $ns_id) { */ function subscription_send($subscriber_mail, $replaces, $subject, $id, $template) { global $conf; + global $lang; $text = rawLocale($template); - $replaces = array_merge($replaces, array('TITLE' => $conf['title'], - 'DOKUWIKIURL' => DOKU_URL, - 'PAGE' => $id)); - - foreach ($replaces as $key => $substitution) { - $text = str_replace('@'.strtoupper($key).'@', $substitution, $text); - } + $trep = array_merge($replaces, array('PAGE' => $id)); - global $lang; $subject = $lang['mail_' . $subject] . ' ' . $id; - mail_send('', '['.$conf['title'].'] '. $subject, $text, - $conf['mailfrom'], '', $subscriber_mail); + $mail = new Mailer(); + $mail->bcc($subscriber_mail); + $mail->subject($subject); + $mail->setBody($text,$trep); + + return $mail->send(); } -- cgit v1.2.3 From c9a53c46de6ad79de64bd49ca6e4e8296982aa86 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Nov 2011 17:52:19 +0100 Subject: added Mailer class to autoloader --- inc/load.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/load.php b/inc/load.php index d30397f6e..b5cfd4273 100644 --- a/inc/load.php +++ b/inc/load.php @@ -76,6 +76,7 @@ function load_autoload($name){ 'SafeFN' => DOKU_INC.'inc/SafeFN.class.php', 'Sitemapper' => DOKU_INC.'inc/Sitemapper.php', 'PassHash' => DOKU_INC.'inc/PassHash.class.php', + 'Mailer' => DOKU_INC.'inc/Mailer.class.php', 'DokuWiki_Action_Plugin' => DOKU_PLUGIN.'action.php', 'DokuWiki_Admin_Plugin' => DOKU_PLUGIN.'admin.php', -- cgit v1.2.3 From 2adaf2b8a608f4a1ac3fe0dba94ff28d7a0d48e5 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 13 Nov 2011 12:18:09 +0100 Subject: allow non-txt extensions when accessing locales --- inc/common.php | 4 ++-- inc/pageutils.php | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/inc/common.php b/inc/common.php index b624c334c..1d8061ab0 100644 --- a/inc/common.php +++ b/inc/common.php @@ -789,8 +789,8 @@ function formText($text){ * * @author Andreas Gohr */ -function rawLocale($id){ - return io_readFile(localeFN($id)); +function rawLocale($id,$ext='txt'){ + return io_readFile(localeFN($id,$ext)); } /** diff --git a/inc/pageutils.php b/inc/pageutils.php index 31b5f9ff9..c355d4894 100644 --- a/inc/pageutils.php +++ b/inc/pageutils.php @@ -347,27 +347,29 @@ function mediaFN($id, $rev=''){ if(empty($rev)){ $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); }else{ - $ext = mimetype($id); - $name = substr($id,0, -1*strlen($ext[0])-1); + $ext = mimetype($id); + $name = substr($id,0, -1*strlen($ext[0])-1); $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); } return $fn; } /** - * Returns the full filepath to a localized textfile if local + * Returns the full filepath to a localized file if local * version isn't found the english one is returned * + * @param string $id The id of the local file + * @param string $ext The file extension (usually txt) * @author Andreas Gohr */ -function localeFN($id){ +function localeFN($id,$ext='txt'){ global $conf; - $file = DOKU_CONF.'/lang/'.$conf['lang'].'/'.$id.'.txt'; + $file = DOKU_CONF.'/lang/'.$conf['lang'].'/'.$id.'.'.$ext; if(!@file_exists($file)){ - $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt'; + $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; if(!@file_exists($file)){ //fall back to english - $file = DOKU_INC.'inc/lang/en/'.$id.'.txt'; + $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; } } return $file; -- cgit v1.2.3 From f08086ec453c4529ae7361f9540b430ee61238f0 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 13 Nov 2011 12:37:45 +0100 Subject: 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' => ''.hsc(dformat()).'', 'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']), 'IPADDRESS' => ''.hsc($ip).'', - 'HOSTNAME' => ''.hsc(gethostsbyaddrs($ip)).'', + 'HOSTNAME' => ''.hsc($cip).'', 'TITLE' => hsc($conf['title']), 'DOKUWIKIURL' => ''.DOKU_URL.'', '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 @@ + + + @TITLE@ + + + + +@HTMLBODY@ + +


+This mail was generated by DokuWiki at @DOKUWIKIURL@. + + -- cgit v1.2.3 From 850dbf1f4b1e2bdee8b2e5f0438a5625d0a1bedf Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 13 Nov 2011 13:06:07 +0100 Subject: allow image embeds in HTML mail templates Using the place holder @MEDIA()@ you now can directly embed images from the media repository into your HTML mail template. An example was added to the mailwrap.html, but because images are embedded as is (no prescaling) this is suboptimal. If we keep it, a smaller version of the DokuWiki logo should be shipped with DokuWiki. --- inc/Mailer.class.php | 22 ++++++++++++++++++++++ inc/lang/en/mailwrap.html | 1 + 2 files changed, 23 insertions(+) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index cdd4b266a..7e6889292 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -86,6 +86,24 @@ class Mailer { ); } + /** + * Callback function to automatically embed images referenced in HTML templates + */ + protected function autoembed_cb($matches){ + static $embeds = 0; + $embeds++; + + // get file and mime type + $media = cleanID($matches[1]); + list($ext, $mime) = mimetype($media); + $file = mediaFN($media); + if(!file_exists($file)) return $matches[0]; //bad reference, keep as is + + // attach it and set placeholder + $this->attachFile($file,$mime,'','autoembed'.$embeds); + return '%%autoembed'.$embeds.'%%'; + } + /** * Add an arbitrary header to the mail * @@ -163,6 +181,10 @@ class Mailer { } } + // embed media from templates + $html = preg_replace_callback('/@MEDIA\(([^\)]+)\)@/', + array($this,'autoembed_cb'),$html); + // prepare default replacements $ip = clientIP(); $cip = gethostsbyaddrs($ip); diff --git a/inc/lang/en/mailwrap.html b/inc/lang/en/mailwrap.html index d67644c95..00daf29ac 100644 --- a/inc/lang/en/mailwrap.html +++ b/inc/lang/en/mailwrap.html @@ -8,6 +8,7 @@ @HTMLBODY@


+DokuWiki This mail was generated by DokuWiki at @DOKUWIKIURL@. -- cgit v1.2.3 From b796e32e30bfcda629ebcf2a86550c6371839b0d Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 4 Mar 2012 17:43:51 +0100 Subject: fixed missing replacement for HTML notify mails --- inc/common.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/common.php b/inc/common.php index 1d8061ab0..a75d452a0 100644 --- a/inc/common.php +++ b/inc/common.php @@ -1147,7 +1147,7 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ $mail->to($to); $mail->bcc($bcc); $mail->subject($subject); - $mail->setBody($text,$trep); + $mail->setBody($text,$trep,$hrep); return $mail->send(); } -- cgit v1.2.3 From 45992a638a85374bf0df1084b3657c845abec060 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Mar 2012 07:58:02 +0100 Subject: fixed mailprefix handling --- inc/Mailer.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 7e6889292..5a5e7f2c6 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -500,7 +500,7 @@ class Mailer { } } - if(isset($subject)){ + if(isset($this->headers['Subject'])){ // add prefix to subject if(empty($conf['mailprefix'])){ if(utf8_strlen($conf['title']) < 20) { @@ -512,17 +512,17 @@ class Mailer { $prefix = '['.$conf['mailprefix'].']'; } $len = strlen($prefix); - if(substr($this->headers['subject'],0,$len) != $prefix){ - $this->headers['subject'] = $prefix.' '.$this->headers['subject']; + if(substr($this->headers['Subject'],0,$len) != $prefix){ + $this->headers['Subject'] = $prefix.' '.$this->headers['Subject']; } // encode subject if(defined('MAILHEADER_ASCIIONLY')){ - $this->headers['subject'] = utf8_deaccent($this->headers['subject']); - $this->headers['subject'] = utf8_strip($this->headers['subject']); + $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']); + $this->headers['Subject'] = utf8_strip($this->headers['Subject']); } if(!utf8_isASCII($this->headers['Subject'])){ - $subject = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; + $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; } } -- cgit v1.2.3 From a4c4a73d78c4db014dbc66ea67968472d52708f9 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Mar 2012 08:02:27 +0100 Subject: fixed signature stripping --- inc/Mailer.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 5a5e7f2c6..753ea3fd6 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -167,7 +167,7 @@ class Mailer { } if($wrap){ $wrap = rawLocale('mailwrap','html'); - $html = preg_replace('/\n-- \n.*$/m','',$html); //strip signature + $html = preg_replace('/\n--
.*$/s','',$html); //strip signature $html = str_replace('@HTMLBODY@',$html,$wrap); } -- cgit v1.2.3 From 47a906ead4a9ea1105bd2d1038b64b7bd0d67b76 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Mar 2012 09:17:01 +0100 Subject: use inlinestyles for diffs in HTML mails --- inc/DifferenceEngine.php | 61 +++++++++++++++++++++++++++++++++++++----------- inc/common.php | 4 ++++ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/inc/DifferenceEngine.php b/inc/DifferenceEngine.php index 6e1d07382..52dca32c8 100644 --- a/inc/DifferenceEngine.php +++ b/inc/DifferenceEngine.php @@ -818,6 +818,39 @@ class DiffFormatter { } } +/** + * Utilityclass for styling HTML formatted diffs + * + * Depends on global var $DIFF_INLINESTYLES, if true some minimal predefined + * inline styles are used. Useful for HTML mails and RSS feeds + * + * @author Andreas Gohr + */ +class HTMLDiff { + /** + * Holds the style names and basic CSS + */ + static public $styles = array( + 'diff-addedline' => 'background-color: #ddffdd;', + 'diff-deletedline' => 'background-color: #ffdddd;', + 'diff-context' => 'background-color: #f5f5f5;', + 'diff-mark' => 'color: #ff0000;', + ); + + /** + * Return a class or style parameter + */ + static function css($classname){ + global $DIFF_INLINESTYLES; + + if($DIFF_INLINESTYLES){ + if(!isset(self::$styles[$classname])) return ''; + return 'style="'.self::$styles[$classname].'"'; + }else{ + return 'class="'.$classname.'"'; + } + } +} /** * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3 @@ -838,11 +871,11 @@ class _HWLDF_WordAccumulator { function _flushGroup($new_tag) { if ($this->_group !== '') { if ($this->_tag == 'mark') - $this->_line .= ''.$this->_group.''; + $this->_line .= ''.$this->_group.''; elseif ($this->_tag == 'add') - $this->_line .= ''.$this->_group.''; + $this->_line .= ''.$this->_group.''; elseif ($this->_tag == 'del') - $this->_line .= ''.$this->_group.''; + $this->_line .= ''.$this->_group.''; else $this->_line .= $this->_group; } @@ -1020,8 +1053,8 @@ class TableDiffFormatter extends DiffFormatter { global $lang; $l1 = $lang['line'].' '.$xbeg; $l2 = $lang['line'].' '.$ybeg; - $r = ''.$l1.":\n". - ''.$l2.":\n". + $r = ''.$l1.":\n". + ''.$l2.":\n". "\n"; return $r; } @@ -1037,11 +1070,11 @@ class TableDiffFormatter extends DiffFormatter { } function addedLine($line) { - return '+' . $line.''; + return '+' . $line.''; } function deletedLine($line) { - return '-' . $line.''; + return '-' . $line.''; } function emptyLine() { @@ -1049,7 +1082,7 @@ class TableDiffFormatter extends DiffFormatter { } function contextLine($line) { - return ' '.$line.''; + return ' '.$line.''; } function _added($lines) { @@ -1115,9 +1148,9 @@ class InlineDiffFormatter extends DiffFormatter { $xbeg .= "," . $xlen; if ($ylen != 1) $ybeg .= "," . $ylen; - $r = '@@ '.$lang['line']." -$xbeg +$ybeg @@"; - $r .= ' '.$lang['deleted'].''; - $r .= ' '.$lang['created'].''; + $r = '@@ '.$lang['line']." -$xbeg +$ybeg @@"; + $r .= ' '.$lang['deleted'].''; + $r .= ' '.$lang['created'].''; $r .= "\n"; return $r; } @@ -1134,19 +1167,19 @@ class InlineDiffFormatter extends DiffFormatter { function _added($lines) { foreach ($lines as $line) { - print(''. $line . "\n"); + print(''. $line . "\n"); } } function _deleted($lines) { foreach ($lines as $line) { - print('' . $line . "\n"); + print('' . $line . "\n"); } } function _context($lines) { foreach ($lines as $line) { - print(''.$line."\n"); + print(''.$line."\n"); } } diff --git a/inc/common.php b/inc/common.php index a75d452a0..5e2bb7c93 100644 --- a/inc/common.php +++ b/inc/common.php @@ -1086,6 +1086,7 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ global $lang; global $conf; global $INFO; + global $DIFF_INLINESTYLES; // decide if there is something to do, eg. whom to mail if($who == 'admin'){ @@ -1131,8 +1132,11 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ explode("\n",rawWiki($id))); $dformat = new UnifiedDiffFormatter(); $tdiff = $dformat->format($df); + + $DIFF_INLINESTYLES = true; $dformat = new InlineDiffFormatter(); $hdiff = $dformat->format($df); + $DIFF_INLINESTYLES = false; }else{ $subject = $lang['mail_newpage'].' '.$id; $trep['OLDPAGE'] = '---'; -- cgit v1.2.3 From d2113a3cac4f670858d94dc53de4f653828dac91 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Mar 2012 09:19:30 +0100 Subject: removed footer image from HTML mails --- inc/lang/en/mailwrap.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inc/lang/en/mailwrap.html b/inc/lang/en/mailwrap.html index 00daf29ac..f9f80fd80 100644 --- a/inc/lang/en/mailwrap.html +++ b/inc/lang/en/mailwrap.html @@ -7,8 +7,7 @@ @HTMLBODY@ -


-DokuWiki +

This mail was generated by DokuWiki at @DOKUWIKIURL@. -- cgit v1.2.3 From 9f3eca0b520fc5f689170108fbcbf490d12f2c2e Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Mar 2012 12:44:28 +0100 Subject: Add various headers to the mails FS#2247. pull request #83 closed --- inc/Mailer.class.php | 23 ++++++++++++++++------- inc/common.php | 7 +++++++ inc/subscription.php | 6 +++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 753ea3fd6..141b69a18 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -33,14 +33,23 @@ class Mailer { * Initializes the boundary strings and part counters */ public function __construct(){ - if(isset($_SERVER['SERVER_NAME'])){ - $server = $_SERVER['SERVER_NAME']; - }else{ - $server = 'localhost'; - } + global $conf; + + $server = parse_url(DOKU_URL,PHP_URL_HOST); $this->partid = md5(uniqid(rand(),true)).'@'.$server; $this->boundary = '----------'.md5(uniqid(rand(),true)); + + $listid = join('.',array_reverse(explode('/',DOKU_BASE))).$server; + $listid = strtolower(trim($listid,'.')); + + // add some default headers for mailfiltering FS#2247 + $this->setHeader('X-Mailer','DokuWiki '.getVersion()); + $this->setHeader('X-DokuWiki-User', $_SERVER['REMOTE_USER']); + $this->setHeader('X-DokuWiki-Title', $conf['title']); + $this->setHeader('X-DokuWiki-Server', $server); + $this->setHeader('X-Auto-Response-Suppress', 'OOF'); + $this->setHeader('List-Id',$conf['title'].' <'.$listid.'>'); } /** @@ -114,10 +123,10 @@ class Mailer { * @param bool $clean remove all non-ASCII chars and line feeds? */ public function setHeader($header,$value,$clean=true){ - $header = ucwords(strtolower($header)); // streamline casing + $header = str_replace(' ','-',ucwords(strtolower(str_replace('-',' ',$header)))); // streamline casing if($clean){ $header = preg_replace('/[^\w \-\.\+\@]+/','',$header); - $value = preg_replace('/[^\w \-\.\+\@]+/','',$value); + $value = preg_replace('/[^\w \-\.\+\@<>]+/','',$value); } // empty value deletes diff --git a/inc/common.php b/inc/common.php index 5e2bb7c93..79646eadc 100644 --- a/inc/common.php +++ b/inc/common.php @@ -1152,6 +1152,13 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ $mail->bcc($bcc); $mail->subject($subject); $mail->setBody($text,$trep,$hrep); + if($who == 'subscribers'){ + $mail->setHeader( + 'List-Unsubscribe', + '<'.wl($id,array('do'=>'subscribe'),true,'&').'>', + false + ); + } return $mail->send(); } diff --git a/inc/subscription.php b/inc/subscription.php index e9f17bc28..d1ee0397a 100644 --- a/inc/subscription.php +++ b/inc/subscription.php @@ -387,6 +387,10 @@ function subscription_send($subscriber_mail, $replaces, $subject, $id, $template $mail->bcc($subscriber_mail); $mail->subject($subject); $mail->setBody($text,$trep); - + $mail->setHeader( + 'List-Unsubscribe', + '<'.wl($id,array('do'=>'subscribe'),true,'&').'>', + false + ); return $mail->send(); } -- cgit v1.2.3 From ba9c057b0126f2c449e16adf5073920b5a663773 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Mar 2012 12:50:34 +0100 Subject: use real HRs in HTML mails --- inc/Mailer.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 141b69a18..d7dc70ded 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -171,8 +171,10 @@ class Mailer { // create HTML from text if not given if(is_null($html)){ - $html = hsc($text); - $html = nl2br($text); + $html = $text; + $html = hsc($html); + $html = preg_replace('/^-----*$/m','
',$html); + $html = nl2br($html); } if($wrap){ $wrap = rawLocale('mailwrap','html'); -- cgit v1.2.3 From 6e87cfd37115039280bb5d88291d188c94b07a38 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Mar 2012 12:50:47 +0100 Subject: fixed subscriber mail signatures --- inc/lang/en/subscr_digest.txt | 2 +- inc/lang/en/subscr_list.txt | 2 +- inc/lang/en/subscr_single.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/lang/en/subscr_digest.txt b/inc/lang/en/subscr_digest.txt index fac8564bd..35011b6e6 100644 --- a/inc/lang/en/subscr_digest.txt +++ b/inc/lang/en/subscr_digest.txt @@ -15,6 +15,6 @@ To cancel the page notifications, log into the wiki at @SUBSCRIBE@ and unsubscribe page and/or namespace changes. --- +-- This mail was generated by DokuWiki at @DOKUWIKIURL@ diff --git a/inc/lang/en/subscr_list.txt b/inc/lang/en/subscr_list.txt index efe27d866..4c38b9326 100644 --- a/inc/lang/en/subscr_list.txt +++ b/inc/lang/en/subscr_list.txt @@ -12,6 +12,6 @@ To cancel the page notifications, log into the wiki at @SUBSCRIBE@ and unsubscribe page and/or namespace changes. --- +-- This mail was generated by DokuWiki at @DOKUWIKIURL@ diff --git a/inc/lang/en/subscr_single.txt b/inc/lang/en/subscr_single.txt index f2abe6d77..673c4c32a 100644 --- a/inc/lang/en/subscr_single.txt +++ b/inc/lang/en/subscr_single.txt @@ -18,6 +18,6 @@ To cancel the page notifications, log into the wiki at @NEWPAGE@ and unsubscribe page and/or namespace changes. --- +-- This mail was generated by DokuWiki at @DOKUWIKIURL@ -- cgit v1.2.3 From 04058413cc2649aa16f5bfe605270e8e5d4536f3 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Wed, 14 Mar 2012 10:40:25 +0100 Subject: add missing table tags for HTML diff mails --- inc/common.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/common.php b/inc/common.php index 79646eadc..d19147387 100644 --- a/inc/common.php +++ b/inc/common.php @@ -1136,6 +1136,7 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ $DIFF_INLINESTYLES = true; $dformat = new InlineDiffFormatter(); $hdiff = $dformat->format($df); + $hdiff = ''.$hdiff.'
'; $DIFF_INLINESTYLES = false; }else{ $subject = $lang['mail_newpage'].' '.$id; -- cgit v1.2.3 From 052b2a1780ce07c45e80cd16a55e43177850de35 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 23 Mar 2012 11:39:25 +0100 Subject: removed commented code left from the quoted_printable days --- inc/Mailer.class.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index d7dc70ded..9744fa44e 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -370,15 +370,6 @@ class Mailer { } if(!utf8_isASCII($text)){ - //FIXME check if this is needed for base64 too - // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" - /* - if (preg_match('/^"(.+)"$/', $text, $matches)) { - $text = '"=?UTF-8?Q?'.mail_quotedprintable_encode($matches[1], 0).'?="'; - } else { - $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text, 0).'?='; - } - */ $text = '=?UTF-8?B?'.base64_encode($text).'?='; } }else{ -- cgit v1.2.3 From 2398a2b54196667d6659d3d0489212b271c43703 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 15 Apr 2012 13:25:05 +0200 Subject: made it possible to disable HTML mails in the config --- conf/dokuwiki.php | 1 + inc/Mailer.class.php | 8 ++++++++ lib/plugins/config/lang/en/lang.php | 1 + lib/plugins/config/settings/config.metadata.php | 1 + 4 files changed, 11 insertions(+) diff --git a/conf/dokuwiki.php b/conf/dokuwiki.php index 298c8e572..fddc38a91 100644 --- a/conf/dokuwiki.php +++ b/conf/dokuwiki.php @@ -101,6 +101,7 @@ $conf['notify'] = ''; //send change info to this email (leave $conf['registernotify'] = ''; //send info about newly registered users to this email (leave blank for nobody) $conf['mailfrom'] = ''; //use this email when sending mails $conf['mailprefix'] = ''; //use this as prefix of outgoing mails +$conf['htmlmail'] = 1; //send HTML multipart mails $conf['gzip_output'] = 0; //use gzip content encodeing for the output xhtml (if allowed by browser) $conf['gdlib'] = 2; //the GDlib version (0, 1 or 2) 2 tries to autodetect $conf['im_convert'] = ''; //path to ImageMagicks convert (will be used instead of GD) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index 9744fa44e..f04cdd3e2 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -26,6 +26,7 @@ class Mailer { private $sendparam= null; private $validator = null; + private $allowhtml = true; /** * Constructor @@ -43,6 +44,8 @@ class Mailer { $listid = join('.',array_reverse(explode('/',DOKU_BASE))).$server; $listid = strtolower(trim($listid,'.')); + $this->allowhtml = (bool) $conf['htmlmail']; + // add some default headers for mailfiltering FS#2247 $this->setHeader('X-Mailer','DokuWiki '.getVersion()); $this->setHeader('X-DokuWiki-User', $_SERVER['REMOTE_USER']); @@ -434,6 +437,11 @@ class Mailer { protected function prepareBody(){ global $conf; + // no HTML mails allowed? remove HTML body + if(!$this->allowhtml){ + $this->html = ''; + } + // check for body if(!$this->text && !$this->html){ return false; diff --git a/lib/plugins/config/lang/en/lang.php b/lib/plugins/config/lang/en/lang.php index 380f2fd1d..fc27e5158 100644 --- a/lib/plugins/config/lang/en/lang.php +++ b/lib/plugins/config/lang/en/lang.php @@ -127,6 +127,7 @@ $lang['notify'] = 'Send change notifications to this email address'; $lang['registernotify'] = 'Send info on newly registered users to this email address'; $lang['mailfrom'] = 'Email address to use for automatic mails'; $lang['mailprefix'] = 'Email subject prefix to use for automatic mails'; +$lang['htmlmail'] = 'Send better looking but larger in size HTML multipart emails. Disable for plain text only mails.'; $lang['gzip_output'] = 'Use gzip Content-Encoding for xhtml'; $lang['gdlib'] = 'GD Lib version'; $lang['im_convert'] = 'Path to ImageMagick\'s convert tool'; diff --git a/lib/plugins/config/settings/config.metadata.php b/lib/plugins/config/settings/config.metadata.php index 5f2c32ea7..60ea6a4c2 100644 --- a/lib/plugins/config/settings/config.metadata.php +++ b/lib/plugins/config/settings/config.metadata.php @@ -179,6 +179,7 @@ $meta['fnencode'] = array('multichoice','_choices' => array('url','safe','utf $meta['autoplural'] = array('onoff'); $meta['mailfrom'] = array('richemail'); $meta['mailprefix'] = array('string'); +$meta['htmlmail'] = array('onoff'); $meta['compress'] = array('onoff'); $meta['cssdatauri'] = array('numeric','_pattern' => '/^\d+$/'); $meta['gzip_output'] = array('onoff'); -- cgit v1.2.3 From f41c79d730286e8e8c95deb88a4c876e08e278a2 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 15 Apr 2012 13:26:57 +0200 Subject: changed internal Mailer members from private to protected --- inc/Mailer.class.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php index f04cdd3e2..507150d00 100644 --- a/inc/Mailer.class.php +++ b/inc/Mailer.class.php @@ -16,17 +16,17 @@ if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n"); class Mailer { - private $headers = array(); - private $attach = array(); - private $html = ''; - private $text = ''; + protected $headers = array(); + protected $attach = array(); + protected $html = ''; + protected $text = ''; - private $boundary = ''; - private $partid = ''; - private $sendparam= null; + protected $boundary = ''; + protected $partid = ''; + protected $sendparam= null; - private $validator = null; - private $allowhtml = true; + protected $validator = null; + protected $allowhtml = true; /** * Constructor -- cgit v1.2.3