1bb01c27cSAndreas Gohr<?php 2bb01c27cSAndreas Gohr/** 31d045709SAndreas Gohr * A class to build and send multi part mails (with HTML content and embedded 41d045709SAndreas Gohr * attachments). All mails are assumed to be in UTF-8 encoding. 51d045709SAndreas Gohr * 61d045709SAndreas Gohr * Attachments are handled in memory so this shouldn't be used to send huge 71d045709SAndreas Gohr * files, but then again mail shouldn't be used to send huge files either. 81d045709SAndreas Gohr * 9bb01c27cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10bb01c27cSAndreas Gohr */ 11bb01c27cSAndreas Gohr 12*e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 13*e1d9dcc8SAndreas Gohr 14bb01c27cSAndreas Gohr// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?) 15bb01c27cSAndreas Gohr// think different 16bb01c27cSAndreas Gohrif(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL', "\n"); 17bb01c27cSAndreas Gohr#define('MAILHEADER_ASCIIONLY',1); 18bb01c27cSAndreas Gohr 19a89c75afSAndreas Gohr/** 20a89c75afSAndreas Gohr * Mail Handling 21a89c75afSAndreas Gohr */ 22bb01c27cSAndreas Gohrclass Mailer { 23bb01c27cSAndreas Gohr 24f41c79d7SAndreas Gohr protected $headers = array(); 25f41c79d7SAndreas Gohr protected $attach = array(); 26f41c79d7SAndreas Gohr protected $html = ''; 27f41c79d7SAndreas Gohr protected $text = ''; 28bb01c27cSAndreas Gohr 29f41c79d7SAndreas Gohr protected $boundary = ''; 30f41c79d7SAndreas Gohr protected $partid = ''; 31f41c79d7SAndreas Gohr protected $sendparam = null; 32bb01c27cSAndreas Gohr 33f41c79d7SAndreas Gohr protected $allowhtml = true; 341d045709SAndreas Gohr 359ea45836SChristopher Smith protected $replacements = array('text'=> array(), 'html' => array()); 369ea45836SChristopher Smith 371d045709SAndreas Gohr /** 381d045709SAndreas Gohr * Constructor 391d045709SAndreas Gohr * 409ea45836SChristopher Smith * Initializes the boundary strings, part counters and token replacements 411d045709SAndreas Gohr */ 421d045709SAndreas Gohr public function __construct() { 439f3eca0bSAndreas Gohr global $conf; 44585bf44eSChristopher Smith /* @var Input $INPUT */ 45585bf44eSChristopher Smith global $INPUT; 469f3eca0bSAndreas Gohr 479f3eca0bSAndreas Gohr $server = parse_url(DOKU_URL, PHP_URL_HOST); 48749c0023SAndreas Gohr if(strpos($server,'.') === false) $server .= '.localhost'; 491d045709SAndreas Gohr 50749c0023SAndreas Gohr $this->partid = substr(md5(uniqid(mt_rand(), true)),0, 8).'@'.$server; 51749c0023SAndreas Gohr $this->boundary = '__________'.md5(uniqid(mt_rand(), true)); 529f3eca0bSAndreas Gohr 53749c0023SAndreas Gohr $listid = implode('.', array_reverse(explode('/', DOKU_BASE))).$server; 549f3eca0bSAndreas Gohr $listid = strtolower(trim($listid, '.')); 559f3eca0bSAndreas Gohr 562398a2b5SAndreas Gohr $this->allowhtml = (bool)$conf['htmlmail']; 572398a2b5SAndreas Gohr 589f3eca0bSAndreas Gohr // add some default headers for mailfiltering FS#2247 595f43dcf4SLukas Rademacher if(!empty($conf['mailreturnpath'])) { 605f43dcf4SLukas Rademacher $this->setHeader('Return-Path', $conf['mailreturnpath']); 615f43dcf4SLukas Rademacher } 626a1f928fSAndreas Gohr $this->setHeader('X-Mailer', 'DokuWiki'); 63585bf44eSChristopher Smith $this->setHeader('X-DokuWiki-User', $INPUT->server->str('REMOTE_USER')); 649f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Title', $conf['title']); 659f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Server', $server); 669f3eca0bSAndreas Gohr $this->setHeader('X-Auto-Response-Suppress', 'OOF'); 679f3eca0bSAndreas Gohr $this->setHeader('List-Id', $conf['title'].' <'.$listid.'>'); 68d6e04b60SAndreas Gohr $this->setHeader('Date', date('r'), false); 699ea45836SChristopher Smith 709ea45836SChristopher Smith $this->prepareTokenReplacements(); 71bb01c27cSAndreas Gohr } 72bb01c27cSAndreas Gohr 73bb01c27cSAndreas Gohr /** 74bb01c27cSAndreas Gohr * Attach a file 75bb01c27cSAndreas Gohr * 76a89c75afSAndreas Gohr * @param string $path Path to the file to attach 77a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 78a89c75afSAndreas Gohr * @param string $name The filename to use 79a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 80bb01c27cSAndreas Gohr */ 81bb01c27cSAndreas Gohr public function attachFile($path, $mime, $name = '', $embed = '') { 82bb01c27cSAndreas Gohr if(!$name) { 833009a773SAndreas Gohr $name = utf8_basename($path); 84bb01c27cSAndreas Gohr } 85bb01c27cSAndreas Gohr 86bb01c27cSAndreas Gohr $this->attach[] = array( 87bb01c27cSAndreas Gohr 'data' => file_get_contents($path), 88bb01c27cSAndreas Gohr 'mime' => $mime, 89bb01c27cSAndreas Gohr 'name' => $name, 90bb01c27cSAndreas Gohr 'embed' => $embed 91bb01c27cSAndreas Gohr ); 92bb01c27cSAndreas Gohr } 93bb01c27cSAndreas Gohr 94bb01c27cSAndreas Gohr /** 95bb01c27cSAndreas Gohr * Attach a file 96bb01c27cSAndreas Gohr * 97a89c75afSAndreas Gohr * @param string $data The file contents to attach 98a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 99a89c75afSAndreas Gohr * @param string $name The filename to use 100a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 101bb01c27cSAndreas Gohr */ 102bb01c27cSAndreas Gohr public function attachContent($data, $mime, $name = '', $embed = '') { 103bb01c27cSAndreas Gohr if(!$name) { 104a89c75afSAndreas Gohr list(, $ext) = explode('/', $mime); 105bb01c27cSAndreas Gohr $name = count($this->attach).".$ext"; 106bb01c27cSAndreas Gohr } 107bb01c27cSAndreas Gohr 108bb01c27cSAndreas Gohr $this->attach[] = array( 109bb01c27cSAndreas Gohr 'data' => $data, 110bb01c27cSAndreas Gohr 'mime' => $mime, 111bb01c27cSAndreas Gohr 'name' => $name, 112bb01c27cSAndreas Gohr 'embed' => $embed 113bb01c27cSAndreas Gohr ); 114bb01c27cSAndreas Gohr } 115bb01c27cSAndreas Gohr 116bb01c27cSAndreas Gohr /** 117850dbf1fSAndreas Gohr * Callback function to automatically embed images referenced in HTML templates 11842ea7f44SGerrit Uitslag * 11942ea7f44SGerrit Uitslag * @param array $matches 12042ea7f44SGerrit Uitslag * @return string placeholder 121850dbf1fSAndreas Gohr */ 122749c0023SAndreas Gohr protected function autoEmbedCallBack($matches) { 123850dbf1fSAndreas Gohr static $embeds = 0; 124850dbf1fSAndreas Gohr $embeds++; 125850dbf1fSAndreas Gohr 126850dbf1fSAndreas Gohr // get file and mime type 127850dbf1fSAndreas Gohr $media = cleanID($matches[1]); 128a89c75afSAndreas Gohr list(, $mime) = mimetype($media); 129850dbf1fSAndreas Gohr $file = mediaFN($media); 130850dbf1fSAndreas Gohr if(!file_exists($file)) return $matches[0]; //bad reference, keep as is 131850dbf1fSAndreas Gohr 132850dbf1fSAndreas Gohr // attach it and set placeholder 133850dbf1fSAndreas Gohr $this->attachFile($file, $mime, '', 'autoembed'.$embeds); 134850dbf1fSAndreas Gohr return '%%autoembed'.$embeds.'%%'; 135850dbf1fSAndreas Gohr } 136850dbf1fSAndreas Gohr 137850dbf1fSAndreas Gohr /** 1381d045709SAndreas Gohr * Add an arbitrary header to the mail 1391d045709SAndreas Gohr * 140a36fc348SAndreas Gohr * If an empy value is passed, the header is removed 141a36fc348SAndreas Gohr * 1421d045709SAndreas Gohr * @param string $header the header name (no trailing colon!) 14359bc3b48SGerrit Uitslag * @param string|string[] $value the value of the header 1441d045709SAndreas Gohr * @param bool $clean remove all non-ASCII chars and line feeds? 1451d045709SAndreas Gohr */ 1461d045709SAndreas Gohr public function setHeader($header, $value, $clean = true) { 1479f3eca0bSAndreas Gohr $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing 1481d045709SAndreas Gohr if($clean) { 149578b2c23SAndreas Gohr $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header); 150578b2c23SAndreas Gohr $value = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value); 1511d045709SAndreas Gohr } 152a36fc348SAndreas Gohr 153a36fc348SAndreas Gohr // empty value deletes 154b6c97c70SAndreas Gohr if(is_array($value)){ 155b6c97c70SAndreas Gohr $value = array_map('trim', $value); 156b6c97c70SAndreas Gohr $value = array_filter($value); 157b6c97c70SAndreas Gohr if(!$value) $value = ''; 158b6c97c70SAndreas Gohr }else{ 159a36fc348SAndreas Gohr $value = trim($value); 160b6c97c70SAndreas Gohr } 161a36fc348SAndreas Gohr if($value === '') { 162a36fc348SAndreas Gohr if(isset($this->headers[$header])) unset($this->headers[$header]); 163a36fc348SAndreas Gohr } else { 1641d045709SAndreas Gohr $this->headers[$header] = $value; 1651d045709SAndreas Gohr } 166a36fc348SAndreas Gohr } 1671d045709SAndreas Gohr 1681d045709SAndreas Gohr /** 1691d045709SAndreas Gohr * Set additional parameters to be passed to sendmail 1701d045709SAndreas Gohr * 1711d045709SAndreas Gohr * Whatever is set here is directly passed to PHP's mail() command as last 1721d045709SAndreas Gohr * parameter. Depending on the PHP setup this might break mailing alltogether 17342ea7f44SGerrit Uitslag * 17442ea7f44SGerrit Uitslag * @param string $param 1751d045709SAndreas Gohr */ 1761d045709SAndreas Gohr public function setParameters($param) { 1771d045709SAndreas Gohr $this->sendparam = $param; 1781d045709SAndreas Gohr } 1791d045709SAndreas Gohr 1801d045709SAndreas Gohr /** 181abbf0890SAndreas Gohr * Set the text and HTML body and apply replacements 182abbf0890SAndreas Gohr * 183abbf0890SAndreas Gohr * This function applies a whole bunch of default replacements in addition 18404dcb5b2SChristopher Smith * to the ones specified as parameters 185abbf0890SAndreas Gohr * 186abbf0890SAndreas Gohr * If you pass the HTML part or HTML replacements yourself you have to make 187abbf0890SAndreas Gohr * sure you encode all HTML special chars correctly 188abbf0890SAndreas Gohr * 189abbf0890SAndreas Gohr * @param string $text plain text body 190abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 19164159a61SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, null to use $textrep (urls wrapped in <a> tags) 19259bc3b48SGerrit Uitslag * @param string $html the HTML body, leave null to create it from $text 193f08086ecSAndreas Gohr * @param bool $wrap wrap the HTML in the default header/Footer 194abbf0890SAndreas Gohr */ 195f08086ecSAndreas Gohr public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true) { 196585bf44eSChristopher Smith 19776efd6d0SAndreas Gohr $htmlrep = (array)$htmlrep; 19876efd6d0SAndreas Gohr $textrep = (array)$textrep; 199abbf0890SAndreas Gohr 200abbf0890SAndreas Gohr // create HTML from text if not given 201749c0023SAndreas Gohr if($html === null) { 202ba9c057bSAndreas Gohr $html = $text; 203ba9c057bSAndreas Gohr $html = hsc($html); 204ba2c2f17Sfurun $html = preg_replace('/^----+$/m', '<hr >', $html); 205ba9c057bSAndreas Gohr $html = nl2br($html); 206abbf0890SAndreas Gohr } 207f08086ecSAndreas Gohr if($wrap) { 208749c0023SAndreas Gohr $wrapper = rawLocale('mailwrap', 'html'); 2093819cafdSfurun $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature 2103819cafdSfurun $html = str_replace('@EMAILSIGNATURE@', '', $html); //strip @EMAILSIGNATURE@ 211749c0023SAndreas Gohr $html = str_replace('@HTMLBODY@', $html, $wrapper); 212f08086ecSAndreas Gohr } 213f08086ecSAndreas Gohr 2143819cafdSfurun if(strpos($text, '@EMAILSIGNATURE@') === false) { 2159ea45836SChristopher Smith $text .= '@EMAILSIGNATURE@'; 2163819cafdSfurun } 217ba2c2f17Sfurun 21876efd6d0SAndreas Gohr // copy over all replacements missing for HTML (autolink URLs) 21976efd6d0SAndreas Gohr foreach($textrep as $key => $value) { 22076efd6d0SAndreas Gohr if(isset($htmlrep[$key])) continue; 2213e7e6067SKlap-in if(media_isexternal($value)) { 22276efd6d0SAndreas Gohr $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>'; 22376efd6d0SAndreas Gohr } else { 22476efd6d0SAndreas Gohr $htmlrep[$key] = hsc($value); 22576efd6d0SAndreas Gohr } 226abbf0890SAndreas Gohr } 227abbf0890SAndreas Gohr 228850dbf1fSAndreas Gohr // embed media from templates 229a89c75afSAndreas Gohr $html = preg_replace_callback( 230a89c75afSAndreas Gohr '/@MEDIA\(([^\)]+)\)@/', 231749c0023SAndreas Gohr array($this, 'autoEmbedCallBack'), $html 232a89c75afSAndreas Gohr ); 233850dbf1fSAndreas Gohr 2349ea45836SChristopher Smith // add default token replacements 2359ea45836SChristopher Smith $trep = array_merge($this->replacements['text'], (array)$textrep); 2369ea45836SChristopher Smith $hrep = array_merge($this->replacements['html'], (array)$htmlrep); 237abbf0890SAndreas Gohr 238abbf0890SAndreas Gohr // Apply replacements 239abbf0890SAndreas Gohr foreach($trep as $key => $substitution) { 240abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@', $substitution, $text); 241abbf0890SAndreas Gohr } 242abbf0890SAndreas Gohr foreach($hrep as $key => $substitution) { 243abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@', $substitution, $html); 244abbf0890SAndreas Gohr } 245abbf0890SAndreas Gohr 246abbf0890SAndreas Gohr $this->setHTML($html); 247abbf0890SAndreas Gohr $this->setText($text); 248abbf0890SAndreas Gohr } 249abbf0890SAndreas Gohr 250abbf0890SAndreas Gohr /** 251bb01c27cSAndreas Gohr * Set the HTML part of the mail 252bb01c27cSAndreas Gohr * 253bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 254abbf0890SAndreas Gohr * 255abbf0890SAndreas Gohr * You probably want to use setBody() instead 25642ea7f44SGerrit Uitslag * 25742ea7f44SGerrit Uitslag * @param string $html 258bb01c27cSAndreas Gohr */ 2591d045709SAndreas Gohr public function setHTML($html) { 260bb01c27cSAndreas Gohr $this->html = $html; 261bb01c27cSAndreas Gohr } 262bb01c27cSAndreas Gohr 263bb01c27cSAndreas Gohr /** 264bb01c27cSAndreas Gohr * Set the plain text part of the mail 265abbf0890SAndreas Gohr * 266abbf0890SAndreas Gohr * You probably want to use setBody() instead 26742ea7f44SGerrit Uitslag * 26842ea7f44SGerrit Uitslag * @param string $text 269bb01c27cSAndreas Gohr */ 2701d045709SAndreas Gohr public function setText($text) { 271bb01c27cSAndreas Gohr $this->text = $text; 272bb01c27cSAndreas Gohr } 273bb01c27cSAndreas Gohr 274bb01c27cSAndreas Gohr /** 275a36fc348SAndreas Gohr * Add the To: recipients 276a36fc348SAndreas Gohr * 2778c253612SGerrit Uitslag * @see cleanAddress 27859bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 279a36fc348SAndreas Gohr */ 280a36fc348SAndreas Gohr public function to($address) { 281a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 282a36fc348SAndreas Gohr } 283a36fc348SAndreas Gohr 284a36fc348SAndreas Gohr /** 285a36fc348SAndreas Gohr * Add the Cc: recipients 286a36fc348SAndreas Gohr * 2878c253612SGerrit Uitslag * @see cleanAddress 28859bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 289a36fc348SAndreas Gohr */ 290a36fc348SAndreas Gohr public function cc($address) { 291a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 292a36fc348SAndreas Gohr } 293a36fc348SAndreas Gohr 294a36fc348SAndreas Gohr /** 295a36fc348SAndreas Gohr * Add the Bcc: recipients 296a36fc348SAndreas Gohr * 2978c253612SGerrit Uitslag * @see cleanAddress 29859bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 299a36fc348SAndreas Gohr */ 300a36fc348SAndreas Gohr public function bcc($address) { 301a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 302a36fc348SAndreas Gohr } 303a36fc348SAndreas Gohr 304a36fc348SAndreas Gohr /** 305a36fc348SAndreas Gohr * Add the From: address 306a36fc348SAndreas Gohr * 307a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 308a36fc348SAndreas Gohr * to call this function 309a36fc348SAndreas Gohr * 3108c253612SGerrit Uitslag * @see cleanAddress 311a36fc348SAndreas Gohr * @param string $address from address 312a36fc348SAndreas Gohr */ 313a36fc348SAndreas Gohr public function from($address) { 314a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 315a36fc348SAndreas Gohr } 316a36fc348SAndreas Gohr 317a36fc348SAndreas Gohr /** 318a36fc348SAndreas Gohr * Add the mail's Subject: header 319a36fc348SAndreas Gohr * 320a36fc348SAndreas Gohr * @param string $subject the mail subject 321a36fc348SAndreas Gohr */ 322a36fc348SAndreas Gohr public function subject($subject) { 323a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 324a36fc348SAndreas Gohr } 325a36fc348SAndreas Gohr 326a36fc348SAndreas Gohr /** 3271d045709SAndreas Gohr * Sets an email address header with correct encoding 328bb01c27cSAndreas Gohr * 329bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 330bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 331bb01c27cSAndreas Gohr * 332bb01c27cSAndreas Gohr * Example: 3338c253612SGerrit Uitslag * cc("föö <foo@bar.com>, me@somewhere.com","TBcc"); 334bb01c27cSAndreas Gohr * 33542ea7f44SGerrit Uitslag * @param string|string[] $addresses Multiple adresses separated by commas or as array 33642ea7f44SGerrit Uitslag * @return false|string the prepared header (can contain multiple lines) 337bb01c27cSAndreas Gohr */ 338b6c97c70SAndreas Gohr public function cleanAddress($addresses) { 339bb01c27cSAndreas Gohr $headers = ''; 340b6c97c70SAndreas Gohr if(!is_array($addresses)){ 341b6c97c70SAndreas Gohr $addresses = explode(',', $addresses); 342b6c97c70SAndreas Gohr } 343b6c97c70SAndreas Gohr 344b6c97c70SAndreas Gohr foreach($addresses as $part) { 345b6c97c70SAndreas Gohr $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors 346bb01c27cSAndreas Gohr $part = trim($part); 347bb01c27cSAndreas Gohr 348bb01c27cSAndreas Gohr // parse address 349bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) { 350bb01c27cSAndreas Gohr $text = trim($matches[1]); 351bb01c27cSAndreas Gohr $addr = $matches[2]; 352bb01c27cSAndreas Gohr } else { 353bb01c27cSAndreas Gohr $addr = $part; 354bb01c27cSAndreas Gohr } 355bb01c27cSAndreas Gohr // skip empty ones 356bb01c27cSAndreas Gohr if(empty($addr)) { 357bb01c27cSAndreas Gohr continue; 358bb01c27cSAndreas Gohr } 359bb01c27cSAndreas Gohr 360bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 361bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)) { 36265cc1598SPhy msg(hsc("E-Mail address <$addr> is not ASCII"), -1); 363bb01c27cSAndreas Gohr continue; 364bb01c27cSAndreas Gohr } 365bb01c27cSAndreas Gohr 36664d23c16SAndreas Gohr if(!mail_isvalid($addr)) { 36765cc1598SPhy msg(hsc("E-Mail address <$addr> is not valid"), -1); 368bb01c27cSAndreas Gohr continue; 369bb01c27cSAndreas Gohr } 370bb01c27cSAndreas Gohr 371bb01c27cSAndreas Gohr // text was given 37230085ef3SYurii K if(!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652) 373bb01c27cSAndreas Gohr // add address quotes 374bb01c27cSAndreas Gohr $addr = "<$addr>"; 375bb01c27cSAndreas Gohr 376bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 377bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 378bb01c27cSAndreas Gohr $text = utf8_strip($text); 379bb01c27cSAndreas Gohr } 380bb01c27cSAndreas Gohr 381b6c97c70SAndreas Gohr if(strpos($text, ',') !== false || !utf8_isASCII($text)) { 382bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 383bb01c27cSAndreas Gohr } 384bb01c27cSAndreas Gohr } else { 385bb01c27cSAndreas Gohr $text = ''; 386bb01c27cSAndreas Gohr } 387bb01c27cSAndreas Gohr 388bb01c27cSAndreas Gohr // add to header comma seperated 389bb01c27cSAndreas Gohr if($headers != '') { 390bb01c27cSAndreas Gohr $headers .= ', '; 391bb01c27cSAndreas Gohr } 392bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 393bb01c27cSAndreas Gohr } 394bb01c27cSAndreas Gohr 395b6c97c70SAndreas Gohr $headers = trim($headers); 396bb01c27cSAndreas Gohr if(empty($headers)) return false; 397bb01c27cSAndreas Gohr 398bb01c27cSAndreas Gohr return $headers; 399bb01c27cSAndreas Gohr } 400bb01c27cSAndreas Gohr 401bb01c27cSAndreas Gohr 402bb01c27cSAndreas Gohr /** 403bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 404bb01c27cSAndreas Gohr * 405bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 40642ea7f44SGerrit Uitslag * 40742ea7f44SGerrit Uitslag * @return string mime multiparts 408bb01c27cSAndreas Gohr */ 409bb01c27cSAndreas Gohr protected function prepareAttachments() { 410bb01c27cSAndreas Gohr $mime = ''; 411bb01c27cSAndreas Gohr $part = 1; 412bb01c27cSAndreas Gohr // embedded attachments 413bb01c27cSAndreas Gohr foreach($this->attach as $media) { 414ce9d2cc8SAndreas Gohr $media['name'] = str_replace(':', '_', cleanID($media['name'], true)); 415ce9d2cc8SAndreas Gohr 416bb01c27cSAndreas Gohr // create content id 417bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 418bb01c27cSAndreas Gohr 419bb01c27cSAndreas Gohr // replace wildcards 420bb01c27cSAndreas Gohr if($media['embed']) { 421bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html); 422bb01c27cSAndreas Gohr } 423bb01c27cSAndreas Gohr 424bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 4251d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"'); 4261d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64'); 4271d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-ID',"<$cid>"); 428bb01c27cSAndreas Gohr if($media['embed']) { 4291d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']); 430bb01c27cSAndreas Gohr } else { 4311d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']); 432bb01c27cSAndreas Gohr } 433bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 434bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL); 435bb01c27cSAndreas Gohr 436bb01c27cSAndreas Gohr $part++; 437bb01c27cSAndreas Gohr } 438bb01c27cSAndreas Gohr return $mime; 439bb01c27cSAndreas Gohr } 440bb01c27cSAndreas Gohr 4411d045709SAndreas Gohr /** 4421d045709SAndreas Gohr * Build the body and handles multi part mails 4431d045709SAndreas Gohr * 4441d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4451d045709SAndreas Gohr * 4461d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4471d045709SAndreas Gohr */ 4481d045709SAndreas Gohr protected function prepareBody() { 4491d045709SAndreas Gohr 4502398a2b5SAndreas Gohr // no HTML mails allowed? remove HTML body 4512398a2b5SAndreas Gohr if(!$this->allowhtml) { 4522398a2b5SAndreas Gohr $this->html = ''; 4532398a2b5SAndreas Gohr } 4542398a2b5SAndreas Gohr 455bb01c27cSAndreas Gohr // check for body 456bb01c27cSAndreas Gohr if(!$this->text && !$this->html) { 457bb01c27cSAndreas Gohr return false; 458bb01c27cSAndreas Gohr } 459bb01c27cSAndreas Gohr 460bb01c27cSAndreas Gohr // add general headers 461bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 462bb01c27cSAndreas Gohr 4631d045709SAndreas Gohr $body = ''; 4641d045709SAndreas Gohr 465bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)) { // we can send a simple single part message 466bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 467bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 468be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 469bb01c27cSAndreas Gohr } else { // multi part it is 4701d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 471bb01c27cSAndreas Gohr 472bb01c27cSAndreas Gohr // prepare the attachments 473bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 474bb01c27cSAndreas Gohr 475bb01c27cSAndreas Gohr // do we have alternative text content? 476bb01c27cSAndreas Gohr if($this->text && $this->html) { 477a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 478a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 479bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4801d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4811d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 482bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 483be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 484bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 485a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 486d6e04b60SAndreas Gohr ' boundary="'.$this->boundary.'";'.MAILHEADER_EOL. 487d6e04b60SAndreas Gohr ' type="text/html"'.MAILHEADER_EOL; 488bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 489bb01c27cSAndreas Gohr } 490bb01c27cSAndreas Gohr 4911d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4921d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4931d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 494bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 495be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL); 496bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 497bb01c27cSAndreas Gohr $body .= $attachments; 498bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 499bb01c27cSAndreas Gohr 500bb01c27cSAndreas Gohr // close open multipart/alternative boundary 501bb01c27cSAndreas Gohr if($this->text && $this->html) { 502bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 503bb01c27cSAndreas Gohr } 504bb01c27cSAndreas Gohr } 505bb01c27cSAndreas Gohr 506bb01c27cSAndreas Gohr return $body; 507bb01c27cSAndreas Gohr } 508bb01c27cSAndreas Gohr 509bb01c27cSAndreas Gohr /** 510a36fc348SAndreas Gohr * Cleanup and encode the headers array 511a36fc348SAndreas Gohr */ 512a36fc348SAndreas Gohr protected function cleanHeaders() { 513a36fc348SAndreas Gohr global $conf; 514a36fc348SAndreas Gohr 515a36fc348SAndreas Gohr // clean up addresses 516a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 517acbf061cSGerrit Uitslag $addrs = array('To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'); 518a36fc348SAndreas Gohr foreach($addrs as $addr) { 519a36fc348SAndreas Gohr if(isset($this->headers[$addr])) { 520a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 521a36fc348SAndreas Gohr } 522a36fc348SAndreas Gohr } 523a36fc348SAndreas Gohr 52445992a63SAndreas Gohr if(isset($this->headers['Subject'])) { 525a36fc348SAndreas Gohr // add prefix to subject 52654f30755SAndreas Gohr if(empty($conf['mailprefix'])) { 5278a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 52854f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 52954f30755SAndreas Gohr } else { 5308a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 5318a215f09SAndreas Gohr } 5328a215f09SAndreas Gohr } else { 533a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 53454f30755SAndreas Gohr } 535a36fc348SAndreas Gohr $len = strlen($prefix); 53645992a63SAndreas Gohr if(substr($this->headers['Subject'], 0, $len) != $prefix) { 53745992a63SAndreas Gohr $this->headers['Subject'] = $prefix.' '.$this->headers['Subject']; 538a36fc348SAndreas Gohr } 539a36fc348SAndreas Gohr 540a36fc348SAndreas Gohr // encode subject 541a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 54245992a63SAndreas Gohr $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']); 54345992a63SAndreas Gohr $this->headers['Subject'] = utf8_strip($this->headers['Subject']); 544a36fc348SAndreas Gohr } 545a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])) { 54645992a63SAndreas Gohr $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 547a36fc348SAndreas Gohr } 548a36fc348SAndreas Gohr } 549a36fc348SAndreas Gohr 550a36fc348SAndreas Gohr } 5511d8036c2SAndreas Gohr 5521d8036c2SAndreas Gohr /** 5531d8036c2SAndreas Gohr * Returns a complete, EOL terminated header line, wraps it if necessary 5541d8036c2SAndreas Gohr * 55542ea7f44SGerrit Uitslag * @param string $key 55642ea7f44SGerrit Uitslag * @param string $val 55742ea7f44SGerrit Uitslag * @return string line 5581d8036c2SAndreas Gohr */ 5591d8036c2SAndreas Gohr protected function wrappedHeaderLine($key, $val){ 5601d8036c2SAndreas Gohr return wordwrap("$key: $val", 78, MAILHEADER_EOL.' ').MAILHEADER_EOL; 561a36fc348SAndreas Gohr } 562a36fc348SAndreas Gohr 563a36fc348SAndreas Gohr /** 564bb01c27cSAndreas Gohr * Create a string from the headers array 5651d045709SAndreas Gohr * 5661d045709SAndreas Gohr * @returns string the headers 567bb01c27cSAndreas Gohr */ 568bb01c27cSAndreas Gohr protected function prepareHeaders() { 569bb01c27cSAndreas Gohr $headers = ''; 570bb01c27cSAndreas Gohr foreach($this->headers as $key => $val) { 571749c0023SAndreas Gohr if ($val === '' || $val === null) continue; 5721d8036c2SAndreas Gohr $headers .= $this->wrappedHeaderLine($key, $val); 573bb01c27cSAndreas Gohr } 574bb01c27cSAndreas Gohr return $headers; 575bb01c27cSAndreas Gohr } 576bb01c27cSAndreas Gohr 577bb01c27cSAndreas Gohr /** 578bb01c27cSAndreas Gohr * return a full email with all headers 579bb01c27cSAndreas Gohr * 5801d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5811d045709SAndreas Gohr * used for MHT exports 5821d045709SAndreas Gohr * 5831d045709SAndreas Gohr * @return string the mail, false on errors 584bb01c27cSAndreas Gohr */ 585bb01c27cSAndreas Gohr public function dump() { 586a36fc348SAndreas Gohr $this->cleanHeaders(); 587bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5884d18e936SAndreas Gohr if($body === false) return false; 5891d045709SAndreas Gohr $headers = $this->prepareHeaders(); 590bb01c27cSAndreas Gohr 591bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 592bb01c27cSAndreas Gohr } 5931d045709SAndreas Gohr 5941d045709SAndreas Gohr /** 5959ea45836SChristopher Smith * Prepare default token replacement strings 5969ea45836SChristopher Smith * 5979ea45836SChristopher Smith * Populates the '$replacements' property. 5989ea45836SChristopher Smith * Should be called by the class constructor 5999ea45836SChristopher Smith */ 6009ea45836SChristopher Smith protected function prepareTokenReplacements() { 6019ea45836SChristopher Smith global $INFO; 6029ea45836SChristopher Smith global $conf; 6039ea45836SChristopher Smith /* @var Input $INPUT */ 6049ea45836SChristopher Smith global $INPUT; 6059ea45836SChristopher Smith global $lang; 6069ea45836SChristopher Smith 6079ea45836SChristopher Smith $ip = clientIP(); 6089ea45836SChristopher Smith $cip = gethostsbyaddrs($ip); 6099ea45836SChristopher Smith 6109ea45836SChristopher Smith $this->replacements['text'] = array( 6119ea45836SChristopher Smith 'DATE' => dformat(), 6129ea45836SChristopher Smith 'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'), 6139ea45836SChristopher Smith 'IPADDRESS' => $ip, 6149ea45836SChristopher Smith 'HOSTNAME' => $cip, 6159ea45836SChristopher Smith 'TITLE' => $conf['title'], 6169ea45836SChristopher Smith 'DOKUWIKIURL' => DOKU_URL, 6179ea45836SChristopher Smith 'USER' => $INPUT->server->str('REMOTE_USER'), 6189ea45836SChristopher Smith 'NAME' => $INFO['userinfo']['name'], 619774514c9SGerrit Uitslag 'MAIL' => $INFO['userinfo']['mail'] 6209ea45836SChristopher Smith ); 62164159a61SAndreas Gohr $signature = str_replace( 62264159a61SAndreas Gohr '@DOKUWIKIURL@', 62364159a61SAndreas Gohr $this->replacements['text']['DOKUWIKIURL'], 62464159a61SAndreas Gohr $lang['email_signature_text'] 62564159a61SAndreas Gohr ); 626774514c9SGerrit Uitslag $this->replacements['text']['EMAILSIGNATURE'] = "\n-- \n" . $signature . "\n"; 6279ea45836SChristopher Smith 6289ea45836SChristopher Smith $this->replacements['html'] = array( 6299ea45836SChristopher Smith 'DATE' => '<i>' . hsc(dformat()) . '</i>', 6309ea45836SChristopher Smith 'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')), 6319ea45836SChristopher Smith 'IPADDRESS' => '<code>' . hsc($ip) . '</code>', 6329ea45836SChristopher Smith 'HOSTNAME' => '<code>' . hsc($cip) . '</code>', 6339ea45836SChristopher Smith 'TITLE' => hsc($conf['title']), 6349ea45836SChristopher Smith 'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>', 6359ea45836SChristopher Smith 'USER' => hsc($INPUT->server->str('REMOTE_USER')), 6369ea45836SChristopher Smith 'NAME' => hsc($INFO['userinfo']['name']), 6379ea45836SChristopher Smith 'MAIL' => '<a href="mailto:"' . hsc($INFO['userinfo']['mail']) . '">' . 638774514c9SGerrit Uitslag hsc($INFO['userinfo']['mail']) . '</a>' 6399ea45836SChristopher Smith ); 640774514c9SGerrit Uitslag $signature = $lang['email_signature_text']; 641774514c9SGerrit Uitslag if(!empty($lang['email_signature_html'])) { 642774514c9SGerrit Uitslag $signature = $lang['email_signature_html']; 643774514c9SGerrit Uitslag } 644774514c9SGerrit Uitslag $signature = str_replace( 645774514c9SGerrit Uitslag array( 646774514c9SGerrit Uitslag '@DOKUWIKIURL@', 647774514c9SGerrit Uitslag "\n" 648774514c9SGerrit Uitslag ), 649774514c9SGerrit Uitslag array( 650774514c9SGerrit Uitslag $this->replacements['html']['DOKUWIKIURL'], 651774514c9SGerrit Uitslag '<br />' 652774514c9SGerrit Uitslag ), 653774514c9SGerrit Uitslag $signature 654774514c9SGerrit Uitslag ); 655774514c9SGerrit Uitslag $this->replacements['html']['EMAILSIGNATURE'] = $signature; 6569ea45836SChristopher Smith } 6579ea45836SChristopher Smith 6589ea45836SChristopher Smith /** 6591d045709SAndreas Gohr * Send the mail 6601d045709SAndreas Gohr * 6611d045709SAndreas Gohr * Call this after all data was set 6621d045709SAndreas Gohr * 66328d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 6641d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 6651d045709SAndreas Gohr */ 6661d045709SAndreas Gohr public function send() { 66728d2ad80SAndreas Gohr $success = false; 668a36fc348SAndreas Gohr 66928d2ad80SAndreas Gohr // prepare hook data 67028d2ad80SAndreas Gohr $data = array( 67128d2ad80SAndreas Gohr // pass the whole mail class to plugin 67228d2ad80SAndreas Gohr 'mail' => $this, 67328d2ad80SAndreas Gohr // pass references for backward compatibility 67428d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 67528d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 67628d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 67728d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 67828d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 67928d2ad80SAndreas Gohr 'body' => &$this->text, 680a89c75afSAndreas Gohr 'params' => &$this->sendparam, 68128d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 68228d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 68328d2ad80SAndreas Gohr 'success' => &$success, 68428d2ad80SAndreas Gohr ); 68528d2ad80SAndreas Gohr 68628d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 687*e1d9dcc8SAndreas Gohr $evt = new Event('MAIL_MESSAGE_SEND', $data); 68828d2ad80SAndreas Gohr if($evt->advise_before(true)) { 68928d2ad80SAndreas Gohr // clean up before using the headers 690a36fc348SAndreas Gohr $this->cleanHeaders(); 691a36fc348SAndreas Gohr 6921d045709SAndreas Gohr // any recipients? 6931d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 6941d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 695a89c75afSAndreas Gohr trim($this->headers['Bcc']) === '' 696a89c75afSAndreas Gohr ) return false; 6971d045709SAndreas Gohr 6981d045709SAndreas Gohr // The To: header is special 6996be717dbSMichael Hamann if(array_key_exists('To', $this->headers)) { 7006be717dbSMichael Hamann $to = (string)$this->headers['To']; 7011d045709SAndreas Gohr unset($this->headers['To']); 7021d045709SAndreas Gohr } else { 7031d045709SAndreas Gohr $to = ''; 7041d045709SAndreas Gohr } 7051d045709SAndreas Gohr 7061d045709SAndreas Gohr // so is the subject 7076be717dbSMichael Hamann if(array_key_exists('Subject', $this->headers)) { 7086be717dbSMichael Hamann $subject = (string)$this->headers['Subject']; 7091d045709SAndreas Gohr unset($this->headers['Subject']); 7101d045709SAndreas Gohr } else { 7111d045709SAndreas Gohr $subject = ''; 7121d045709SAndreas Gohr } 7131d045709SAndreas Gohr 7141d045709SAndreas Gohr // make the body 7151d045709SAndreas Gohr $body = $this->prepareBody(); 7164c89a7f6SAndreas Gohr if($body === false) return false; 7171d045709SAndreas Gohr 7181d045709SAndreas Gohr // cook the headers 7191d045709SAndreas Gohr $headers = $this->prepareHeaders(); 72028d2ad80SAndreas Gohr // add any headers set by legacy plugins 72128d2ad80SAndreas Gohr if(trim($data['headers'])) { 72228d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 72328d2ad80SAndreas Gohr } 7241d045709SAndreas Gohr 7251d045709SAndreas Gohr // send the thing 726749c0023SAndreas Gohr if($this->sendparam === null) { 72728d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers); 7281d045709SAndreas Gohr } else { 72928d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers, $this->sendparam); 7301d045709SAndreas Gohr } 7311d045709SAndreas Gohr } 73228d2ad80SAndreas Gohr // any AFTER actions? 73328d2ad80SAndreas Gohr $evt->advise_after(); 73428d2ad80SAndreas Gohr return $success; 73528d2ad80SAndreas Gohr } 736bb01c27cSAndreas Gohr} 737