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 12bb01c27cSAndreas Gohr// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?) 13bb01c27cSAndreas Gohr// think different 14bb01c27cSAndreas Gohrif(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL', "\n"); 15bb01c27cSAndreas Gohr#define('MAILHEADER_ASCIIONLY',1); 16bb01c27cSAndreas Gohr 17a89c75afSAndreas Gohr/** 18a89c75afSAndreas Gohr * Mail Handling 19a89c75afSAndreas Gohr */ 20bb01c27cSAndreas Gohrclass Mailer { 21bb01c27cSAndreas Gohr 22f41c79d7SAndreas Gohr protected $headers = array(); 23f41c79d7SAndreas Gohr protected $attach = array(); 24f41c79d7SAndreas Gohr protected $html = ''; 25f41c79d7SAndreas Gohr protected $text = ''; 26bb01c27cSAndreas Gohr 27f41c79d7SAndreas Gohr protected $boundary = ''; 28f41c79d7SAndreas Gohr protected $partid = ''; 29f41c79d7SAndreas Gohr protected $sendparam = null; 30bb01c27cSAndreas Gohr 31a89c75afSAndreas Gohr /** @var EmailAddressValidator */ 32f41c79d7SAndreas Gohr protected $validator = null; 33f41c79d7SAndreas Gohr protected $allowhtml = true; 341d045709SAndreas Gohr 35*9ea45836SChristopher Smith protected $replacements = array('text'=> array(), 'html' => array()); 36*9ea45836SChristopher Smith 371d045709SAndreas Gohr /** 381d045709SAndreas Gohr * Constructor 391d045709SAndreas Gohr * 40*9ea45836SChristopher 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); 48d6e04b60SAndreas Gohr if(strpos($server,'.') === false) $server = $server.'.localhost'; 491d045709SAndreas Gohr 501d8036c2SAndreas Gohr $this->partid = substr(md5(uniqid(rand(), true)),0, 8).'@'.$server; 51b3577cf9SAndreas Gohr $this->boundary = '__________'.md5(uniqid(rand(), true)); 529f3eca0bSAndreas Gohr 539f3eca0bSAndreas Gohr $listid = join('.', 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 596a1f928fSAndreas Gohr $this->setHeader('X-Mailer', 'DokuWiki'); 60585bf44eSChristopher Smith $this->setHeader('X-DokuWiki-User', $INPUT->server->str('REMOTE_USER')); 619f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Title', $conf['title']); 629f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Server', $server); 639f3eca0bSAndreas Gohr $this->setHeader('X-Auto-Response-Suppress', 'OOF'); 649f3eca0bSAndreas Gohr $this->setHeader('List-Id', $conf['title'].' <'.$listid.'>'); 65d6e04b60SAndreas Gohr $this->setHeader('Date', date('r'), false); 66*9ea45836SChristopher Smith 67*9ea45836SChristopher Smith $this->prepareTokenReplacements(); 68bb01c27cSAndreas Gohr } 69bb01c27cSAndreas Gohr 70bb01c27cSAndreas Gohr /** 71bb01c27cSAndreas Gohr * Attach a file 72bb01c27cSAndreas Gohr * 73a89c75afSAndreas Gohr * @param string $path Path to the file to attach 74a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 75a89c75afSAndreas Gohr * @param string $name The filename to use 76a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 77bb01c27cSAndreas Gohr */ 78bb01c27cSAndreas Gohr public function attachFile($path, $mime, $name = '', $embed = '') { 79bb01c27cSAndreas Gohr if(!$name) { 803009a773SAndreas Gohr $name = utf8_basename($path); 81bb01c27cSAndreas Gohr } 82bb01c27cSAndreas Gohr 83bb01c27cSAndreas Gohr $this->attach[] = array( 84bb01c27cSAndreas Gohr 'data' => file_get_contents($path), 85bb01c27cSAndreas Gohr 'mime' => $mime, 86bb01c27cSAndreas Gohr 'name' => $name, 87bb01c27cSAndreas Gohr 'embed' => $embed 88bb01c27cSAndreas Gohr ); 89bb01c27cSAndreas Gohr } 90bb01c27cSAndreas Gohr 91bb01c27cSAndreas Gohr /** 92bb01c27cSAndreas Gohr * Attach a file 93bb01c27cSAndreas Gohr * 94a89c75afSAndreas Gohr * @param string $data The file contents to attach 95a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 96a89c75afSAndreas Gohr * @param string $name The filename to use 97a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 98bb01c27cSAndreas Gohr */ 99bb01c27cSAndreas Gohr public function attachContent($data, $mime, $name = '', $embed = '') { 100bb01c27cSAndreas Gohr if(!$name) { 101a89c75afSAndreas Gohr list(, $ext) = explode('/', $mime); 102bb01c27cSAndreas Gohr $name = count($this->attach).".$ext"; 103bb01c27cSAndreas Gohr } 104bb01c27cSAndreas Gohr 105bb01c27cSAndreas Gohr $this->attach[] = array( 106bb01c27cSAndreas Gohr 'data' => $data, 107bb01c27cSAndreas Gohr 'mime' => $mime, 108bb01c27cSAndreas Gohr 'name' => $name, 109bb01c27cSAndreas Gohr 'embed' => $embed 110bb01c27cSAndreas Gohr ); 111bb01c27cSAndreas Gohr } 112bb01c27cSAndreas Gohr 113bb01c27cSAndreas Gohr /** 114850dbf1fSAndreas Gohr * Callback function to automatically embed images referenced in HTML templates 11542ea7f44SGerrit Uitslag * 11642ea7f44SGerrit Uitslag * @param array $matches 11742ea7f44SGerrit Uitslag * @return string placeholder 118850dbf1fSAndreas Gohr */ 119850dbf1fSAndreas Gohr protected function autoembed_cb($matches) { 120850dbf1fSAndreas Gohr static $embeds = 0; 121850dbf1fSAndreas Gohr $embeds++; 122850dbf1fSAndreas Gohr 123850dbf1fSAndreas Gohr // get file and mime type 124850dbf1fSAndreas Gohr $media = cleanID($matches[1]); 125a89c75afSAndreas Gohr list(, $mime) = mimetype($media); 126850dbf1fSAndreas Gohr $file = mediaFN($media); 127850dbf1fSAndreas Gohr if(!file_exists($file)) return $matches[0]; //bad reference, keep as is 128850dbf1fSAndreas Gohr 129850dbf1fSAndreas Gohr // attach it and set placeholder 130850dbf1fSAndreas Gohr $this->attachFile($file, $mime, '', 'autoembed'.$embeds); 131850dbf1fSAndreas Gohr return '%%autoembed'.$embeds.'%%'; 132850dbf1fSAndreas Gohr } 133850dbf1fSAndreas Gohr 134850dbf1fSAndreas Gohr /** 1351d045709SAndreas Gohr * Add an arbitrary header to the mail 1361d045709SAndreas Gohr * 137a36fc348SAndreas Gohr * If an empy value is passed, the header is removed 138a36fc348SAndreas Gohr * 1391d045709SAndreas Gohr * @param string $header the header name (no trailing colon!) 14059bc3b48SGerrit Uitslag * @param string|string[] $value the value of the header 1411d045709SAndreas Gohr * @param bool $clean remove all non-ASCII chars and line feeds? 1421d045709SAndreas Gohr */ 1431d045709SAndreas Gohr public function setHeader($header, $value, $clean = true) { 1449f3eca0bSAndreas Gohr $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing 1451d045709SAndreas Gohr if($clean) { 146578b2c23SAndreas Gohr $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header); 147578b2c23SAndreas Gohr $value = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value); 1481d045709SAndreas Gohr } 149a36fc348SAndreas Gohr 150a36fc348SAndreas Gohr // empty value deletes 151b6c97c70SAndreas Gohr if(is_array($value)){ 152b6c97c70SAndreas Gohr $value = array_map('trim', $value); 153b6c97c70SAndreas Gohr $value = array_filter($value); 154b6c97c70SAndreas Gohr if(!$value) $value = ''; 155b6c97c70SAndreas Gohr }else{ 156a36fc348SAndreas Gohr $value = trim($value); 157b6c97c70SAndreas Gohr } 158a36fc348SAndreas Gohr if($value === '') { 159a36fc348SAndreas Gohr if(isset($this->headers[$header])) unset($this->headers[$header]); 160a36fc348SAndreas Gohr } else { 1611d045709SAndreas Gohr $this->headers[$header] = $value; 1621d045709SAndreas Gohr } 163a36fc348SAndreas Gohr } 1641d045709SAndreas Gohr 1651d045709SAndreas Gohr /** 1661d045709SAndreas Gohr * Set additional parameters to be passed to sendmail 1671d045709SAndreas Gohr * 1681d045709SAndreas Gohr * Whatever is set here is directly passed to PHP's mail() command as last 1691d045709SAndreas Gohr * parameter. Depending on the PHP setup this might break mailing alltogether 17042ea7f44SGerrit Uitslag * 17142ea7f44SGerrit Uitslag * @param string $param 1721d045709SAndreas Gohr */ 1731d045709SAndreas Gohr public function setParameters($param) { 1741d045709SAndreas Gohr $this->sendparam = $param; 1751d045709SAndreas Gohr } 1761d045709SAndreas Gohr 1771d045709SAndreas Gohr /** 178abbf0890SAndreas Gohr * Set the text and HTML body and apply replacements 179abbf0890SAndreas Gohr * 180abbf0890SAndreas Gohr * This function applies a whole bunch of default replacements in addition 181abbf0890SAndreas Gohr * to the ones specidifed as parameters 182abbf0890SAndreas Gohr * 183abbf0890SAndreas Gohr * If you pass the HTML part or HTML replacements yourself you have to make 184abbf0890SAndreas Gohr * sure you encode all HTML special chars correctly 185abbf0890SAndreas Gohr * 186abbf0890SAndreas Gohr * @param string $text plain text body 187abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 188abbf0890SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep 18959bc3b48SGerrit Uitslag * @param string $html the HTML body, leave null to create it from $text 190f08086ecSAndreas Gohr * @param bool $wrap wrap the HTML in the default header/Footer 191abbf0890SAndreas Gohr */ 192f08086ecSAndreas Gohr public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true) { 193585bf44eSChristopher Smith 19476efd6d0SAndreas Gohr $htmlrep = (array)$htmlrep; 19576efd6d0SAndreas Gohr $textrep = (array)$textrep; 196abbf0890SAndreas Gohr 197abbf0890SAndreas Gohr // create HTML from text if not given 198abbf0890SAndreas Gohr if(is_null($html)) { 199ba9c057bSAndreas Gohr $html = $text; 200ba9c057bSAndreas Gohr $html = hsc($html); 201ba2c2f17Sfurun $html = preg_replace('/^----+$/m', '<hr >', $html); 202ba9c057bSAndreas Gohr $html = nl2br($html); 203abbf0890SAndreas Gohr } 204f08086ecSAndreas Gohr if($wrap) { 2053819cafdSfurun $wrap = rawLocale('mailwrap', 'html'); 2063819cafdSfurun $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature 2073819cafdSfurun $html = str_replace('@EMAILSIGNATURE@', '', $html); //strip @EMAILSIGNATURE@ 2083819cafdSfurun $html = str_replace('@HTMLBODY@', $html, $wrap); 209f08086ecSAndreas Gohr } 210f08086ecSAndreas Gohr 2113819cafdSfurun if(strpos($text, '@EMAILSIGNATURE@') === false) { 212*9ea45836SChristopher Smith $text .= '@EMAILSIGNATURE@'; 2133819cafdSfurun } 214ba2c2f17Sfurun 21576efd6d0SAndreas Gohr // copy over all replacements missing for HTML (autolink URLs) 21676efd6d0SAndreas Gohr foreach($textrep as $key => $value) { 21776efd6d0SAndreas Gohr if(isset($htmlrep[$key])) continue; 2183e7e6067SKlap-in if(media_isexternal($value)) { 21976efd6d0SAndreas Gohr $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>'; 22076efd6d0SAndreas Gohr } else { 22176efd6d0SAndreas Gohr $htmlrep[$key] = hsc($value); 22276efd6d0SAndreas Gohr } 223abbf0890SAndreas Gohr } 224abbf0890SAndreas Gohr 225850dbf1fSAndreas Gohr // embed media from templates 226a89c75afSAndreas Gohr $html = preg_replace_callback( 227a89c75afSAndreas Gohr '/@MEDIA\(([^\)]+)\)@/', 228a89c75afSAndreas Gohr array($this, 'autoembed_cb'), $html 229a89c75afSAndreas Gohr ); 230850dbf1fSAndreas Gohr 231*9ea45836SChristopher Smith // add default token replacements 232*9ea45836SChristopher Smith $trep = array_merge($this->replacements['text'], (array)$textrep); 233*9ea45836SChristopher Smith $hrep = array_merge($this->replacements['html'], (array)$htmlrep); 234abbf0890SAndreas Gohr 235abbf0890SAndreas Gohr // Apply replacements 236abbf0890SAndreas Gohr foreach($trep as $key => $substitution) { 237abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@', $substitution, $text); 238abbf0890SAndreas Gohr } 239abbf0890SAndreas Gohr foreach($hrep as $key => $substitution) { 240abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@', $substitution, $html); 241abbf0890SAndreas Gohr } 242abbf0890SAndreas Gohr 243abbf0890SAndreas Gohr $this->setHTML($html); 244abbf0890SAndreas Gohr $this->setText($text); 245abbf0890SAndreas Gohr } 246abbf0890SAndreas Gohr 247abbf0890SAndreas Gohr /** 248bb01c27cSAndreas Gohr * Set the HTML part of the mail 249bb01c27cSAndreas Gohr * 250bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 251abbf0890SAndreas Gohr * 252abbf0890SAndreas Gohr * You probably want to use setBody() instead 25342ea7f44SGerrit Uitslag * 25442ea7f44SGerrit Uitslag * @param string $html 255bb01c27cSAndreas Gohr */ 2561d045709SAndreas Gohr public function setHTML($html) { 257bb01c27cSAndreas Gohr $this->html = $html; 258bb01c27cSAndreas Gohr } 259bb01c27cSAndreas Gohr 260bb01c27cSAndreas Gohr /** 261bb01c27cSAndreas Gohr * Set the plain text part of the mail 262abbf0890SAndreas Gohr * 263abbf0890SAndreas Gohr * You probably want to use setBody() instead 26442ea7f44SGerrit Uitslag * 26542ea7f44SGerrit Uitslag * @param string $text 266bb01c27cSAndreas Gohr */ 2671d045709SAndreas Gohr public function setText($text) { 268bb01c27cSAndreas Gohr $this->text = $text; 269bb01c27cSAndreas Gohr } 270bb01c27cSAndreas Gohr 271bb01c27cSAndreas Gohr /** 272a36fc348SAndreas Gohr * Add the To: recipients 273a36fc348SAndreas Gohr * 2748c253612SGerrit Uitslag * @see cleanAddress 27559bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 276a36fc348SAndreas Gohr */ 277a36fc348SAndreas Gohr public function to($address) { 278a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 279a36fc348SAndreas Gohr } 280a36fc348SAndreas Gohr 281a36fc348SAndreas Gohr /** 282a36fc348SAndreas Gohr * Add the Cc: recipients 283a36fc348SAndreas Gohr * 2848c253612SGerrit Uitslag * @see cleanAddress 28559bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 286a36fc348SAndreas Gohr */ 287a36fc348SAndreas Gohr public function cc($address) { 288a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 289a36fc348SAndreas Gohr } 290a36fc348SAndreas Gohr 291a36fc348SAndreas Gohr /** 292a36fc348SAndreas Gohr * Add the Bcc: recipients 293a36fc348SAndreas Gohr * 2948c253612SGerrit Uitslag * @see cleanAddress 29559bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 296a36fc348SAndreas Gohr */ 297a36fc348SAndreas Gohr public function bcc($address) { 298a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 299a36fc348SAndreas Gohr } 300a36fc348SAndreas Gohr 301a36fc348SAndreas Gohr /** 302a36fc348SAndreas Gohr * Add the From: address 303a36fc348SAndreas Gohr * 304a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 305a36fc348SAndreas Gohr * to call this function 306a36fc348SAndreas Gohr * 3078c253612SGerrit Uitslag * @see cleanAddress 308a36fc348SAndreas Gohr * @param string $address from address 309a36fc348SAndreas Gohr */ 310a36fc348SAndreas Gohr public function from($address) { 311a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 312a36fc348SAndreas Gohr } 313a36fc348SAndreas Gohr 314a36fc348SAndreas Gohr /** 315a36fc348SAndreas Gohr * Add the mail's Subject: header 316a36fc348SAndreas Gohr * 317a36fc348SAndreas Gohr * @param string $subject the mail subject 318a36fc348SAndreas Gohr */ 319a36fc348SAndreas Gohr public function subject($subject) { 320a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 321a36fc348SAndreas Gohr } 322a36fc348SAndreas Gohr 323a36fc348SAndreas Gohr /** 3241d045709SAndreas Gohr * Sets an email address header with correct encoding 325bb01c27cSAndreas Gohr * 326bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 327bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 328bb01c27cSAndreas Gohr * 329bb01c27cSAndreas Gohr * Example: 3308c253612SGerrit Uitslag * cc("föö <foo@bar.com>, me@somewhere.com","TBcc"); 331bb01c27cSAndreas Gohr * 33242ea7f44SGerrit Uitslag * @param string|string[] $addresses Multiple adresses separated by commas or as array 33342ea7f44SGerrit Uitslag * @return false|string the prepared header (can contain multiple lines) 334bb01c27cSAndreas Gohr */ 335b6c97c70SAndreas Gohr public function cleanAddress($addresses) { 336bb01c27cSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 337bb01c27cSAndreas Gohr $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 338bb01c27cSAndreas Gohr 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)) { 362bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"), -1); 363bb01c27cSAndreas Gohr continue; 364bb01c27cSAndreas Gohr } 365bb01c27cSAndreas Gohr 3661d045709SAndreas Gohr if(is_null($this->validator)) { 3671d045709SAndreas Gohr $this->validator = new EmailAddressValidator(); 3681d045709SAndreas Gohr $this->validator->allowLocalAddresses = true; 3691d045709SAndreas Gohr } 3701d045709SAndreas Gohr if(!$this->validator->check_email_address($addr)) { 371bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"), -1); 372bb01c27cSAndreas Gohr continue; 373bb01c27cSAndreas Gohr } 374bb01c27cSAndreas Gohr 375bb01c27cSAndreas Gohr // text was given 376bb01c27cSAndreas Gohr if(!empty($text) && $names) { 377bb01c27cSAndreas Gohr // add address quotes 378bb01c27cSAndreas Gohr $addr = "<$addr>"; 379bb01c27cSAndreas Gohr 380bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 381bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 382bb01c27cSAndreas Gohr $text = utf8_strip($text); 383bb01c27cSAndreas Gohr } 384bb01c27cSAndreas Gohr 385b6c97c70SAndreas Gohr if(strpos($text, ',') !== false || !utf8_isASCII($text)) { 386bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 387bb01c27cSAndreas Gohr } 388bb01c27cSAndreas Gohr } else { 389bb01c27cSAndreas Gohr $text = ''; 390bb01c27cSAndreas Gohr } 391bb01c27cSAndreas Gohr 392bb01c27cSAndreas Gohr // add to header comma seperated 393bb01c27cSAndreas Gohr if($headers != '') { 394bb01c27cSAndreas Gohr $headers .= ', '; 395bb01c27cSAndreas Gohr } 396bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 397bb01c27cSAndreas Gohr } 398bb01c27cSAndreas Gohr 399b6c97c70SAndreas Gohr $headers = trim($headers); 400bb01c27cSAndreas Gohr if(empty($headers)) return false; 401bb01c27cSAndreas Gohr 402bb01c27cSAndreas Gohr return $headers; 403bb01c27cSAndreas Gohr } 404bb01c27cSAndreas Gohr 405bb01c27cSAndreas Gohr 406bb01c27cSAndreas Gohr /** 407bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 408bb01c27cSAndreas Gohr * 409bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 41042ea7f44SGerrit Uitslag * 41142ea7f44SGerrit Uitslag * @return string mime multiparts 412bb01c27cSAndreas Gohr */ 413bb01c27cSAndreas Gohr protected function prepareAttachments() { 414bb01c27cSAndreas Gohr $mime = ''; 415bb01c27cSAndreas Gohr $part = 1; 416bb01c27cSAndreas Gohr // embedded attachments 417bb01c27cSAndreas Gohr foreach($this->attach as $media) { 418ce9d2cc8SAndreas Gohr $media['name'] = str_replace(':', '_', cleanID($media['name'], true)); 419ce9d2cc8SAndreas Gohr 420bb01c27cSAndreas Gohr // create content id 421bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 422bb01c27cSAndreas Gohr 423bb01c27cSAndreas Gohr // replace wildcards 424bb01c27cSAndreas Gohr if($media['embed']) { 425bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html); 426bb01c27cSAndreas Gohr } 427bb01c27cSAndreas Gohr 428bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 4291d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"'); 4301d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64'); 4311d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-ID',"<$cid>"); 432bb01c27cSAndreas Gohr if($media['embed']) { 4331d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']); 434bb01c27cSAndreas Gohr } else { 4351d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']); 436bb01c27cSAndreas Gohr } 437bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 438bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL); 439bb01c27cSAndreas Gohr 440bb01c27cSAndreas Gohr $part++; 441bb01c27cSAndreas Gohr } 442bb01c27cSAndreas Gohr return $mime; 443bb01c27cSAndreas Gohr } 444bb01c27cSAndreas Gohr 4451d045709SAndreas Gohr /** 4461d045709SAndreas Gohr * Build the body and handles multi part mails 4471d045709SAndreas Gohr * 4481d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4491d045709SAndreas Gohr * 4501d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4511d045709SAndreas Gohr */ 4521d045709SAndreas Gohr protected function prepareBody() { 4531d045709SAndreas Gohr 4542398a2b5SAndreas Gohr // no HTML mails allowed? remove HTML body 4552398a2b5SAndreas Gohr if(!$this->allowhtml) { 4562398a2b5SAndreas Gohr $this->html = ''; 4572398a2b5SAndreas Gohr } 4582398a2b5SAndreas Gohr 459bb01c27cSAndreas Gohr // check for body 460bb01c27cSAndreas Gohr if(!$this->text && !$this->html) { 461bb01c27cSAndreas Gohr return false; 462bb01c27cSAndreas Gohr } 463bb01c27cSAndreas Gohr 464bb01c27cSAndreas Gohr // add general headers 465bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 466bb01c27cSAndreas Gohr 4671d045709SAndreas Gohr $body = ''; 4681d045709SAndreas Gohr 469bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)) { // we can send a simple single part message 470bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 471bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 472be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 473bb01c27cSAndreas Gohr } else { // multi part it is 4741d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 475bb01c27cSAndreas Gohr 476bb01c27cSAndreas Gohr // prepare the attachments 477bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 478bb01c27cSAndreas Gohr 479bb01c27cSAndreas Gohr // do we have alternative text content? 480bb01c27cSAndreas Gohr if($this->text && $this->html) { 481a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 482a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 483bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4841d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4851d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 486bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 487be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 488bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 489a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 490d6e04b60SAndreas Gohr ' boundary="'.$this->boundary.'";'.MAILHEADER_EOL. 491d6e04b60SAndreas Gohr ' type="text/html"'.MAILHEADER_EOL; 492bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 493bb01c27cSAndreas Gohr } 494bb01c27cSAndreas Gohr 4951d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4961d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4971d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 498bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 499be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL); 500bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 501bb01c27cSAndreas Gohr $body .= $attachments; 502bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 503bb01c27cSAndreas Gohr 504bb01c27cSAndreas Gohr // close open multipart/alternative boundary 505bb01c27cSAndreas Gohr if($this->text && $this->html) { 506bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 507bb01c27cSAndreas Gohr } 508bb01c27cSAndreas Gohr } 509bb01c27cSAndreas Gohr 510bb01c27cSAndreas Gohr return $body; 511bb01c27cSAndreas Gohr } 512bb01c27cSAndreas Gohr 513bb01c27cSAndreas Gohr /** 514a36fc348SAndreas Gohr * Cleanup and encode the headers array 515a36fc348SAndreas Gohr */ 516a36fc348SAndreas Gohr protected function cleanHeaders() { 517a36fc348SAndreas Gohr global $conf; 518a36fc348SAndreas Gohr 519a36fc348SAndreas Gohr // clean up addresses 520a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 521acbf061cSGerrit Uitslag $addrs = array('To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'); 522a36fc348SAndreas Gohr foreach($addrs as $addr) { 523a36fc348SAndreas Gohr if(isset($this->headers[$addr])) { 524a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 525a36fc348SAndreas Gohr } 526a36fc348SAndreas Gohr } 527a36fc348SAndreas Gohr 52845992a63SAndreas Gohr if(isset($this->headers['Subject'])) { 529a36fc348SAndreas Gohr // add prefix to subject 53054f30755SAndreas Gohr if(empty($conf['mailprefix'])) { 5318a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 53254f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 53354f30755SAndreas Gohr } else { 5348a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 5358a215f09SAndreas Gohr } 5368a215f09SAndreas Gohr } else { 537a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 53854f30755SAndreas Gohr } 539a36fc348SAndreas Gohr $len = strlen($prefix); 54045992a63SAndreas Gohr if(substr($this->headers['Subject'], 0, $len) != $prefix) { 54145992a63SAndreas Gohr $this->headers['Subject'] = $prefix.' '.$this->headers['Subject']; 542a36fc348SAndreas Gohr } 543a36fc348SAndreas Gohr 544a36fc348SAndreas Gohr // encode subject 545a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 54645992a63SAndreas Gohr $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']); 54745992a63SAndreas Gohr $this->headers['Subject'] = utf8_strip($this->headers['Subject']); 548a36fc348SAndreas Gohr } 549a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])) { 55045992a63SAndreas Gohr $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 551a36fc348SAndreas Gohr } 552a36fc348SAndreas Gohr } 553a36fc348SAndreas Gohr 554a36fc348SAndreas Gohr } 5551d8036c2SAndreas Gohr 5561d8036c2SAndreas Gohr /** 5571d8036c2SAndreas Gohr * Returns a complete, EOL terminated header line, wraps it if necessary 5581d8036c2SAndreas Gohr * 55942ea7f44SGerrit Uitslag * @param string $key 56042ea7f44SGerrit Uitslag * @param string $val 56142ea7f44SGerrit Uitslag * @return string line 5621d8036c2SAndreas Gohr */ 5631d8036c2SAndreas Gohr protected function wrappedHeaderLine($key, $val){ 5641d8036c2SAndreas Gohr return wordwrap("$key: $val", 78, MAILHEADER_EOL.' ').MAILHEADER_EOL; 565a36fc348SAndreas Gohr } 566a36fc348SAndreas Gohr 567a36fc348SAndreas Gohr /** 568bb01c27cSAndreas Gohr * Create a string from the headers array 5691d045709SAndreas Gohr * 5701d045709SAndreas Gohr * @returns string the headers 571bb01c27cSAndreas Gohr */ 572bb01c27cSAndreas Gohr protected function prepareHeaders() { 573bb01c27cSAndreas Gohr $headers = ''; 574bb01c27cSAndreas Gohr foreach($this->headers as $key => $val) { 5756be717dbSMichael Hamann if ($val === '' || is_null($val)) continue; 5761d8036c2SAndreas Gohr $headers .= $this->wrappedHeaderLine($key, $val); 577bb01c27cSAndreas Gohr } 578bb01c27cSAndreas Gohr return $headers; 579bb01c27cSAndreas Gohr } 580bb01c27cSAndreas Gohr 581bb01c27cSAndreas Gohr /** 582bb01c27cSAndreas Gohr * return a full email with all headers 583bb01c27cSAndreas Gohr * 5841d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5851d045709SAndreas Gohr * used for MHT exports 5861d045709SAndreas Gohr * 5871d045709SAndreas Gohr * @return string the mail, false on errors 588bb01c27cSAndreas Gohr */ 589bb01c27cSAndreas Gohr public function dump() { 590a36fc348SAndreas Gohr $this->cleanHeaders(); 591bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5924d18e936SAndreas Gohr if($body === false) return false; 5931d045709SAndreas Gohr $headers = $this->prepareHeaders(); 594bb01c27cSAndreas Gohr 595bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 596bb01c27cSAndreas Gohr } 5971d045709SAndreas Gohr 5981d045709SAndreas Gohr /** 599*9ea45836SChristopher Smith * Prepare default token replacement strings 600*9ea45836SChristopher Smith * 601*9ea45836SChristopher Smith * Populates the '$replacements' property. 602*9ea45836SChristopher Smith * Should be called by the class constructor 603*9ea45836SChristopher Smith */ 604*9ea45836SChristopher Smith protected function prepareTokenReplacements() { 605*9ea45836SChristopher Smith global $INFO; 606*9ea45836SChristopher Smith global $conf; 607*9ea45836SChristopher Smith /* @var Input $INPUT */ 608*9ea45836SChristopher Smith global $INPUT; 609*9ea45836SChristopher Smith global $lang; 610*9ea45836SChristopher Smith 611*9ea45836SChristopher Smith $ip = clientIP(); 612*9ea45836SChristopher Smith $cip = gethostsbyaddrs($ip); 613*9ea45836SChristopher Smith 614*9ea45836SChristopher Smith $this->replacements['text'] = array( 615*9ea45836SChristopher Smith 'DATE' => dformat(), 616*9ea45836SChristopher Smith 'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'), 617*9ea45836SChristopher Smith 'IPADDRESS' => $ip, 618*9ea45836SChristopher Smith 'HOSTNAME' => $cip, 619*9ea45836SChristopher Smith 'TITLE' => $conf['title'], 620*9ea45836SChristopher Smith 'DOKUWIKIURL' => DOKU_URL, 621*9ea45836SChristopher Smith 'USER' => $INPUT->server->str('REMOTE_USER'), 622*9ea45836SChristopher Smith 'NAME' => $INFO['userinfo']['name'], 623*9ea45836SChristopher Smith 'MAIL' => $INFO['userinfo']['mail'], 624*9ea45836SChristopher Smith // per RFC2045 6.8, "text line breaks must be converted into CRLF sequences prior to base64 encoding.", so we use \r\n 625*9ea45836SChristopher Smith 'EMAILSIGNATURE' => "\r\n-- \r\n" . $lang['email_signature'] . ":\r\n" . DOKU_URL . "\r\n", 626*9ea45836SChristopher Smith ); 627*9ea45836SChristopher Smith 628*9ea45836SChristopher Smith $this->replacements['html'] = array( 629*9ea45836SChristopher Smith 'DATE' => '<i>' . hsc(dformat()) . '</i>', 630*9ea45836SChristopher Smith 'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')), 631*9ea45836SChristopher Smith 'IPADDRESS' => '<code>' . hsc($ip) . '</code>', 632*9ea45836SChristopher Smith 'HOSTNAME' => '<code>' . hsc($cip) . '</code>', 633*9ea45836SChristopher Smith 'TITLE' => hsc($conf['title']), 634*9ea45836SChristopher Smith 'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>', 635*9ea45836SChristopher Smith 'USER' => hsc($INPUT->server->str('REMOTE_USER')), 636*9ea45836SChristopher Smith 'NAME' => hsc($INFO['userinfo']['name']), 637*9ea45836SChristopher Smith 'MAIL' => '<a href="mailto:"' . hsc($INFO['userinfo']['mail']) . '">' . 638*9ea45836SChristopher Smith hsc($INFO['userinfo']['mail']) . '</a>', 639*9ea45836SChristopher Smith 'EMAILSIGNATURE' => hsc($lang['email_signature']) . ':<br />' . 640*9ea45836SChristopher Smith '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>', 641*9ea45836SChristopher Smith ); 642*9ea45836SChristopher Smith } 643*9ea45836SChristopher Smith 644*9ea45836SChristopher Smith /** 6451d045709SAndreas Gohr * Send the mail 6461d045709SAndreas Gohr * 6471d045709SAndreas Gohr * Call this after all data was set 6481d045709SAndreas Gohr * 64928d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 6501d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 6511d045709SAndreas Gohr */ 6521d045709SAndreas Gohr public function send() { 65328d2ad80SAndreas Gohr $success = false; 654a36fc348SAndreas Gohr 65528d2ad80SAndreas Gohr // prepare hook data 65628d2ad80SAndreas Gohr $data = array( 65728d2ad80SAndreas Gohr // pass the whole mail class to plugin 65828d2ad80SAndreas Gohr 'mail' => $this, 65928d2ad80SAndreas Gohr // pass references for backward compatibility 66028d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 66128d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 66228d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 66328d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 66428d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 66528d2ad80SAndreas Gohr 'body' => &$this->text, 666a89c75afSAndreas Gohr 'params' => &$this->sendparam, 66728d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 66828d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 66928d2ad80SAndreas Gohr 'success' => &$success, 67028d2ad80SAndreas Gohr ); 67128d2ad80SAndreas Gohr 67228d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 67328d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 67428d2ad80SAndreas Gohr if($evt->advise_before(true)) { 67528d2ad80SAndreas Gohr // clean up before using the headers 676a36fc348SAndreas Gohr $this->cleanHeaders(); 677a36fc348SAndreas Gohr 6781d045709SAndreas Gohr // any recipients? 6791d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 6801d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 681a89c75afSAndreas Gohr trim($this->headers['Bcc']) === '' 682a89c75afSAndreas Gohr ) return false; 6831d045709SAndreas Gohr 6841d045709SAndreas Gohr // The To: header is special 6856be717dbSMichael Hamann if(array_key_exists('To', $this->headers)) { 6866be717dbSMichael Hamann $to = (string)$this->headers['To']; 6871d045709SAndreas Gohr unset($this->headers['To']); 6881d045709SAndreas Gohr } else { 6891d045709SAndreas Gohr $to = ''; 6901d045709SAndreas Gohr } 6911d045709SAndreas Gohr 6921d045709SAndreas Gohr // so is the subject 6936be717dbSMichael Hamann if(array_key_exists('Subject', $this->headers)) { 6946be717dbSMichael Hamann $subject = (string)$this->headers['Subject']; 6951d045709SAndreas Gohr unset($this->headers['Subject']); 6961d045709SAndreas Gohr } else { 6971d045709SAndreas Gohr $subject = ''; 6981d045709SAndreas Gohr } 6991d045709SAndreas Gohr 7001d045709SAndreas Gohr // make the body 7011d045709SAndreas Gohr $body = $this->prepareBody(); 7024c89a7f6SAndreas Gohr if($body === false) return false; 7031d045709SAndreas Gohr 7041d045709SAndreas Gohr // cook the headers 7051d045709SAndreas Gohr $headers = $this->prepareHeaders(); 70628d2ad80SAndreas Gohr // add any headers set by legacy plugins 70728d2ad80SAndreas Gohr if(trim($data['headers'])) { 70828d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 70928d2ad80SAndreas Gohr } 7101d045709SAndreas Gohr 7111d045709SAndreas Gohr // send the thing 7121d045709SAndreas Gohr if(is_null($this->sendparam)) { 71328d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers); 7141d045709SAndreas Gohr } else { 71528d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers, $this->sendparam); 7161d045709SAndreas Gohr } 7171d045709SAndreas Gohr } 71828d2ad80SAndreas Gohr // any AFTER actions? 71928d2ad80SAndreas Gohr $evt->advise_after(); 72028d2ad80SAndreas Gohr return $success; 72128d2ad80SAndreas Gohr } 722bb01c27cSAndreas Gohr} 723