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 // No named recipients for To: in Windows (see FS#652) 335bb01c27cSAndreas Gohr $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 336bb01c27cSAndreas Gohr 337bb01c27cSAndreas Gohr $headers = ''; 338b6c97c70SAndreas Gohr if(!is_array($addresses)){ 339b6c97c70SAndreas Gohr $addresses = explode(',', $addresses); 340b6c97c70SAndreas Gohr } 341b6c97c70SAndreas Gohr 342b6c97c70SAndreas Gohr foreach($addresses as $part) { 343b6c97c70SAndreas Gohr $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors 344bb01c27cSAndreas Gohr $part = trim($part); 345bb01c27cSAndreas Gohr 346bb01c27cSAndreas Gohr // parse address 347bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) { 348bb01c27cSAndreas Gohr $text = trim($matches[1]); 349bb01c27cSAndreas Gohr $addr = $matches[2]; 350bb01c27cSAndreas Gohr } else { 351bb01c27cSAndreas Gohr $addr = $part; 352bb01c27cSAndreas Gohr } 353bb01c27cSAndreas Gohr // skip empty ones 354bb01c27cSAndreas Gohr if(empty($addr)) { 355bb01c27cSAndreas Gohr continue; 356bb01c27cSAndreas Gohr } 357bb01c27cSAndreas Gohr 358bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 359bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)) { 360bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"), -1); 361bb01c27cSAndreas Gohr continue; 362bb01c27cSAndreas Gohr } 363bb01c27cSAndreas Gohr 364*64d23c16SAndreas Gohr if(!mail_isvalid($addr)) { 365bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"), -1); 366bb01c27cSAndreas Gohr continue; 367bb01c27cSAndreas Gohr } 368bb01c27cSAndreas Gohr 369bb01c27cSAndreas Gohr // text was given 370bb01c27cSAndreas Gohr if(!empty($text) && $names) { 371bb01c27cSAndreas Gohr // add address quotes 372bb01c27cSAndreas Gohr $addr = "<$addr>"; 373bb01c27cSAndreas Gohr 374bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 375bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 376bb01c27cSAndreas Gohr $text = utf8_strip($text); 377bb01c27cSAndreas Gohr } 378bb01c27cSAndreas Gohr 379b6c97c70SAndreas Gohr if(strpos($text, ',') !== false || !utf8_isASCII($text)) { 380bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 381bb01c27cSAndreas Gohr } 382bb01c27cSAndreas Gohr } else { 383bb01c27cSAndreas Gohr $text = ''; 384bb01c27cSAndreas Gohr } 385bb01c27cSAndreas Gohr 386bb01c27cSAndreas Gohr // add to header comma seperated 387bb01c27cSAndreas Gohr if($headers != '') { 388bb01c27cSAndreas Gohr $headers .= ', '; 389bb01c27cSAndreas Gohr } 390bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 391bb01c27cSAndreas Gohr } 392bb01c27cSAndreas Gohr 393b6c97c70SAndreas Gohr $headers = trim($headers); 394bb01c27cSAndreas Gohr if(empty($headers)) return false; 395bb01c27cSAndreas Gohr 396bb01c27cSAndreas Gohr return $headers; 397bb01c27cSAndreas Gohr } 398bb01c27cSAndreas Gohr 399bb01c27cSAndreas Gohr 400bb01c27cSAndreas Gohr /** 401bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 402bb01c27cSAndreas Gohr * 403bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 40442ea7f44SGerrit Uitslag * 40542ea7f44SGerrit Uitslag * @return string mime multiparts 406bb01c27cSAndreas Gohr */ 407bb01c27cSAndreas Gohr protected function prepareAttachments() { 408bb01c27cSAndreas Gohr $mime = ''; 409bb01c27cSAndreas Gohr $part = 1; 410bb01c27cSAndreas Gohr // embedded attachments 411bb01c27cSAndreas Gohr foreach($this->attach as $media) { 412ce9d2cc8SAndreas Gohr $media['name'] = str_replace(':', '_', cleanID($media['name'], true)); 413ce9d2cc8SAndreas Gohr 414bb01c27cSAndreas Gohr // create content id 415bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 416bb01c27cSAndreas Gohr 417bb01c27cSAndreas Gohr // replace wildcards 418bb01c27cSAndreas Gohr if($media['embed']) { 419bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html); 420bb01c27cSAndreas Gohr } 421bb01c27cSAndreas Gohr 422bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 4231d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"'); 4241d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64'); 4251d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-ID',"<$cid>"); 426bb01c27cSAndreas Gohr if($media['embed']) { 4271d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']); 428bb01c27cSAndreas Gohr } else { 4291d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']); 430bb01c27cSAndreas Gohr } 431bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 432bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL); 433bb01c27cSAndreas Gohr 434bb01c27cSAndreas Gohr $part++; 435bb01c27cSAndreas Gohr } 436bb01c27cSAndreas Gohr return $mime; 437bb01c27cSAndreas Gohr } 438bb01c27cSAndreas Gohr 4391d045709SAndreas Gohr /** 4401d045709SAndreas Gohr * Build the body and handles multi part mails 4411d045709SAndreas Gohr * 4421d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4431d045709SAndreas Gohr * 4441d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4451d045709SAndreas Gohr */ 4461d045709SAndreas Gohr protected function prepareBody() { 4471d045709SAndreas Gohr 4482398a2b5SAndreas Gohr // no HTML mails allowed? remove HTML body 4492398a2b5SAndreas Gohr if(!$this->allowhtml) { 4502398a2b5SAndreas Gohr $this->html = ''; 4512398a2b5SAndreas Gohr } 4522398a2b5SAndreas Gohr 453bb01c27cSAndreas Gohr // check for body 454bb01c27cSAndreas Gohr if(!$this->text && !$this->html) { 455bb01c27cSAndreas Gohr return false; 456bb01c27cSAndreas Gohr } 457bb01c27cSAndreas Gohr 458bb01c27cSAndreas Gohr // add general headers 459bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 460bb01c27cSAndreas Gohr 4611d045709SAndreas Gohr $body = ''; 4621d045709SAndreas Gohr 463bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)) { // we can send a simple single part message 464bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 465bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 466be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 467bb01c27cSAndreas Gohr } else { // multi part it is 4681d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 469bb01c27cSAndreas Gohr 470bb01c27cSAndreas Gohr // prepare the attachments 471bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 472bb01c27cSAndreas Gohr 473bb01c27cSAndreas Gohr // do we have alternative text content? 474bb01c27cSAndreas Gohr if($this->text && $this->html) { 475a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 476a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 477bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4781d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4791d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 480bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 481be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 482bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 483a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 484d6e04b60SAndreas Gohr ' boundary="'.$this->boundary.'";'.MAILHEADER_EOL. 485d6e04b60SAndreas Gohr ' type="text/html"'.MAILHEADER_EOL; 486bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 487bb01c27cSAndreas Gohr } 488bb01c27cSAndreas Gohr 4891d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4901d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4911d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 492bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 493be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL); 494bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 495bb01c27cSAndreas Gohr $body .= $attachments; 496bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 497bb01c27cSAndreas Gohr 498bb01c27cSAndreas Gohr // close open multipart/alternative boundary 499bb01c27cSAndreas Gohr if($this->text && $this->html) { 500bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 501bb01c27cSAndreas Gohr } 502bb01c27cSAndreas Gohr } 503bb01c27cSAndreas Gohr 504bb01c27cSAndreas Gohr return $body; 505bb01c27cSAndreas Gohr } 506bb01c27cSAndreas Gohr 507bb01c27cSAndreas Gohr /** 508a36fc348SAndreas Gohr * Cleanup and encode the headers array 509a36fc348SAndreas Gohr */ 510a36fc348SAndreas Gohr protected function cleanHeaders() { 511a36fc348SAndreas Gohr global $conf; 512a36fc348SAndreas Gohr 513a36fc348SAndreas Gohr // clean up addresses 514a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 515acbf061cSGerrit Uitslag $addrs = array('To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'); 516a36fc348SAndreas Gohr foreach($addrs as $addr) { 517a36fc348SAndreas Gohr if(isset($this->headers[$addr])) { 518a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 519a36fc348SAndreas Gohr } 520a36fc348SAndreas Gohr } 521a36fc348SAndreas Gohr 52245992a63SAndreas Gohr if(isset($this->headers['Subject'])) { 523a36fc348SAndreas Gohr // add prefix to subject 52454f30755SAndreas Gohr if(empty($conf['mailprefix'])) { 5258a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 52654f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 52754f30755SAndreas Gohr } else { 5288a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 5298a215f09SAndreas Gohr } 5308a215f09SAndreas Gohr } else { 531a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 53254f30755SAndreas Gohr } 533a36fc348SAndreas Gohr $len = strlen($prefix); 53445992a63SAndreas Gohr if(substr($this->headers['Subject'], 0, $len) != $prefix) { 53545992a63SAndreas Gohr $this->headers['Subject'] = $prefix.' '.$this->headers['Subject']; 536a36fc348SAndreas Gohr } 537a36fc348SAndreas Gohr 538a36fc348SAndreas Gohr // encode subject 539a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 54045992a63SAndreas Gohr $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']); 54145992a63SAndreas Gohr $this->headers['Subject'] = utf8_strip($this->headers['Subject']); 542a36fc348SAndreas Gohr } 543a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])) { 54445992a63SAndreas Gohr $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 545a36fc348SAndreas Gohr } 546a36fc348SAndreas Gohr } 547a36fc348SAndreas Gohr 548a36fc348SAndreas Gohr } 5491d8036c2SAndreas Gohr 5501d8036c2SAndreas Gohr /** 5511d8036c2SAndreas Gohr * Returns a complete, EOL terminated header line, wraps it if necessary 5521d8036c2SAndreas Gohr * 55342ea7f44SGerrit Uitslag * @param string $key 55442ea7f44SGerrit Uitslag * @param string $val 55542ea7f44SGerrit Uitslag * @return string line 5561d8036c2SAndreas Gohr */ 5571d8036c2SAndreas Gohr protected function wrappedHeaderLine($key, $val){ 5581d8036c2SAndreas Gohr return wordwrap("$key: $val", 78, MAILHEADER_EOL.' ').MAILHEADER_EOL; 559a36fc348SAndreas Gohr } 560a36fc348SAndreas Gohr 561a36fc348SAndreas Gohr /** 562bb01c27cSAndreas Gohr * Create a string from the headers array 5631d045709SAndreas Gohr * 5641d045709SAndreas Gohr * @returns string the headers 565bb01c27cSAndreas Gohr */ 566bb01c27cSAndreas Gohr protected function prepareHeaders() { 567bb01c27cSAndreas Gohr $headers = ''; 568bb01c27cSAndreas Gohr foreach($this->headers as $key => $val) { 5696be717dbSMichael Hamann if ($val === '' || is_null($val)) continue; 5701d8036c2SAndreas Gohr $headers .= $this->wrappedHeaderLine($key, $val); 571bb01c27cSAndreas Gohr } 572bb01c27cSAndreas Gohr return $headers; 573bb01c27cSAndreas Gohr } 574bb01c27cSAndreas Gohr 575bb01c27cSAndreas Gohr /** 576bb01c27cSAndreas Gohr * return a full email with all headers 577bb01c27cSAndreas Gohr * 5781d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5791d045709SAndreas Gohr * used for MHT exports 5801d045709SAndreas Gohr * 5811d045709SAndreas Gohr * @return string the mail, false on errors 582bb01c27cSAndreas Gohr */ 583bb01c27cSAndreas Gohr public function dump() { 584a36fc348SAndreas Gohr $this->cleanHeaders(); 585bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5864d18e936SAndreas Gohr if($body === false) return false; 5871d045709SAndreas Gohr $headers = $this->prepareHeaders(); 588bb01c27cSAndreas Gohr 589bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 590bb01c27cSAndreas Gohr } 5911d045709SAndreas Gohr 5921d045709SAndreas Gohr /** 5939ea45836SChristopher Smith * Prepare default token replacement strings 5949ea45836SChristopher Smith * 5959ea45836SChristopher Smith * Populates the '$replacements' property. 5969ea45836SChristopher Smith * Should be called by the class constructor 5979ea45836SChristopher Smith */ 5989ea45836SChristopher Smith protected function prepareTokenReplacements() { 5999ea45836SChristopher Smith global $INFO; 6009ea45836SChristopher Smith global $conf; 6019ea45836SChristopher Smith /* @var Input $INPUT */ 6029ea45836SChristopher Smith global $INPUT; 6039ea45836SChristopher Smith global $lang; 6049ea45836SChristopher Smith 6059ea45836SChristopher Smith $ip = clientIP(); 6069ea45836SChristopher Smith $cip = gethostsbyaddrs($ip); 6079ea45836SChristopher Smith 6089ea45836SChristopher Smith $this->replacements['text'] = array( 6099ea45836SChristopher Smith 'DATE' => dformat(), 6109ea45836SChristopher Smith 'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'), 6119ea45836SChristopher Smith 'IPADDRESS' => $ip, 6129ea45836SChristopher Smith 'HOSTNAME' => $cip, 6139ea45836SChristopher Smith 'TITLE' => $conf['title'], 6149ea45836SChristopher Smith 'DOKUWIKIURL' => DOKU_URL, 6159ea45836SChristopher Smith 'USER' => $INPUT->server->str('REMOTE_USER'), 6169ea45836SChristopher Smith 'NAME' => $INFO['userinfo']['name'], 617774514c9SGerrit Uitslag 'MAIL' => $INFO['userinfo']['mail'] 6189ea45836SChristopher Smith ); 619774514c9SGerrit Uitslag $signature = str_replace('@DOKUWIKIURL@', $this->replacements['text']['DOKUWIKIURL'], $lang['email_signature_text']); 620774514c9SGerrit Uitslag $this->replacements['text']['EMAILSIGNATURE'] = "\n-- \n" . $signature . "\n"; 6219ea45836SChristopher Smith 6229ea45836SChristopher Smith $this->replacements['html'] = array( 6239ea45836SChristopher Smith 'DATE' => '<i>' . hsc(dformat()) . '</i>', 6249ea45836SChristopher Smith 'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')), 6259ea45836SChristopher Smith 'IPADDRESS' => '<code>' . hsc($ip) . '</code>', 6269ea45836SChristopher Smith 'HOSTNAME' => '<code>' . hsc($cip) . '</code>', 6279ea45836SChristopher Smith 'TITLE' => hsc($conf['title']), 6289ea45836SChristopher Smith 'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>', 6299ea45836SChristopher Smith 'USER' => hsc($INPUT->server->str('REMOTE_USER')), 6309ea45836SChristopher Smith 'NAME' => hsc($INFO['userinfo']['name']), 6319ea45836SChristopher Smith 'MAIL' => '<a href="mailto:"' . hsc($INFO['userinfo']['mail']) . '">' . 632774514c9SGerrit Uitslag hsc($INFO['userinfo']['mail']) . '</a>' 6339ea45836SChristopher Smith ); 634774514c9SGerrit Uitslag $signature = $lang['email_signature_text']; 635774514c9SGerrit Uitslag if(!empty($lang['email_signature_html'])) { 636774514c9SGerrit Uitslag $signature = $lang['email_signature_html']; 637774514c9SGerrit Uitslag } 638774514c9SGerrit Uitslag $signature = str_replace( 639774514c9SGerrit Uitslag array( 640774514c9SGerrit Uitslag '@DOKUWIKIURL@', 641774514c9SGerrit Uitslag "\n" 642774514c9SGerrit Uitslag ), 643774514c9SGerrit Uitslag array( 644774514c9SGerrit Uitslag $this->replacements['html']['DOKUWIKIURL'], 645774514c9SGerrit Uitslag '<br />' 646774514c9SGerrit Uitslag ), 647774514c9SGerrit Uitslag $signature 648774514c9SGerrit Uitslag ); 649774514c9SGerrit Uitslag $this->replacements['html']['EMAILSIGNATURE'] = $signature; 6509ea45836SChristopher Smith } 6519ea45836SChristopher Smith 6529ea45836SChristopher Smith /** 6531d045709SAndreas Gohr * Send the mail 6541d045709SAndreas Gohr * 6551d045709SAndreas Gohr * Call this after all data was set 6561d045709SAndreas Gohr * 65728d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 6581d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 6591d045709SAndreas Gohr */ 6601d045709SAndreas Gohr public function send() { 66128d2ad80SAndreas Gohr $success = false; 662a36fc348SAndreas Gohr 66328d2ad80SAndreas Gohr // prepare hook data 66428d2ad80SAndreas Gohr $data = array( 66528d2ad80SAndreas Gohr // pass the whole mail class to plugin 66628d2ad80SAndreas Gohr 'mail' => $this, 66728d2ad80SAndreas Gohr // pass references for backward compatibility 66828d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 66928d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 67028d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 67128d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 67228d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 67328d2ad80SAndreas Gohr 'body' => &$this->text, 674a89c75afSAndreas Gohr 'params' => &$this->sendparam, 67528d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 67628d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 67728d2ad80SAndreas Gohr 'success' => &$success, 67828d2ad80SAndreas Gohr ); 67928d2ad80SAndreas Gohr 68028d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 68128d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 68228d2ad80SAndreas Gohr if($evt->advise_before(true)) { 68328d2ad80SAndreas Gohr // clean up before using the headers 684a36fc348SAndreas Gohr $this->cleanHeaders(); 685a36fc348SAndreas Gohr 6861d045709SAndreas Gohr // any recipients? 6871d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 6881d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 689a89c75afSAndreas Gohr trim($this->headers['Bcc']) === '' 690a89c75afSAndreas Gohr ) return false; 6911d045709SAndreas Gohr 6921d045709SAndreas Gohr // The To: header is special 6936be717dbSMichael Hamann if(array_key_exists('To', $this->headers)) { 6946be717dbSMichael Hamann $to = (string)$this->headers['To']; 6951d045709SAndreas Gohr unset($this->headers['To']); 6961d045709SAndreas Gohr } else { 6971d045709SAndreas Gohr $to = ''; 6981d045709SAndreas Gohr } 6991d045709SAndreas Gohr 7001d045709SAndreas Gohr // so is the subject 7016be717dbSMichael Hamann if(array_key_exists('Subject', $this->headers)) { 7026be717dbSMichael Hamann $subject = (string)$this->headers['Subject']; 7031d045709SAndreas Gohr unset($this->headers['Subject']); 7041d045709SAndreas Gohr } else { 7051d045709SAndreas Gohr $subject = ''; 7061d045709SAndreas Gohr } 7071d045709SAndreas Gohr 7081d045709SAndreas Gohr // make the body 7091d045709SAndreas Gohr $body = $this->prepareBody(); 7104c89a7f6SAndreas Gohr if($body === false) return false; 7111d045709SAndreas Gohr 7121d045709SAndreas Gohr // cook the headers 7131d045709SAndreas Gohr $headers = $this->prepareHeaders(); 71428d2ad80SAndreas Gohr // add any headers set by legacy plugins 71528d2ad80SAndreas Gohr if(trim($data['headers'])) { 71628d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 71728d2ad80SAndreas Gohr } 7181d045709SAndreas Gohr 7191d045709SAndreas Gohr // send the thing 7201d045709SAndreas Gohr if(is_null($this->sendparam)) { 72128d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers); 7221d045709SAndreas Gohr } else { 72328d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers, $this->sendparam); 7241d045709SAndreas Gohr } 7251d045709SAndreas Gohr } 72628d2ad80SAndreas Gohr // any AFTER actions? 72728d2ad80SAndreas Gohr $evt->advise_after(); 72828d2ad80SAndreas Gohr return $success; 72928d2ad80SAndreas Gohr } 730bb01c27cSAndreas Gohr} 731