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 31f41c79d7SAndreas Gohr protected $allowhtml = true; 321d045709SAndreas Gohr 339ea45836SChristopher Smith protected $replacements = array('text'=> array(), 'html' => array()); 349ea45836SChristopher Smith 351d045709SAndreas Gohr /** 361d045709SAndreas Gohr * Constructor 371d045709SAndreas Gohr * 389ea45836SChristopher Smith * Initializes the boundary strings, part counters and token replacements 391d045709SAndreas Gohr */ 401d045709SAndreas Gohr public function __construct() { 419f3eca0bSAndreas Gohr global $conf; 42585bf44eSChristopher Smith /* @var Input $INPUT */ 43585bf44eSChristopher Smith global $INPUT; 449f3eca0bSAndreas Gohr 459f3eca0bSAndreas Gohr $server = parse_url(DOKU_URL, PHP_URL_HOST); 46d6e04b60SAndreas Gohr if(strpos($server,'.') === false) $server = $server.'.localhost'; 471d045709SAndreas Gohr 481d8036c2SAndreas Gohr $this->partid = substr(md5(uniqid(rand(), true)),0, 8).'@'.$server; 49b3577cf9SAndreas Gohr $this->boundary = '__________'.md5(uniqid(rand(), true)); 509f3eca0bSAndreas Gohr 519f3eca0bSAndreas Gohr $listid = join('.', array_reverse(explode('/', DOKU_BASE))).$server; 529f3eca0bSAndreas Gohr $listid = strtolower(trim($listid, '.')); 539f3eca0bSAndreas Gohr 542398a2b5SAndreas Gohr $this->allowhtml = (bool)$conf['htmlmail']; 552398a2b5SAndreas Gohr 569f3eca0bSAndreas Gohr // add some default headers for mailfiltering FS#2247 576a1f928fSAndreas Gohr $this->setHeader('X-Mailer', 'DokuWiki'); 58585bf44eSChristopher Smith $this->setHeader('X-DokuWiki-User', $INPUT->server->str('REMOTE_USER')); 599f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Title', $conf['title']); 609f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Server', $server); 619f3eca0bSAndreas Gohr $this->setHeader('X-Auto-Response-Suppress', 'OOF'); 629f3eca0bSAndreas Gohr $this->setHeader('List-Id', $conf['title'].' <'.$listid.'>'); 63d6e04b60SAndreas Gohr $this->setHeader('Date', date('r'), false); 649ea45836SChristopher Smith 659ea45836SChristopher Smith $this->prepareTokenReplacements(); 66bb01c27cSAndreas Gohr } 67bb01c27cSAndreas Gohr 68bb01c27cSAndreas Gohr /** 69bb01c27cSAndreas Gohr * Attach a file 70bb01c27cSAndreas Gohr * 71a89c75afSAndreas Gohr * @param string $path Path to the file to attach 72a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 73a89c75afSAndreas Gohr * @param string $name The filename to use 74a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 75bb01c27cSAndreas Gohr */ 76bb01c27cSAndreas Gohr public function attachFile($path, $mime, $name = '', $embed = '') { 77bb01c27cSAndreas Gohr if(!$name) { 783009a773SAndreas Gohr $name = utf8_basename($path); 79bb01c27cSAndreas Gohr } 80bb01c27cSAndreas Gohr 81bb01c27cSAndreas Gohr $this->attach[] = array( 82bb01c27cSAndreas Gohr 'data' => file_get_contents($path), 83bb01c27cSAndreas Gohr 'mime' => $mime, 84bb01c27cSAndreas Gohr 'name' => $name, 85bb01c27cSAndreas Gohr 'embed' => $embed 86bb01c27cSAndreas Gohr ); 87bb01c27cSAndreas Gohr } 88bb01c27cSAndreas Gohr 89bb01c27cSAndreas Gohr /** 90bb01c27cSAndreas Gohr * Attach a file 91bb01c27cSAndreas Gohr * 92a89c75afSAndreas Gohr * @param string $data The file contents to attach 93a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 94a89c75afSAndreas Gohr * @param string $name The filename to use 95a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 96bb01c27cSAndreas Gohr */ 97bb01c27cSAndreas Gohr public function attachContent($data, $mime, $name = '', $embed = '') { 98bb01c27cSAndreas Gohr if(!$name) { 99a89c75afSAndreas Gohr list(, $ext) = explode('/', $mime); 100bb01c27cSAndreas Gohr $name = count($this->attach).".$ext"; 101bb01c27cSAndreas Gohr } 102bb01c27cSAndreas Gohr 103bb01c27cSAndreas Gohr $this->attach[] = array( 104bb01c27cSAndreas Gohr 'data' => $data, 105bb01c27cSAndreas Gohr 'mime' => $mime, 106bb01c27cSAndreas Gohr 'name' => $name, 107bb01c27cSAndreas Gohr 'embed' => $embed 108bb01c27cSAndreas Gohr ); 109bb01c27cSAndreas Gohr } 110bb01c27cSAndreas Gohr 111bb01c27cSAndreas Gohr /** 112850dbf1fSAndreas Gohr * Callback function to automatically embed images referenced in HTML templates 11342ea7f44SGerrit Uitslag * 11442ea7f44SGerrit Uitslag * @param array $matches 11542ea7f44SGerrit Uitslag * @return string placeholder 116850dbf1fSAndreas Gohr */ 117850dbf1fSAndreas Gohr protected function autoembed_cb($matches) { 118850dbf1fSAndreas Gohr static $embeds = 0; 119850dbf1fSAndreas Gohr $embeds++; 120850dbf1fSAndreas Gohr 121850dbf1fSAndreas Gohr // get file and mime type 122850dbf1fSAndreas Gohr $media = cleanID($matches[1]); 123a89c75afSAndreas Gohr list(, $mime) = mimetype($media); 124850dbf1fSAndreas Gohr $file = mediaFN($media); 125850dbf1fSAndreas Gohr if(!file_exists($file)) return $matches[0]; //bad reference, keep as is 126850dbf1fSAndreas Gohr 127850dbf1fSAndreas Gohr // attach it and set placeholder 128850dbf1fSAndreas Gohr $this->attachFile($file, $mime, '', 'autoembed'.$embeds); 129850dbf1fSAndreas Gohr return '%%autoembed'.$embeds.'%%'; 130850dbf1fSAndreas Gohr } 131850dbf1fSAndreas Gohr 132850dbf1fSAndreas Gohr /** 1331d045709SAndreas Gohr * Add an arbitrary header to the mail 1341d045709SAndreas Gohr * 135a36fc348SAndreas Gohr * If an empy value is passed, the header is removed 136a36fc348SAndreas Gohr * 1371d045709SAndreas Gohr * @param string $header the header name (no trailing colon!) 13859bc3b48SGerrit Uitslag * @param string|string[] $value the value of the header 1391d045709SAndreas Gohr * @param bool $clean remove all non-ASCII chars and line feeds? 1401d045709SAndreas Gohr */ 1411d045709SAndreas Gohr public function setHeader($header, $value, $clean = true) { 1429f3eca0bSAndreas Gohr $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing 1431d045709SAndreas Gohr if($clean) { 144578b2c23SAndreas Gohr $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header); 145578b2c23SAndreas Gohr $value = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value); 1461d045709SAndreas Gohr } 147a36fc348SAndreas Gohr 148a36fc348SAndreas Gohr // empty value deletes 149b6c97c70SAndreas Gohr if(is_array($value)){ 150b6c97c70SAndreas Gohr $value = array_map('trim', $value); 151b6c97c70SAndreas Gohr $value = array_filter($value); 152b6c97c70SAndreas Gohr if(!$value) $value = ''; 153b6c97c70SAndreas Gohr }else{ 154a36fc348SAndreas Gohr $value = trim($value); 155b6c97c70SAndreas Gohr } 156a36fc348SAndreas Gohr if($value === '') { 157a36fc348SAndreas Gohr if(isset($this->headers[$header])) unset($this->headers[$header]); 158a36fc348SAndreas Gohr } else { 1591d045709SAndreas Gohr $this->headers[$header] = $value; 1601d045709SAndreas Gohr } 161a36fc348SAndreas Gohr } 1621d045709SAndreas Gohr 1631d045709SAndreas Gohr /** 1641d045709SAndreas Gohr * Set additional parameters to be passed to sendmail 1651d045709SAndreas Gohr * 1661d045709SAndreas Gohr * Whatever is set here is directly passed to PHP's mail() command as last 1671d045709SAndreas Gohr * parameter. Depending on the PHP setup this might break mailing alltogether 16842ea7f44SGerrit Uitslag * 16942ea7f44SGerrit Uitslag * @param string $param 1701d045709SAndreas Gohr */ 1711d045709SAndreas Gohr public function setParameters($param) { 1721d045709SAndreas Gohr $this->sendparam = $param; 1731d045709SAndreas Gohr } 1741d045709SAndreas Gohr 1751d045709SAndreas Gohr /** 176abbf0890SAndreas Gohr * Set the text and HTML body and apply replacements 177abbf0890SAndreas Gohr * 178abbf0890SAndreas Gohr * This function applies a whole bunch of default replacements in addition 17904dcb5b2SChristopher Smith * to the ones specified as parameters 180abbf0890SAndreas Gohr * 181abbf0890SAndreas Gohr * If you pass the HTML part or HTML replacements yourself you have to make 182abbf0890SAndreas Gohr * sure you encode all HTML special chars correctly 183abbf0890SAndreas Gohr * 184abbf0890SAndreas Gohr * @param string $text plain text body 185abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 186abbf0890SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep 18759bc3b48SGerrit Uitslag * @param string $html the HTML body, leave null to create it from $text 188f08086ecSAndreas Gohr * @param bool $wrap wrap the HTML in the default header/Footer 189abbf0890SAndreas Gohr */ 190f08086ecSAndreas Gohr public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true) { 191585bf44eSChristopher Smith 19276efd6d0SAndreas Gohr $htmlrep = (array)$htmlrep; 19376efd6d0SAndreas Gohr $textrep = (array)$textrep; 194abbf0890SAndreas Gohr 195abbf0890SAndreas Gohr // create HTML from text if not given 196abbf0890SAndreas Gohr if(is_null($html)) { 197ba9c057bSAndreas Gohr $html = $text; 198ba9c057bSAndreas Gohr $html = hsc($html); 199ba2c2f17Sfurun $html = preg_replace('/^----+$/m', '<hr >', $html); 200ba9c057bSAndreas Gohr $html = nl2br($html); 201abbf0890SAndreas Gohr } 202f08086ecSAndreas Gohr if($wrap) { 2033819cafdSfurun $wrap = rawLocale('mailwrap', 'html'); 2043819cafdSfurun $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature 2053819cafdSfurun $html = str_replace('@EMAILSIGNATURE@', '', $html); //strip @EMAILSIGNATURE@ 2063819cafdSfurun $html = str_replace('@HTMLBODY@', $html, $wrap); 207f08086ecSAndreas Gohr } 208f08086ecSAndreas Gohr 2093819cafdSfurun if(strpos($text, '@EMAILSIGNATURE@') === false) { 2109ea45836SChristopher Smith $text .= '@EMAILSIGNATURE@'; 2113819cafdSfurun } 212ba2c2f17Sfurun 21376efd6d0SAndreas Gohr // copy over all replacements missing for HTML (autolink URLs) 21476efd6d0SAndreas Gohr foreach($textrep as $key => $value) { 21576efd6d0SAndreas Gohr if(isset($htmlrep[$key])) continue; 2163e7e6067SKlap-in if(media_isexternal($value)) { 21776efd6d0SAndreas Gohr $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>'; 21876efd6d0SAndreas Gohr } else { 21976efd6d0SAndreas Gohr $htmlrep[$key] = hsc($value); 22076efd6d0SAndreas Gohr } 221abbf0890SAndreas Gohr } 222abbf0890SAndreas Gohr 223850dbf1fSAndreas Gohr // embed media from templates 224a89c75afSAndreas Gohr $html = preg_replace_callback( 225a89c75afSAndreas Gohr '/@MEDIA\(([^\)]+)\)@/', 226a89c75afSAndreas Gohr array($this, 'autoembed_cb'), $html 227a89c75afSAndreas Gohr ); 228850dbf1fSAndreas Gohr 2299ea45836SChristopher Smith // add default token replacements 2309ea45836SChristopher Smith $trep = array_merge($this->replacements['text'], (array)$textrep); 2319ea45836SChristopher Smith $hrep = array_merge($this->replacements['html'], (array)$htmlrep); 232abbf0890SAndreas Gohr 233abbf0890SAndreas Gohr // Apply replacements 234abbf0890SAndreas Gohr foreach($trep as $key => $substitution) { 235abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@', $substitution, $text); 236abbf0890SAndreas Gohr } 237abbf0890SAndreas Gohr foreach($hrep as $key => $substitution) { 238abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@', $substitution, $html); 239abbf0890SAndreas Gohr } 240abbf0890SAndreas Gohr 241abbf0890SAndreas Gohr $this->setHTML($html); 242abbf0890SAndreas Gohr $this->setText($text); 243abbf0890SAndreas Gohr } 244abbf0890SAndreas Gohr 245abbf0890SAndreas Gohr /** 246bb01c27cSAndreas Gohr * Set the HTML part of the mail 247bb01c27cSAndreas Gohr * 248bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 249abbf0890SAndreas Gohr * 250abbf0890SAndreas Gohr * You probably want to use setBody() instead 25142ea7f44SGerrit Uitslag * 25242ea7f44SGerrit Uitslag * @param string $html 253bb01c27cSAndreas Gohr */ 2541d045709SAndreas Gohr public function setHTML($html) { 255bb01c27cSAndreas Gohr $this->html = $html; 256bb01c27cSAndreas Gohr } 257bb01c27cSAndreas Gohr 258bb01c27cSAndreas Gohr /** 259bb01c27cSAndreas Gohr * Set the plain text part of the mail 260abbf0890SAndreas Gohr * 261abbf0890SAndreas Gohr * You probably want to use setBody() instead 26242ea7f44SGerrit Uitslag * 26342ea7f44SGerrit Uitslag * @param string $text 264bb01c27cSAndreas Gohr */ 2651d045709SAndreas Gohr public function setText($text) { 266bb01c27cSAndreas Gohr $this->text = $text; 267bb01c27cSAndreas Gohr } 268bb01c27cSAndreas Gohr 269bb01c27cSAndreas Gohr /** 270a36fc348SAndreas Gohr * Add the To: recipients 271a36fc348SAndreas Gohr * 2728c253612SGerrit Uitslag * @see cleanAddress 27359bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 274a36fc348SAndreas Gohr */ 275a36fc348SAndreas Gohr public function to($address) { 276a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 277a36fc348SAndreas Gohr } 278a36fc348SAndreas Gohr 279a36fc348SAndreas Gohr /** 280a36fc348SAndreas Gohr * Add the Cc: recipients 281a36fc348SAndreas Gohr * 2828c253612SGerrit Uitslag * @see cleanAddress 28359bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 284a36fc348SAndreas Gohr */ 285a36fc348SAndreas Gohr public function cc($address) { 286a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 287a36fc348SAndreas Gohr } 288a36fc348SAndreas Gohr 289a36fc348SAndreas Gohr /** 290a36fc348SAndreas Gohr * Add the Bcc: recipients 291a36fc348SAndreas Gohr * 2928c253612SGerrit Uitslag * @see cleanAddress 29359bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 294a36fc348SAndreas Gohr */ 295a36fc348SAndreas Gohr public function bcc($address) { 296a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 297a36fc348SAndreas Gohr } 298a36fc348SAndreas Gohr 299a36fc348SAndreas Gohr /** 300a36fc348SAndreas Gohr * Add the From: address 301a36fc348SAndreas Gohr * 302a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 303a36fc348SAndreas Gohr * to call this function 304a36fc348SAndreas Gohr * 3058c253612SGerrit Uitslag * @see cleanAddress 306a36fc348SAndreas Gohr * @param string $address from address 307a36fc348SAndreas Gohr */ 308a36fc348SAndreas Gohr public function from($address) { 309a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 310a36fc348SAndreas Gohr } 311a36fc348SAndreas Gohr 312a36fc348SAndreas Gohr /** 313a36fc348SAndreas Gohr * Add the mail's Subject: header 314a36fc348SAndreas Gohr * 315a36fc348SAndreas Gohr * @param string $subject the mail subject 316a36fc348SAndreas Gohr */ 317a36fc348SAndreas Gohr public function subject($subject) { 318a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 319a36fc348SAndreas Gohr } 320a36fc348SAndreas Gohr 321a36fc348SAndreas Gohr /** 3221d045709SAndreas Gohr * Sets an email address header with correct encoding 323bb01c27cSAndreas Gohr * 324bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 325bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 326bb01c27cSAndreas Gohr * 327bb01c27cSAndreas Gohr * Example: 3288c253612SGerrit Uitslag * cc("föö <foo@bar.com>, me@somewhere.com","TBcc"); 329bb01c27cSAndreas Gohr * 33042ea7f44SGerrit Uitslag * @param string|string[] $addresses Multiple adresses separated by commas or as array 33142ea7f44SGerrit Uitslag * @return false|string the prepared header (can contain multiple lines) 332bb01c27cSAndreas Gohr */ 333b6c97c70SAndreas Gohr public function cleanAddress($addresses) { 334bb01c27cSAndreas Gohr $headers = ''; 335b6c97c70SAndreas Gohr if(!is_array($addresses)){ 336b6c97c70SAndreas Gohr $addresses = explode(',', $addresses); 337b6c97c70SAndreas Gohr } 338b6c97c70SAndreas Gohr 339b6c97c70SAndreas Gohr foreach($addresses as $part) { 340b6c97c70SAndreas Gohr $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors 341bb01c27cSAndreas Gohr $part = trim($part); 342bb01c27cSAndreas Gohr 343bb01c27cSAndreas Gohr // parse address 344bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) { 345bb01c27cSAndreas Gohr $text = trim($matches[1]); 346bb01c27cSAndreas Gohr $addr = $matches[2]; 347bb01c27cSAndreas Gohr } else { 348bb01c27cSAndreas Gohr $addr = $part; 349bb01c27cSAndreas Gohr } 350bb01c27cSAndreas Gohr // skip empty ones 351bb01c27cSAndreas Gohr if(empty($addr)) { 352bb01c27cSAndreas Gohr continue; 353bb01c27cSAndreas Gohr } 354bb01c27cSAndreas Gohr 355bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 356bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)) { 357bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"), -1); 358bb01c27cSAndreas Gohr continue; 359bb01c27cSAndreas Gohr } 360bb01c27cSAndreas Gohr 36164d23c16SAndreas Gohr if(!mail_isvalid($addr)) { 362bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"), -1); 363bb01c27cSAndreas Gohr continue; 364bb01c27cSAndreas Gohr } 365bb01c27cSAndreas Gohr 366bb01c27cSAndreas Gohr // text was given 367*30085ef3SYurii K if(!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652) 368bb01c27cSAndreas Gohr // add address quotes 369bb01c27cSAndreas Gohr $addr = "<$addr>"; 370bb01c27cSAndreas Gohr 371bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 372bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 373bb01c27cSAndreas Gohr $text = utf8_strip($text); 374bb01c27cSAndreas Gohr } 375bb01c27cSAndreas Gohr 376b6c97c70SAndreas Gohr if(strpos($text, ',') !== false || !utf8_isASCII($text)) { 377bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 378bb01c27cSAndreas Gohr } 379bb01c27cSAndreas Gohr } else { 380bb01c27cSAndreas Gohr $text = ''; 381bb01c27cSAndreas Gohr } 382bb01c27cSAndreas Gohr 383bb01c27cSAndreas Gohr // add to header comma seperated 384bb01c27cSAndreas Gohr if($headers != '') { 385bb01c27cSAndreas Gohr $headers .= ', '; 386bb01c27cSAndreas Gohr } 387bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 388bb01c27cSAndreas Gohr } 389bb01c27cSAndreas Gohr 390b6c97c70SAndreas Gohr $headers = trim($headers); 391bb01c27cSAndreas Gohr if(empty($headers)) return false; 392bb01c27cSAndreas Gohr 393bb01c27cSAndreas Gohr return $headers; 394bb01c27cSAndreas Gohr } 395bb01c27cSAndreas Gohr 396bb01c27cSAndreas Gohr 397bb01c27cSAndreas Gohr /** 398bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 399bb01c27cSAndreas Gohr * 400bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 40142ea7f44SGerrit Uitslag * 40242ea7f44SGerrit Uitslag * @return string mime multiparts 403bb01c27cSAndreas Gohr */ 404bb01c27cSAndreas Gohr protected function prepareAttachments() { 405bb01c27cSAndreas Gohr $mime = ''; 406bb01c27cSAndreas Gohr $part = 1; 407bb01c27cSAndreas Gohr // embedded attachments 408bb01c27cSAndreas Gohr foreach($this->attach as $media) { 409ce9d2cc8SAndreas Gohr $media['name'] = str_replace(':', '_', cleanID($media['name'], true)); 410ce9d2cc8SAndreas Gohr 411bb01c27cSAndreas Gohr // create content id 412bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 413bb01c27cSAndreas Gohr 414bb01c27cSAndreas Gohr // replace wildcards 415bb01c27cSAndreas Gohr if($media['embed']) { 416bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html); 417bb01c27cSAndreas Gohr } 418bb01c27cSAndreas Gohr 419bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 4201d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"'); 4211d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64'); 4221d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-ID',"<$cid>"); 423bb01c27cSAndreas Gohr if($media['embed']) { 4241d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']); 425bb01c27cSAndreas Gohr } else { 4261d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']); 427bb01c27cSAndreas Gohr } 428bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 429bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL); 430bb01c27cSAndreas Gohr 431bb01c27cSAndreas Gohr $part++; 432bb01c27cSAndreas Gohr } 433bb01c27cSAndreas Gohr return $mime; 434bb01c27cSAndreas Gohr } 435bb01c27cSAndreas Gohr 4361d045709SAndreas Gohr /** 4371d045709SAndreas Gohr * Build the body and handles multi part mails 4381d045709SAndreas Gohr * 4391d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4401d045709SAndreas Gohr * 4411d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4421d045709SAndreas Gohr */ 4431d045709SAndreas Gohr protected function prepareBody() { 4441d045709SAndreas Gohr 4452398a2b5SAndreas Gohr // no HTML mails allowed? remove HTML body 4462398a2b5SAndreas Gohr if(!$this->allowhtml) { 4472398a2b5SAndreas Gohr $this->html = ''; 4482398a2b5SAndreas Gohr } 4492398a2b5SAndreas Gohr 450bb01c27cSAndreas Gohr // check for body 451bb01c27cSAndreas Gohr if(!$this->text && !$this->html) { 452bb01c27cSAndreas Gohr return false; 453bb01c27cSAndreas Gohr } 454bb01c27cSAndreas Gohr 455bb01c27cSAndreas Gohr // add general headers 456bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 457bb01c27cSAndreas Gohr 4581d045709SAndreas Gohr $body = ''; 4591d045709SAndreas Gohr 460bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)) { // we can send a simple single part message 461bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 462bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 463be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 464bb01c27cSAndreas Gohr } else { // multi part it is 4651d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 466bb01c27cSAndreas Gohr 467bb01c27cSAndreas Gohr // prepare the attachments 468bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 469bb01c27cSAndreas Gohr 470bb01c27cSAndreas Gohr // do we have alternative text content? 471bb01c27cSAndreas Gohr if($this->text && $this->html) { 472a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 473a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 474bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4751d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4761d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 477bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 478be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 479bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 480a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 481d6e04b60SAndreas Gohr ' boundary="'.$this->boundary.'";'.MAILHEADER_EOL. 482d6e04b60SAndreas Gohr ' type="text/html"'.MAILHEADER_EOL; 483bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 484bb01c27cSAndreas Gohr } 485bb01c27cSAndreas Gohr 4861d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4871d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4881d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 489bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 490be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL); 491bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 492bb01c27cSAndreas Gohr $body .= $attachments; 493bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 494bb01c27cSAndreas Gohr 495bb01c27cSAndreas Gohr // close open multipart/alternative boundary 496bb01c27cSAndreas Gohr if($this->text && $this->html) { 497bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 498bb01c27cSAndreas Gohr } 499bb01c27cSAndreas Gohr } 500bb01c27cSAndreas Gohr 501bb01c27cSAndreas Gohr return $body; 502bb01c27cSAndreas Gohr } 503bb01c27cSAndreas Gohr 504bb01c27cSAndreas Gohr /** 505a36fc348SAndreas Gohr * Cleanup and encode the headers array 506a36fc348SAndreas Gohr */ 507a36fc348SAndreas Gohr protected function cleanHeaders() { 508a36fc348SAndreas Gohr global $conf; 509a36fc348SAndreas Gohr 510a36fc348SAndreas Gohr // clean up addresses 511a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 512acbf061cSGerrit Uitslag $addrs = array('To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'); 513a36fc348SAndreas Gohr foreach($addrs as $addr) { 514a36fc348SAndreas Gohr if(isset($this->headers[$addr])) { 515a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 516a36fc348SAndreas Gohr } 517a36fc348SAndreas Gohr } 518a36fc348SAndreas Gohr 51945992a63SAndreas Gohr if(isset($this->headers['Subject'])) { 520a36fc348SAndreas Gohr // add prefix to subject 52154f30755SAndreas Gohr if(empty($conf['mailprefix'])) { 5228a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 52354f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 52454f30755SAndreas Gohr } else { 5258a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 5268a215f09SAndreas Gohr } 5278a215f09SAndreas Gohr } else { 528a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 52954f30755SAndreas Gohr } 530a36fc348SAndreas Gohr $len = strlen($prefix); 53145992a63SAndreas Gohr if(substr($this->headers['Subject'], 0, $len) != $prefix) { 53245992a63SAndreas Gohr $this->headers['Subject'] = $prefix.' '.$this->headers['Subject']; 533a36fc348SAndreas Gohr } 534a36fc348SAndreas Gohr 535a36fc348SAndreas Gohr // encode subject 536a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 53745992a63SAndreas Gohr $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']); 53845992a63SAndreas Gohr $this->headers['Subject'] = utf8_strip($this->headers['Subject']); 539a36fc348SAndreas Gohr } 540a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])) { 54145992a63SAndreas Gohr $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 542a36fc348SAndreas Gohr } 543a36fc348SAndreas Gohr } 544a36fc348SAndreas Gohr 545a36fc348SAndreas Gohr } 5461d8036c2SAndreas Gohr 5471d8036c2SAndreas Gohr /** 5481d8036c2SAndreas Gohr * Returns a complete, EOL terminated header line, wraps it if necessary 5491d8036c2SAndreas Gohr * 55042ea7f44SGerrit Uitslag * @param string $key 55142ea7f44SGerrit Uitslag * @param string $val 55242ea7f44SGerrit Uitslag * @return string line 5531d8036c2SAndreas Gohr */ 5541d8036c2SAndreas Gohr protected function wrappedHeaderLine($key, $val){ 5551d8036c2SAndreas Gohr return wordwrap("$key: $val", 78, MAILHEADER_EOL.' ').MAILHEADER_EOL; 556a36fc348SAndreas Gohr } 557a36fc348SAndreas Gohr 558a36fc348SAndreas Gohr /** 559bb01c27cSAndreas Gohr * Create a string from the headers array 5601d045709SAndreas Gohr * 5611d045709SAndreas Gohr * @returns string the headers 562bb01c27cSAndreas Gohr */ 563bb01c27cSAndreas Gohr protected function prepareHeaders() { 564bb01c27cSAndreas Gohr $headers = ''; 565bb01c27cSAndreas Gohr foreach($this->headers as $key => $val) { 5666be717dbSMichael Hamann if ($val === '' || is_null($val)) continue; 5671d8036c2SAndreas Gohr $headers .= $this->wrappedHeaderLine($key, $val); 568bb01c27cSAndreas Gohr } 569bb01c27cSAndreas Gohr return $headers; 570bb01c27cSAndreas Gohr } 571bb01c27cSAndreas Gohr 572bb01c27cSAndreas Gohr /** 573bb01c27cSAndreas Gohr * return a full email with all headers 574bb01c27cSAndreas Gohr * 5751d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5761d045709SAndreas Gohr * used for MHT exports 5771d045709SAndreas Gohr * 5781d045709SAndreas Gohr * @return string the mail, false on errors 579bb01c27cSAndreas Gohr */ 580bb01c27cSAndreas Gohr public function dump() { 581a36fc348SAndreas Gohr $this->cleanHeaders(); 582bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5834d18e936SAndreas Gohr if($body === false) return false; 5841d045709SAndreas Gohr $headers = $this->prepareHeaders(); 585bb01c27cSAndreas Gohr 586bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 587bb01c27cSAndreas Gohr } 5881d045709SAndreas Gohr 5891d045709SAndreas Gohr /** 5909ea45836SChristopher Smith * Prepare default token replacement strings 5919ea45836SChristopher Smith * 5929ea45836SChristopher Smith * Populates the '$replacements' property. 5939ea45836SChristopher Smith * Should be called by the class constructor 5949ea45836SChristopher Smith */ 5959ea45836SChristopher Smith protected function prepareTokenReplacements() { 5969ea45836SChristopher Smith global $INFO; 5979ea45836SChristopher Smith global $conf; 5989ea45836SChristopher Smith /* @var Input $INPUT */ 5999ea45836SChristopher Smith global $INPUT; 6009ea45836SChristopher Smith global $lang; 6019ea45836SChristopher Smith 6029ea45836SChristopher Smith $ip = clientIP(); 6039ea45836SChristopher Smith $cip = gethostsbyaddrs($ip); 6049ea45836SChristopher Smith 6059ea45836SChristopher Smith $this->replacements['text'] = array( 6069ea45836SChristopher Smith 'DATE' => dformat(), 6079ea45836SChristopher Smith 'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'), 6089ea45836SChristopher Smith 'IPADDRESS' => $ip, 6099ea45836SChristopher Smith 'HOSTNAME' => $cip, 6109ea45836SChristopher Smith 'TITLE' => $conf['title'], 6119ea45836SChristopher Smith 'DOKUWIKIURL' => DOKU_URL, 6129ea45836SChristopher Smith 'USER' => $INPUT->server->str('REMOTE_USER'), 6139ea45836SChristopher Smith 'NAME' => $INFO['userinfo']['name'], 614774514c9SGerrit Uitslag 'MAIL' => $INFO['userinfo']['mail'] 6159ea45836SChristopher Smith ); 616774514c9SGerrit Uitslag $signature = str_replace('@DOKUWIKIURL@', $this->replacements['text']['DOKUWIKIURL'], $lang['email_signature_text']); 617774514c9SGerrit Uitslag $this->replacements['text']['EMAILSIGNATURE'] = "\n-- \n" . $signature . "\n"; 6189ea45836SChristopher Smith 6199ea45836SChristopher Smith $this->replacements['html'] = array( 6209ea45836SChristopher Smith 'DATE' => '<i>' . hsc(dformat()) . '</i>', 6219ea45836SChristopher Smith 'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')), 6229ea45836SChristopher Smith 'IPADDRESS' => '<code>' . hsc($ip) . '</code>', 6239ea45836SChristopher Smith 'HOSTNAME' => '<code>' . hsc($cip) . '</code>', 6249ea45836SChristopher Smith 'TITLE' => hsc($conf['title']), 6259ea45836SChristopher Smith 'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>', 6269ea45836SChristopher Smith 'USER' => hsc($INPUT->server->str('REMOTE_USER')), 6279ea45836SChristopher Smith 'NAME' => hsc($INFO['userinfo']['name']), 6289ea45836SChristopher Smith 'MAIL' => '<a href="mailto:"' . hsc($INFO['userinfo']['mail']) . '">' . 629774514c9SGerrit Uitslag hsc($INFO['userinfo']['mail']) . '</a>' 6309ea45836SChristopher Smith ); 631774514c9SGerrit Uitslag $signature = $lang['email_signature_text']; 632774514c9SGerrit Uitslag if(!empty($lang['email_signature_html'])) { 633774514c9SGerrit Uitslag $signature = $lang['email_signature_html']; 634774514c9SGerrit Uitslag } 635774514c9SGerrit Uitslag $signature = str_replace( 636774514c9SGerrit Uitslag array( 637774514c9SGerrit Uitslag '@DOKUWIKIURL@', 638774514c9SGerrit Uitslag "\n" 639774514c9SGerrit Uitslag ), 640774514c9SGerrit Uitslag array( 641774514c9SGerrit Uitslag $this->replacements['html']['DOKUWIKIURL'], 642774514c9SGerrit Uitslag '<br />' 643774514c9SGerrit Uitslag ), 644774514c9SGerrit Uitslag $signature 645774514c9SGerrit Uitslag ); 646774514c9SGerrit Uitslag $this->replacements['html']['EMAILSIGNATURE'] = $signature; 6479ea45836SChristopher Smith } 6489ea45836SChristopher Smith 6499ea45836SChristopher Smith /** 6501d045709SAndreas Gohr * Send the mail 6511d045709SAndreas Gohr * 6521d045709SAndreas Gohr * Call this after all data was set 6531d045709SAndreas Gohr * 65428d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 6551d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 6561d045709SAndreas Gohr */ 6571d045709SAndreas Gohr public function send() { 65828d2ad80SAndreas Gohr $success = false; 659a36fc348SAndreas Gohr 66028d2ad80SAndreas Gohr // prepare hook data 66128d2ad80SAndreas Gohr $data = array( 66228d2ad80SAndreas Gohr // pass the whole mail class to plugin 66328d2ad80SAndreas Gohr 'mail' => $this, 66428d2ad80SAndreas Gohr // pass references for backward compatibility 66528d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 66628d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 66728d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 66828d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 66928d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 67028d2ad80SAndreas Gohr 'body' => &$this->text, 671a89c75afSAndreas Gohr 'params' => &$this->sendparam, 67228d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 67328d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 67428d2ad80SAndreas Gohr 'success' => &$success, 67528d2ad80SAndreas Gohr ); 67628d2ad80SAndreas Gohr 67728d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 67828d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 67928d2ad80SAndreas Gohr if($evt->advise_before(true)) { 68028d2ad80SAndreas Gohr // clean up before using the headers 681a36fc348SAndreas Gohr $this->cleanHeaders(); 682a36fc348SAndreas Gohr 6831d045709SAndreas Gohr // any recipients? 6841d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 6851d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 686a89c75afSAndreas Gohr trim($this->headers['Bcc']) === '' 687a89c75afSAndreas Gohr ) return false; 6881d045709SAndreas Gohr 6891d045709SAndreas Gohr // The To: header is special 6906be717dbSMichael Hamann if(array_key_exists('To', $this->headers)) { 6916be717dbSMichael Hamann $to = (string)$this->headers['To']; 6921d045709SAndreas Gohr unset($this->headers['To']); 6931d045709SAndreas Gohr } else { 6941d045709SAndreas Gohr $to = ''; 6951d045709SAndreas Gohr } 6961d045709SAndreas Gohr 6971d045709SAndreas Gohr // so is the subject 6986be717dbSMichael Hamann if(array_key_exists('Subject', $this->headers)) { 6996be717dbSMichael Hamann $subject = (string)$this->headers['Subject']; 7001d045709SAndreas Gohr unset($this->headers['Subject']); 7011d045709SAndreas Gohr } else { 7021d045709SAndreas Gohr $subject = ''; 7031d045709SAndreas Gohr } 7041d045709SAndreas Gohr 7051d045709SAndreas Gohr // make the body 7061d045709SAndreas Gohr $body = $this->prepareBody(); 7074c89a7f6SAndreas Gohr if($body === false) return false; 7081d045709SAndreas Gohr 7091d045709SAndreas Gohr // cook the headers 7101d045709SAndreas Gohr $headers = $this->prepareHeaders(); 71128d2ad80SAndreas Gohr // add any headers set by legacy plugins 71228d2ad80SAndreas Gohr if(trim($data['headers'])) { 71328d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 71428d2ad80SAndreas Gohr } 7151d045709SAndreas Gohr 7161d045709SAndreas Gohr // send the thing 7171d045709SAndreas Gohr if(is_null($this->sendparam)) { 71828d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers); 7191d045709SAndreas Gohr } else { 72028d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers, $this->sendparam); 7211d045709SAndreas Gohr } 7221d045709SAndreas Gohr } 72328d2ad80SAndreas Gohr // any AFTER actions? 72428d2ad80SAndreas Gohr $evt->advise_after(); 72528d2ad80SAndreas Gohr return $success; 72628d2ad80SAndreas Gohr } 727bb01c27cSAndreas Gohr} 728