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 351d045709SAndreas Gohr /** 361d045709SAndreas Gohr * Constructor 371d045709SAndreas Gohr * 381d045709SAndreas Gohr * Initializes the boundary strings and part counters 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); 64bb01c27cSAndreas Gohr } 65bb01c27cSAndreas Gohr 66bb01c27cSAndreas Gohr /** 67bb01c27cSAndreas Gohr * Attach a file 68bb01c27cSAndreas Gohr * 69a89c75afSAndreas Gohr * @param string $path Path to the file to attach 70a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 71a89c75afSAndreas Gohr * @param string $name The filename to use 72a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 73bb01c27cSAndreas Gohr */ 74bb01c27cSAndreas Gohr public function attachFile($path, $mime, $name = '', $embed = '') { 75bb01c27cSAndreas Gohr if(!$name) { 763009a773SAndreas Gohr $name = utf8_basename($path); 77bb01c27cSAndreas Gohr } 78bb01c27cSAndreas Gohr 79bb01c27cSAndreas Gohr $this->attach[] = array( 80bb01c27cSAndreas Gohr 'data' => file_get_contents($path), 81bb01c27cSAndreas Gohr 'mime' => $mime, 82bb01c27cSAndreas Gohr 'name' => $name, 83bb01c27cSAndreas Gohr 'embed' => $embed 84bb01c27cSAndreas Gohr ); 85bb01c27cSAndreas Gohr } 86bb01c27cSAndreas Gohr 87bb01c27cSAndreas Gohr /** 88bb01c27cSAndreas Gohr * Attach a file 89bb01c27cSAndreas Gohr * 90a89c75afSAndreas Gohr * @param string $data The file contents to attach 91a89c75afSAndreas Gohr * @param string $mime Mimetype of the attached file 92a89c75afSAndreas Gohr * @param string $name The filename to use 93a89c75afSAndreas Gohr * @param string $embed Unique key to reference this file from the HTML part 94bb01c27cSAndreas Gohr */ 95bb01c27cSAndreas Gohr public function attachContent($data, $mime, $name = '', $embed = '') { 96bb01c27cSAndreas Gohr if(!$name) { 97a89c75afSAndreas Gohr list(, $ext) = explode('/', $mime); 98bb01c27cSAndreas Gohr $name = count($this->attach).".$ext"; 99bb01c27cSAndreas Gohr } 100bb01c27cSAndreas Gohr 101bb01c27cSAndreas Gohr $this->attach[] = array( 102bb01c27cSAndreas Gohr 'data' => $data, 103bb01c27cSAndreas Gohr 'mime' => $mime, 104bb01c27cSAndreas Gohr 'name' => $name, 105bb01c27cSAndreas Gohr 'embed' => $embed 106bb01c27cSAndreas Gohr ); 107bb01c27cSAndreas Gohr } 108bb01c27cSAndreas Gohr 109bb01c27cSAndreas Gohr /** 110850dbf1fSAndreas Gohr * Callback function to automatically embed images referenced in HTML templates 111850dbf1fSAndreas Gohr */ 112850dbf1fSAndreas Gohr protected function autoembed_cb($matches) { 113850dbf1fSAndreas Gohr static $embeds = 0; 114850dbf1fSAndreas Gohr $embeds++; 115850dbf1fSAndreas Gohr 116850dbf1fSAndreas Gohr // get file and mime type 117850dbf1fSAndreas Gohr $media = cleanID($matches[1]); 118a89c75afSAndreas Gohr list(, $mime) = mimetype($media); 119850dbf1fSAndreas Gohr $file = mediaFN($media); 120850dbf1fSAndreas Gohr if(!file_exists($file)) return $matches[0]; //bad reference, keep as is 121850dbf1fSAndreas Gohr 122850dbf1fSAndreas Gohr // attach it and set placeholder 123850dbf1fSAndreas Gohr $this->attachFile($file, $mime, '', 'autoembed'.$embeds); 124850dbf1fSAndreas Gohr return '%%autoembed'.$embeds.'%%'; 125850dbf1fSAndreas Gohr } 126850dbf1fSAndreas Gohr 127850dbf1fSAndreas Gohr /** 1281d045709SAndreas Gohr * Add an arbitrary header to the mail 1291d045709SAndreas Gohr * 130a36fc348SAndreas Gohr * If an empy value is passed, the header is removed 131a36fc348SAndreas Gohr * 1321d045709SAndreas Gohr * @param string $header the header name (no trailing colon!) 133*59bc3b48SGerrit Uitslag * @param string|string[] $value the value of the header 1341d045709SAndreas Gohr * @param bool $clean remove all non-ASCII chars and line feeds? 1351d045709SAndreas Gohr */ 1361d045709SAndreas Gohr public function setHeader($header, $value, $clean = true) { 1379f3eca0bSAndreas Gohr $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing 1381d045709SAndreas Gohr if($clean) { 139578b2c23SAndreas Gohr $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header); 140578b2c23SAndreas Gohr $value = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value); 1411d045709SAndreas Gohr } 142a36fc348SAndreas Gohr 143a36fc348SAndreas Gohr // empty value deletes 144b6c97c70SAndreas Gohr if(is_array($value)){ 145b6c97c70SAndreas Gohr $value = array_map('trim', $value); 146b6c97c70SAndreas Gohr $value = array_filter($value); 147b6c97c70SAndreas Gohr if(!$value) $value = ''; 148b6c97c70SAndreas Gohr }else{ 149a36fc348SAndreas Gohr $value = trim($value); 150b6c97c70SAndreas Gohr } 151a36fc348SAndreas Gohr if($value === '') { 152a36fc348SAndreas Gohr if(isset($this->headers[$header])) unset($this->headers[$header]); 153a36fc348SAndreas Gohr } else { 1541d045709SAndreas Gohr $this->headers[$header] = $value; 1551d045709SAndreas Gohr } 156a36fc348SAndreas Gohr } 1571d045709SAndreas Gohr 1581d045709SAndreas Gohr /** 1591d045709SAndreas Gohr * Set additional parameters to be passed to sendmail 1601d045709SAndreas Gohr * 1611d045709SAndreas Gohr * Whatever is set here is directly passed to PHP's mail() command as last 1621d045709SAndreas Gohr * parameter. Depending on the PHP setup this might break mailing alltogether 1631d045709SAndreas Gohr */ 1641d045709SAndreas Gohr public function setParameters($param) { 1651d045709SAndreas Gohr $this->sendparam = $param; 1661d045709SAndreas Gohr } 1671d045709SAndreas Gohr 1681d045709SAndreas Gohr /** 169abbf0890SAndreas Gohr * Set the text and HTML body and apply replacements 170abbf0890SAndreas Gohr * 171abbf0890SAndreas Gohr * This function applies a whole bunch of default replacements in addition 172abbf0890SAndreas Gohr * to the ones specidifed as parameters 173abbf0890SAndreas Gohr * 174abbf0890SAndreas Gohr * If you pass the HTML part or HTML replacements yourself you have to make 175abbf0890SAndreas Gohr * sure you encode all HTML special chars correctly 176abbf0890SAndreas Gohr * 177abbf0890SAndreas Gohr * @param string $text plain text body 178abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 179abbf0890SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep 180*59bc3b48SGerrit Uitslag * @param string $html the HTML body, leave null to create it from $text 181f08086ecSAndreas Gohr * @param bool $wrap wrap the HTML in the default header/Footer 182abbf0890SAndreas Gohr */ 183f08086ecSAndreas Gohr public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true) { 184abbf0890SAndreas Gohr global $INFO; 185abbf0890SAndreas Gohr global $conf; 186585bf44eSChristopher Smith /* @var Input $INPUT */ 187585bf44eSChristopher Smith global $INPUT; 188585bf44eSChristopher Smith 18976efd6d0SAndreas Gohr $htmlrep = (array)$htmlrep; 19076efd6d0SAndreas Gohr $textrep = (array)$textrep; 191abbf0890SAndreas Gohr 192abbf0890SAndreas Gohr // create HTML from text if not given 193abbf0890SAndreas Gohr if(is_null($html)) { 194ba9c057bSAndreas Gohr $html = $text; 195ba9c057bSAndreas Gohr $html = hsc($html); 196ba9c057bSAndreas Gohr $html = preg_replace('/^-----*$/m', '<hr >', $html); 197ba9c057bSAndreas Gohr $html = nl2br($html); 198abbf0890SAndreas Gohr } 199f08086ecSAndreas Gohr if($wrap) { 200f08086ecSAndreas Gohr $wrap = rawLocale('mailwrap', 'html'); 201a4c4a73dSAndreas Gohr $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature 202f08086ecSAndreas Gohr $html = str_replace('@HTMLBODY@', $html, $wrap); 203f08086ecSAndreas Gohr } 204f08086ecSAndreas Gohr 20576efd6d0SAndreas Gohr // copy over all replacements missing for HTML (autolink URLs) 20676efd6d0SAndreas Gohr foreach($textrep as $key => $value) { 20776efd6d0SAndreas Gohr if(isset($htmlrep[$key])) continue; 2083e7e6067SKlap-in if(media_isexternal($value)) { 20976efd6d0SAndreas Gohr $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>'; 21076efd6d0SAndreas Gohr } else { 21176efd6d0SAndreas Gohr $htmlrep[$key] = hsc($value); 21276efd6d0SAndreas Gohr } 213abbf0890SAndreas Gohr } 214abbf0890SAndreas Gohr 215850dbf1fSAndreas Gohr // embed media from templates 216a89c75afSAndreas Gohr $html = preg_replace_callback( 217a89c75afSAndreas Gohr '/@MEDIA\(([^\)]+)\)@/', 218a89c75afSAndreas Gohr array($this, 'autoembed_cb'), $html 219a89c75afSAndreas Gohr ); 220850dbf1fSAndreas Gohr 221abbf0890SAndreas Gohr // prepare default replacements 222abbf0890SAndreas Gohr $ip = clientIP(); 223f08086ecSAndreas Gohr $cip = gethostsbyaddrs($ip); 224abbf0890SAndreas Gohr $trep = array( 225abbf0890SAndreas Gohr 'DATE' => dformat(), 226585bf44eSChristopher Smith 'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'), 227abbf0890SAndreas Gohr 'IPADDRESS' => $ip, 228f08086ecSAndreas Gohr 'HOSTNAME' => $cip, 229abbf0890SAndreas Gohr 'TITLE' => $conf['title'], 230abbf0890SAndreas Gohr 'DOKUWIKIURL' => DOKU_URL, 231585bf44eSChristopher Smith 'USER' => $INPUT->server->str('REMOTE_USER'), 232abbf0890SAndreas Gohr 'NAME' => $INFO['userinfo']['name'], 233abbf0890SAndreas Gohr 'MAIL' => $INFO['userinfo']['mail'], 234abbf0890SAndreas Gohr ); 235abbf0890SAndreas Gohr $trep = array_merge($trep, (array)$textrep); 236abbf0890SAndreas Gohr $hrep = array( 237abbf0890SAndreas Gohr 'DATE' => '<i>'.hsc(dformat()).'</i>', 238585bf44eSChristopher Smith 'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')), 239abbf0890SAndreas Gohr 'IPADDRESS' => '<code>'.hsc($ip).'</code>', 240f08086ecSAndreas Gohr 'HOSTNAME' => '<code>'.hsc($cip).'</code>', 241abbf0890SAndreas Gohr 'TITLE' => hsc($conf['title']), 242abbf0890SAndreas Gohr 'DOKUWIKIURL' => '<a href="'.DOKU_URL.'">'.DOKU_URL.'</a>', 243585bf44eSChristopher Smith 'USER' => hsc($INPUT->server->str('REMOTE_USER')), 244abbf0890SAndreas Gohr 'NAME' => hsc($INFO['userinfo']['name']), 245abbf0890SAndreas Gohr 'MAIL' => '<a href="mailto:"'.hsc($INFO['userinfo']['mail']).'">'. 246abbf0890SAndreas Gohr hsc($INFO['userinfo']['mail']).'</a>', 247abbf0890SAndreas Gohr ); 248abbf0890SAndreas Gohr $hrep = array_merge($hrep, (array)$htmlrep); 249abbf0890SAndreas Gohr 250abbf0890SAndreas Gohr // Apply replacements 251abbf0890SAndreas Gohr foreach($trep as $key => $substitution) { 252abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@', $substitution, $text); 253abbf0890SAndreas Gohr } 254abbf0890SAndreas Gohr foreach($hrep as $key => $substitution) { 255abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@', $substitution, $html); 256abbf0890SAndreas Gohr } 257abbf0890SAndreas Gohr 258abbf0890SAndreas Gohr $this->setHTML($html); 259abbf0890SAndreas Gohr $this->setText($text); 260abbf0890SAndreas Gohr } 261abbf0890SAndreas Gohr 262abbf0890SAndreas Gohr /** 263bb01c27cSAndreas Gohr * Set the HTML part of the mail 264bb01c27cSAndreas Gohr * 265bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 266abbf0890SAndreas Gohr * 267abbf0890SAndreas Gohr * You probably want to use setBody() instead 268bb01c27cSAndreas Gohr */ 2691d045709SAndreas Gohr public function setHTML($html) { 270bb01c27cSAndreas Gohr $this->html = $html; 271bb01c27cSAndreas Gohr } 272bb01c27cSAndreas Gohr 273bb01c27cSAndreas Gohr /** 274bb01c27cSAndreas Gohr * Set the plain text part of the mail 275abbf0890SAndreas Gohr * 276abbf0890SAndreas Gohr * You probably want to use setBody() instead 277bb01c27cSAndreas Gohr */ 2781d045709SAndreas Gohr public function setText($text) { 279bb01c27cSAndreas Gohr $this->text = $text; 280bb01c27cSAndreas Gohr } 281bb01c27cSAndreas Gohr 282bb01c27cSAndreas Gohr /** 283a36fc348SAndreas Gohr * Add the To: recipients 284a36fc348SAndreas Gohr * 2858c253612SGerrit Uitslag * @see cleanAddress 286*59bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 287a36fc348SAndreas Gohr */ 288a36fc348SAndreas Gohr public function to($address) { 289a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 290a36fc348SAndreas Gohr } 291a36fc348SAndreas Gohr 292a36fc348SAndreas Gohr /** 293a36fc348SAndreas Gohr * Add the Cc: recipients 294a36fc348SAndreas Gohr * 2958c253612SGerrit Uitslag * @see cleanAddress 296*59bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 297a36fc348SAndreas Gohr */ 298a36fc348SAndreas Gohr public function cc($address) { 299a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 300a36fc348SAndreas Gohr } 301a36fc348SAndreas Gohr 302a36fc348SAndreas Gohr /** 303a36fc348SAndreas Gohr * Add the Bcc: recipients 304a36fc348SAndreas Gohr * 3058c253612SGerrit Uitslag * @see cleanAddress 306*59bc3b48SGerrit Uitslag * @param string|string[] $address Multiple adresses separated by commas or as array 307a36fc348SAndreas Gohr */ 308a36fc348SAndreas Gohr public function bcc($address) { 309a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 310a36fc348SAndreas Gohr } 311a36fc348SAndreas Gohr 312a36fc348SAndreas Gohr /** 313a36fc348SAndreas Gohr * Add the From: address 314a36fc348SAndreas Gohr * 315a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 316a36fc348SAndreas Gohr * to call this function 317a36fc348SAndreas Gohr * 3188c253612SGerrit Uitslag * @see cleanAddress 319a36fc348SAndreas Gohr * @param string $address from address 320a36fc348SAndreas Gohr */ 321a36fc348SAndreas Gohr public function from($address) { 322a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 323a36fc348SAndreas Gohr } 324a36fc348SAndreas Gohr 325a36fc348SAndreas Gohr /** 326a36fc348SAndreas Gohr * Add the mail's Subject: header 327a36fc348SAndreas Gohr * 328a36fc348SAndreas Gohr * @param string $subject the mail subject 329a36fc348SAndreas Gohr */ 330a36fc348SAndreas Gohr public function subject($subject) { 331a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 332a36fc348SAndreas Gohr } 333a36fc348SAndreas Gohr 334a36fc348SAndreas Gohr /** 3351d045709SAndreas Gohr * Sets an email address header with correct encoding 336bb01c27cSAndreas Gohr * 337bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 338bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 339bb01c27cSAndreas Gohr * 340bb01c27cSAndreas Gohr * Example: 3418c253612SGerrit Uitslag * cc("föö <foo@bar.com>, me@somewhere.com","TBcc"); 342bb01c27cSAndreas Gohr * 3438c253612SGerrit Uitslag * @param string|array $addresses Multiple adresses separated by commas or as array 344a89c75afSAndreas Gohr * @return bool|string the prepared header (can contain multiple lines) 345bb01c27cSAndreas Gohr */ 346b6c97c70SAndreas Gohr public function cleanAddress($addresses) { 347bb01c27cSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 348bb01c27cSAndreas Gohr $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 349bb01c27cSAndreas Gohr 350bb01c27cSAndreas Gohr $headers = ''; 351b6c97c70SAndreas Gohr if(!is_array($addresses)){ 352b6c97c70SAndreas Gohr $addresses = explode(',', $addresses); 353b6c97c70SAndreas Gohr } 354b6c97c70SAndreas Gohr 355b6c97c70SAndreas Gohr foreach($addresses as $part) { 356b6c97c70SAndreas Gohr $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors 357bb01c27cSAndreas Gohr $part = trim($part); 358bb01c27cSAndreas Gohr 359bb01c27cSAndreas Gohr // parse address 360bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) { 361bb01c27cSAndreas Gohr $text = trim($matches[1]); 362bb01c27cSAndreas Gohr $addr = $matches[2]; 363bb01c27cSAndreas Gohr } else { 364bb01c27cSAndreas Gohr $addr = $part; 365bb01c27cSAndreas Gohr } 366bb01c27cSAndreas Gohr // skip empty ones 367bb01c27cSAndreas Gohr if(empty($addr)) { 368bb01c27cSAndreas Gohr continue; 369bb01c27cSAndreas Gohr } 370bb01c27cSAndreas Gohr 371bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 372bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)) { 373bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"), -1); 374bb01c27cSAndreas Gohr continue; 375bb01c27cSAndreas Gohr } 376bb01c27cSAndreas Gohr 3771d045709SAndreas Gohr if(is_null($this->validator)) { 3781d045709SAndreas Gohr $this->validator = new EmailAddressValidator(); 3791d045709SAndreas Gohr $this->validator->allowLocalAddresses = true; 3801d045709SAndreas Gohr } 3811d045709SAndreas Gohr if(!$this->validator->check_email_address($addr)) { 382bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"), -1); 383bb01c27cSAndreas Gohr continue; 384bb01c27cSAndreas Gohr } 385bb01c27cSAndreas Gohr 386bb01c27cSAndreas Gohr // text was given 387bb01c27cSAndreas Gohr if(!empty($text) && $names) { 388bb01c27cSAndreas Gohr // add address quotes 389bb01c27cSAndreas Gohr $addr = "<$addr>"; 390bb01c27cSAndreas Gohr 391bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 392bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 393bb01c27cSAndreas Gohr $text = utf8_strip($text); 394bb01c27cSAndreas Gohr } 395bb01c27cSAndreas Gohr 396b6c97c70SAndreas Gohr if(strpos($text, ',') !== false || !utf8_isASCII($text)) { 397bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 398bb01c27cSAndreas Gohr } 399bb01c27cSAndreas Gohr } else { 400bb01c27cSAndreas Gohr $text = ''; 401bb01c27cSAndreas Gohr } 402bb01c27cSAndreas Gohr 403bb01c27cSAndreas Gohr // add to header comma seperated 404bb01c27cSAndreas Gohr if($headers != '') { 405bb01c27cSAndreas Gohr $headers .= ', '; 406bb01c27cSAndreas Gohr } 407bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 408bb01c27cSAndreas Gohr } 409bb01c27cSAndreas Gohr 410b6c97c70SAndreas Gohr $headers = trim($headers); 411bb01c27cSAndreas Gohr if(empty($headers)) return false; 412bb01c27cSAndreas Gohr 413bb01c27cSAndreas Gohr return $headers; 414bb01c27cSAndreas Gohr } 415bb01c27cSAndreas Gohr 416bb01c27cSAndreas Gohr 417bb01c27cSAndreas Gohr /** 418bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 419bb01c27cSAndreas Gohr * 420bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 421bb01c27cSAndreas Gohr */ 422bb01c27cSAndreas Gohr protected function prepareAttachments() { 423bb01c27cSAndreas Gohr $mime = ''; 424bb01c27cSAndreas Gohr $part = 1; 425bb01c27cSAndreas Gohr // embedded attachments 426bb01c27cSAndreas Gohr foreach($this->attach as $media) { 427ce9d2cc8SAndreas Gohr $media['name'] = str_replace(':', '_', cleanID($media['name'], true)); 428ce9d2cc8SAndreas Gohr 429bb01c27cSAndreas Gohr // create content id 430bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 431bb01c27cSAndreas Gohr 432bb01c27cSAndreas Gohr // replace wildcards 433bb01c27cSAndreas Gohr if($media['embed']) { 434bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html); 435bb01c27cSAndreas Gohr } 436bb01c27cSAndreas Gohr 437bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 4381d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"'); 4391d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64'); 4401d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-ID',"<$cid>"); 441bb01c27cSAndreas Gohr if($media['embed']) { 4421d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']); 443bb01c27cSAndreas Gohr } else { 4441d8036c2SAndreas Gohr $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']); 445bb01c27cSAndreas Gohr } 446bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 447bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL); 448bb01c27cSAndreas Gohr 449bb01c27cSAndreas Gohr $part++; 450bb01c27cSAndreas Gohr } 451bb01c27cSAndreas Gohr return $mime; 452bb01c27cSAndreas Gohr } 453bb01c27cSAndreas Gohr 4541d045709SAndreas Gohr /** 4551d045709SAndreas Gohr * Build the body and handles multi part mails 4561d045709SAndreas Gohr * 4571d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4581d045709SAndreas Gohr * 4591d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4601d045709SAndreas Gohr */ 4611d045709SAndreas Gohr protected function prepareBody() { 4621d045709SAndreas Gohr 4632398a2b5SAndreas Gohr // no HTML mails allowed? remove HTML body 4642398a2b5SAndreas Gohr if(!$this->allowhtml) { 4652398a2b5SAndreas Gohr $this->html = ''; 4662398a2b5SAndreas Gohr } 4672398a2b5SAndreas Gohr 468bb01c27cSAndreas Gohr // check for body 469bb01c27cSAndreas Gohr if(!$this->text && !$this->html) { 470bb01c27cSAndreas Gohr return false; 471bb01c27cSAndreas Gohr } 472bb01c27cSAndreas Gohr 473bb01c27cSAndreas Gohr // add general headers 474bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 475bb01c27cSAndreas Gohr 4761d045709SAndreas Gohr $body = ''; 4771d045709SAndreas Gohr 478bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)) { // we can send a simple single part message 479bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 480bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 481be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 482bb01c27cSAndreas Gohr } else { // multi part it is 4831d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 484bb01c27cSAndreas Gohr 485bb01c27cSAndreas Gohr // prepare the attachments 486bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 487bb01c27cSAndreas Gohr 488bb01c27cSAndreas Gohr // do we have alternative text content? 489bb01c27cSAndreas Gohr if($this->text && $this->html) { 490a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 491a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 492bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4931d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4941d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 495bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 496be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL); 497bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 498a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 499d6e04b60SAndreas Gohr ' boundary="'.$this->boundary.'";'.MAILHEADER_EOL. 500d6e04b60SAndreas Gohr ' type="text/html"'.MAILHEADER_EOL; 501bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 502bb01c27cSAndreas Gohr } 503bb01c27cSAndreas Gohr 5041d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 5051d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 5061d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 507bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 508be3cc6abSAndreas Gohr $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL); 509bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 510bb01c27cSAndreas Gohr $body .= $attachments; 511bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 512bb01c27cSAndreas Gohr 513bb01c27cSAndreas Gohr // close open multipart/alternative boundary 514bb01c27cSAndreas Gohr if($this->text && $this->html) { 515bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 516bb01c27cSAndreas Gohr } 517bb01c27cSAndreas Gohr } 518bb01c27cSAndreas Gohr 519bb01c27cSAndreas Gohr return $body; 520bb01c27cSAndreas Gohr } 521bb01c27cSAndreas Gohr 522bb01c27cSAndreas Gohr /** 523a36fc348SAndreas Gohr * Cleanup and encode the headers array 524a36fc348SAndreas Gohr */ 525a36fc348SAndreas Gohr protected function cleanHeaders() { 526a36fc348SAndreas Gohr global $conf; 527a36fc348SAndreas Gohr 528a36fc348SAndreas Gohr // clean up addresses 529a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 530acbf061cSGerrit Uitslag $addrs = array('To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'); 531a36fc348SAndreas Gohr foreach($addrs as $addr) { 532a36fc348SAndreas Gohr if(isset($this->headers[$addr])) { 533a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 534a36fc348SAndreas Gohr } 535a36fc348SAndreas Gohr } 536a36fc348SAndreas Gohr 53745992a63SAndreas Gohr if(isset($this->headers['Subject'])) { 538a36fc348SAndreas Gohr // add prefix to subject 53954f30755SAndreas Gohr if(empty($conf['mailprefix'])) { 5408a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 54154f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 54254f30755SAndreas Gohr } else { 5438a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 5448a215f09SAndreas Gohr } 5458a215f09SAndreas Gohr } else { 546a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 54754f30755SAndreas Gohr } 548a36fc348SAndreas Gohr $len = strlen($prefix); 54945992a63SAndreas Gohr if(substr($this->headers['Subject'], 0, $len) != $prefix) { 55045992a63SAndreas Gohr $this->headers['Subject'] = $prefix.' '.$this->headers['Subject']; 551a36fc348SAndreas Gohr } 552a36fc348SAndreas Gohr 553a36fc348SAndreas Gohr // encode subject 554a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')) { 55545992a63SAndreas Gohr $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']); 55645992a63SAndreas Gohr $this->headers['Subject'] = utf8_strip($this->headers['Subject']); 557a36fc348SAndreas Gohr } 558a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])) { 55945992a63SAndreas Gohr $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 560a36fc348SAndreas Gohr } 561a36fc348SAndreas Gohr } 562a36fc348SAndreas Gohr 563a36fc348SAndreas Gohr } 5641d8036c2SAndreas Gohr 5651d8036c2SAndreas Gohr /** 5661d8036c2SAndreas Gohr * Returns a complete, EOL terminated header line, wraps it if necessary 5671d8036c2SAndreas Gohr * 5681d8036c2SAndreas Gohr * @param $key 5691d8036c2SAndreas Gohr * @param $val 5701d8036c2SAndreas Gohr * @return string 5711d8036c2SAndreas Gohr */ 5721d8036c2SAndreas Gohr protected function wrappedHeaderLine($key, $val){ 5731d8036c2SAndreas Gohr return wordwrap("$key: $val", 78, MAILHEADER_EOL.' ').MAILHEADER_EOL; 574a36fc348SAndreas Gohr } 575a36fc348SAndreas Gohr 576a36fc348SAndreas Gohr /** 577bb01c27cSAndreas Gohr * Create a string from the headers array 5781d045709SAndreas Gohr * 5791d045709SAndreas Gohr * @returns string the headers 580bb01c27cSAndreas Gohr */ 581bb01c27cSAndreas Gohr protected function prepareHeaders() { 582bb01c27cSAndreas Gohr $headers = ''; 583bb01c27cSAndreas Gohr foreach($this->headers as $key => $val) { 5846be717dbSMichael Hamann if ($val === '' || is_null($val)) continue; 5851d8036c2SAndreas Gohr $headers .= $this->wrappedHeaderLine($key, $val); 586bb01c27cSAndreas Gohr } 587bb01c27cSAndreas Gohr return $headers; 588bb01c27cSAndreas Gohr } 589bb01c27cSAndreas Gohr 590bb01c27cSAndreas Gohr /** 591bb01c27cSAndreas Gohr * return a full email with all headers 592bb01c27cSAndreas Gohr * 5931d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5941d045709SAndreas Gohr * used for MHT exports 5951d045709SAndreas Gohr * 5961d045709SAndreas Gohr * @return string the mail, false on errors 597bb01c27cSAndreas Gohr */ 598bb01c27cSAndreas Gohr public function dump() { 599a36fc348SAndreas Gohr $this->cleanHeaders(); 600bb01c27cSAndreas Gohr $body = $this->prepareBody(); 6014d18e936SAndreas Gohr if($body === false) return false; 6021d045709SAndreas Gohr $headers = $this->prepareHeaders(); 603bb01c27cSAndreas Gohr 604bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 605bb01c27cSAndreas Gohr } 6061d045709SAndreas Gohr 6071d045709SAndreas Gohr /** 6081d045709SAndreas Gohr * Send the mail 6091d045709SAndreas Gohr * 6101d045709SAndreas Gohr * Call this after all data was set 6111d045709SAndreas Gohr * 61228d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 6131d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 6141d045709SAndreas Gohr */ 6151d045709SAndreas Gohr public function send() { 61628d2ad80SAndreas Gohr $success = false; 617a36fc348SAndreas Gohr 61828d2ad80SAndreas Gohr // prepare hook data 61928d2ad80SAndreas Gohr $data = array( 62028d2ad80SAndreas Gohr // pass the whole mail class to plugin 62128d2ad80SAndreas Gohr 'mail' => $this, 62228d2ad80SAndreas Gohr // pass references for backward compatibility 62328d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 62428d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 62528d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 62628d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 62728d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 62828d2ad80SAndreas Gohr 'body' => &$this->text, 629a89c75afSAndreas Gohr 'params' => &$this->sendparam, 63028d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 63128d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 63228d2ad80SAndreas Gohr 'success' => &$success, 63328d2ad80SAndreas Gohr ); 63428d2ad80SAndreas Gohr 63528d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 63628d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 63728d2ad80SAndreas Gohr if($evt->advise_before(true)) { 63828d2ad80SAndreas Gohr // clean up before using the headers 639a36fc348SAndreas Gohr $this->cleanHeaders(); 640a36fc348SAndreas Gohr 6411d045709SAndreas Gohr // any recipients? 6421d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 6431d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 644a89c75afSAndreas Gohr trim($this->headers['Bcc']) === '' 645a89c75afSAndreas Gohr ) return false; 6461d045709SAndreas Gohr 6471d045709SAndreas Gohr // The To: header is special 6486be717dbSMichael Hamann if(array_key_exists('To', $this->headers)) { 6496be717dbSMichael Hamann $to = (string)$this->headers['To']; 6501d045709SAndreas Gohr unset($this->headers['To']); 6511d045709SAndreas Gohr } else { 6521d045709SAndreas Gohr $to = ''; 6531d045709SAndreas Gohr } 6541d045709SAndreas Gohr 6551d045709SAndreas Gohr // so is the subject 6566be717dbSMichael Hamann if(array_key_exists('Subject', $this->headers)) { 6576be717dbSMichael Hamann $subject = (string)$this->headers['Subject']; 6581d045709SAndreas Gohr unset($this->headers['Subject']); 6591d045709SAndreas Gohr } else { 6601d045709SAndreas Gohr $subject = ''; 6611d045709SAndreas Gohr } 6621d045709SAndreas Gohr 6631d045709SAndreas Gohr // make the body 6641d045709SAndreas Gohr $body = $this->prepareBody(); 6654c89a7f6SAndreas Gohr if($body === false) return false; 6661d045709SAndreas Gohr 6671d045709SAndreas Gohr // cook the headers 6681d045709SAndreas Gohr $headers = $this->prepareHeaders(); 66928d2ad80SAndreas Gohr // add any headers set by legacy plugins 67028d2ad80SAndreas Gohr if(trim($data['headers'])) { 67128d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 67228d2ad80SAndreas Gohr } 6731d045709SAndreas Gohr 6741d045709SAndreas Gohr // send the thing 6751d045709SAndreas Gohr if(is_null($this->sendparam)) { 67628d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers); 6771d045709SAndreas Gohr } else { 67828d2ad80SAndreas Gohr $success = @mail($to, $subject, $body, $headers, $this->sendparam); 6791d045709SAndreas Gohr } 6801d045709SAndreas Gohr } 68128d2ad80SAndreas Gohr // any AFTER actions? 68228d2ad80SAndreas Gohr $evt->advise_after(); 68328d2ad80SAndreas Gohr return $success; 68428d2ad80SAndreas Gohr } 685bb01c27cSAndreas Gohr} 686