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 17bb01c27cSAndreas Gohrclass Mailer { 18bb01c27cSAndreas Gohr 19bb01c27cSAndreas Gohr private $headers = array(); 20bb01c27cSAndreas Gohr private $attach = array(); 21bb01c27cSAndreas Gohr private $html = ''; 22bb01c27cSAndreas Gohr private $text = ''; 23bb01c27cSAndreas Gohr 24bb01c27cSAndreas Gohr private $boundary = ''; 25bb01c27cSAndreas Gohr private $partid = ''; 261d045709SAndreas Gohr private $sendparam= null; 27bb01c27cSAndreas Gohr 281d045709SAndreas Gohr private $validator = null; 29*2398a2b5SAndreas Gohr private $allowhtml = true; 301d045709SAndreas Gohr 311d045709SAndreas Gohr /** 321d045709SAndreas Gohr * Constructor 331d045709SAndreas Gohr * 341d045709SAndreas Gohr * Initializes the boundary strings and part counters 351d045709SAndreas Gohr */ 361d045709SAndreas Gohr public function __construct(){ 379f3eca0bSAndreas Gohr global $conf; 389f3eca0bSAndreas Gohr 399f3eca0bSAndreas Gohr $server = parse_url(DOKU_URL,PHP_URL_HOST); 401d045709SAndreas Gohr 411d045709SAndreas Gohr $this->partid = md5(uniqid(rand(),true)).'@'.$server; 42bb01c27cSAndreas Gohr $this->boundary = '----------'.md5(uniqid(rand(),true)); 439f3eca0bSAndreas Gohr 449f3eca0bSAndreas Gohr $listid = join('.',array_reverse(explode('/',DOKU_BASE))).$server; 459f3eca0bSAndreas Gohr $listid = strtolower(trim($listid,'.')); 469f3eca0bSAndreas Gohr 47*2398a2b5SAndreas Gohr $this->allowhtml = (bool) $conf['htmlmail']; 48*2398a2b5SAndreas Gohr 499f3eca0bSAndreas Gohr // add some default headers for mailfiltering FS#2247 509f3eca0bSAndreas Gohr $this->setHeader('X-Mailer','DokuWiki '.getVersion()); 519f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-User', $_SERVER['REMOTE_USER']); 529f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Title', $conf['title']); 539f3eca0bSAndreas Gohr $this->setHeader('X-DokuWiki-Server', $server); 549f3eca0bSAndreas Gohr $this->setHeader('X-Auto-Response-Suppress', 'OOF'); 559f3eca0bSAndreas Gohr $this->setHeader('List-Id',$conf['title'].' <'.$listid.'>'); 56bb01c27cSAndreas Gohr } 57bb01c27cSAndreas Gohr 58bb01c27cSAndreas Gohr /** 59bb01c27cSAndreas Gohr * Attach a file 60bb01c27cSAndreas Gohr * 61bb01c27cSAndreas Gohr * @param $path Path to the file to attach 62bb01c27cSAndreas Gohr * @param $mime Mimetype of the attached file 63bb01c27cSAndreas Gohr * @param $name The filename to use 64bb01c27cSAndreas Gohr * @param $embed Unique key to reference this file from the HTML part 65bb01c27cSAndreas Gohr */ 66bb01c27cSAndreas Gohr public function attachFile($path,$mime,$name='',$embed=''){ 67bb01c27cSAndreas Gohr if(!$name){ 68bb01c27cSAndreas Gohr $name = basename($path); 69bb01c27cSAndreas Gohr } 70bb01c27cSAndreas Gohr 71bb01c27cSAndreas Gohr $this->attach[] = array( 72bb01c27cSAndreas Gohr 'data' => file_get_contents($path), 73bb01c27cSAndreas Gohr 'mime' => $mime, 74bb01c27cSAndreas Gohr 'name' => $name, 75bb01c27cSAndreas Gohr 'embed' => $embed 76bb01c27cSAndreas Gohr ); 77bb01c27cSAndreas Gohr } 78bb01c27cSAndreas Gohr 79bb01c27cSAndreas Gohr /** 80bb01c27cSAndreas Gohr * Attach a file 81bb01c27cSAndreas Gohr * 82bb01c27cSAndreas Gohr * @param $path The file contents to attach 83bb01c27cSAndreas Gohr * @param $mime Mimetype of the attached file 84bb01c27cSAndreas Gohr * @param $name The filename to use 85bb01c27cSAndreas Gohr * @param $embed Unique key to reference this file from the HTML part 86bb01c27cSAndreas Gohr */ 87bb01c27cSAndreas Gohr public function attachContent($data,$mime,$name='',$embed=''){ 88bb01c27cSAndreas Gohr if(!$name){ 89bb01c27cSAndreas Gohr list($junk,$ext) = split('/',$mime); 90bb01c27cSAndreas Gohr $name = count($this->attach).".$ext"; 91bb01c27cSAndreas Gohr } 92bb01c27cSAndreas Gohr 93bb01c27cSAndreas Gohr $this->attach[] = array( 94bb01c27cSAndreas Gohr 'data' => $data, 95bb01c27cSAndreas Gohr 'mime' => $mime, 96bb01c27cSAndreas Gohr 'name' => $name, 97bb01c27cSAndreas Gohr 'embed' => $embed 98bb01c27cSAndreas Gohr ); 99bb01c27cSAndreas Gohr } 100bb01c27cSAndreas Gohr 101bb01c27cSAndreas Gohr /** 102850dbf1fSAndreas Gohr * Callback function to automatically embed images referenced in HTML templates 103850dbf1fSAndreas Gohr */ 104850dbf1fSAndreas Gohr protected function autoembed_cb($matches){ 105850dbf1fSAndreas Gohr static $embeds = 0; 106850dbf1fSAndreas Gohr $embeds++; 107850dbf1fSAndreas Gohr 108850dbf1fSAndreas Gohr // get file and mime type 109850dbf1fSAndreas Gohr $media = cleanID($matches[1]); 110850dbf1fSAndreas Gohr list($ext, $mime) = mimetype($media); 111850dbf1fSAndreas Gohr $file = mediaFN($media); 112850dbf1fSAndreas Gohr if(!file_exists($file)) return $matches[0]; //bad reference, keep as is 113850dbf1fSAndreas Gohr 114850dbf1fSAndreas Gohr // attach it and set placeholder 115850dbf1fSAndreas Gohr $this->attachFile($file,$mime,'','autoembed'.$embeds); 116850dbf1fSAndreas Gohr return '%%autoembed'.$embeds.'%%'; 117850dbf1fSAndreas Gohr } 118850dbf1fSAndreas Gohr 119850dbf1fSAndreas Gohr /** 1201d045709SAndreas Gohr * Add an arbitrary header to the mail 1211d045709SAndreas Gohr * 122a36fc348SAndreas Gohr * If an empy value is passed, the header is removed 123a36fc348SAndreas Gohr * 1241d045709SAndreas Gohr * @param string $header the header name (no trailing colon!) 1251d045709SAndreas Gohr * @param string $value the value of the header 1261d045709SAndreas Gohr * @param bool $clean remove all non-ASCII chars and line feeds? 1271d045709SAndreas Gohr */ 1281d045709SAndreas Gohr public function setHeader($header,$value,$clean=true){ 1299f3eca0bSAndreas Gohr $header = str_replace(' ','-',ucwords(strtolower(str_replace('-',' ',$header)))); // streamline casing 1301d045709SAndreas Gohr if($clean){ 1311d045709SAndreas Gohr $header = preg_replace('/[^\w \-\.\+\@]+/','',$header); 1329f3eca0bSAndreas Gohr $value = preg_replace('/[^\w \-\.\+\@<>]+/','',$value); 1331d045709SAndreas Gohr } 134a36fc348SAndreas Gohr 135a36fc348SAndreas Gohr // empty value deletes 136a36fc348SAndreas Gohr $value = trim($value); 137a36fc348SAndreas Gohr if($value === ''){ 138a36fc348SAndreas Gohr if(isset($this->headers[$header])) unset($this->headers[$header]); 139a36fc348SAndreas Gohr }else{ 1401d045709SAndreas Gohr $this->headers[$header] = $value; 1411d045709SAndreas Gohr } 142a36fc348SAndreas Gohr } 1431d045709SAndreas Gohr 1441d045709SAndreas Gohr /** 1451d045709SAndreas Gohr * Set additional parameters to be passed to sendmail 1461d045709SAndreas Gohr * 1471d045709SAndreas Gohr * Whatever is set here is directly passed to PHP's mail() command as last 1481d045709SAndreas Gohr * parameter. Depending on the PHP setup this might break mailing alltogether 1491d045709SAndreas Gohr */ 1501d045709SAndreas Gohr public function setParameters($param){ 1511d045709SAndreas Gohr $this->sendparam = $param; 1521d045709SAndreas Gohr } 1531d045709SAndreas Gohr 1541d045709SAndreas Gohr /** 155abbf0890SAndreas Gohr * Set the text and HTML body and apply replacements 156abbf0890SAndreas Gohr * 157abbf0890SAndreas Gohr * This function applies a whole bunch of default replacements in addition 158abbf0890SAndreas Gohr * to the ones specidifed as parameters 159abbf0890SAndreas Gohr * 160abbf0890SAndreas Gohr * If you pass the HTML part or HTML replacements yourself you have to make 161abbf0890SAndreas Gohr * sure you encode all HTML special chars correctly 162abbf0890SAndreas Gohr * 163abbf0890SAndreas Gohr * @param string $text plain text body 164abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 165abbf0890SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep 166abbf0890SAndreas Gohr * @param array $html the HTML body, leave null to create it from $text 167f08086ecSAndreas Gohr * @param bool $wrap wrap the HTML in the default header/Footer 168abbf0890SAndreas Gohr */ 169f08086ecSAndreas Gohr public function setBody($text, $textrep=null, $htmlrep=null, $html=null, $wrap=true){ 170abbf0890SAndreas Gohr global $INFO; 171abbf0890SAndreas Gohr global $conf; 17276efd6d0SAndreas Gohr $htmlrep = (array) $htmlrep; 17376efd6d0SAndreas Gohr $textrep = (array) $textrep; 174abbf0890SAndreas Gohr 175abbf0890SAndreas Gohr // create HTML from text if not given 176abbf0890SAndreas Gohr if(is_null($html)){ 177ba9c057bSAndreas Gohr $html = $text; 178ba9c057bSAndreas Gohr $html = hsc($html); 179ba9c057bSAndreas Gohr $html = preg_replace('/^-----*$/m','<hr >',$html); 180ba9c057bSAndreas Gohr $html = nl2br($html); 181abbf0890SAndreas Gohr } 182f08086ecSAndreas Gohr if($wrap){ 183f08086ecSAndreas Gohr $wrap = rawLocale('mailwrap','html'); 184a4c4a73dSAndreas Gohr $html = preg_replace('/\n-- <br \/>.*$/s','',$html); //strip signature 185f08086ecSAndreas Gohr $html = str_replace('@HTMLBODY@',$html,$wrap); 186f08086ecSAndreas Gohr } 187f08086ecSAndreas Gohr 18876efd6d0SAndreas Gohr // copy over all replacements missing for HTML (autolink URLs) 18976efd6d0SAndreas Gohr foreach($textrep as $key => $value){ 19076efd6d0SAndreas Gohr if(isset($htmlrep[$key])) continue; 19176efd6d0SAndreas Gohr if(preg_match('/^https?:\/\//i',$value)){ 19276efd6d0SAndreas Gohr $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>'; 19376efd6d0SAndreas Gohr }else{ 19476efd6d0SAndreas Gohr $htmlrep[$key] = hsc($value); 19576efd6d0SAndreas Gohr } 196abbf0890SAndreas Gohr } 197abbf0890SAndreas Gohr 198850dbf1fSAndreas Gohr // embed media from templates 199850dbf1fSAndreas Gohr $html = preg_replace_callback('/@MEDIA\(([^\)]+)\)@/', 200850dbf1fSAndreas Gohr array($this,'autoembed_cb'),$html); 201850dbf1fSAndreas Gohr 202abbf0890SAndreas Gohr // prepare default replacements 203abbf0890SAndreas Gohr $ip = clientIP(); 204f08086ecSAndreas Gohr $cip = gethostsbyaddrs($ip); 205abbf0890SAndreas Gohr $trep = array( 206abbf0890SAndreas Gohr 'DATE' => dformat(), 207abbf0890SAndreas Gohr 'BROWSER' => $_SERVER['HTTP_USER_AGENT'], 208abbf0890SAndreas Gohr 'IPADDRESS' => $ip, 209f08086ecSAndreas Gohr 'HOSTNAME' => $cip, 210abbf0890SAndreas Gohr 'TITLE' => $conf['title'], 211abbf0890SAndreas Gohr 'DOKUWIKIURL' => DOKU_URL, 212abbf0890SAndreas Gohr 'USER' => $_SERVER['REMOTE_USER'], 213abbf0890SAndreas Gohr 'NAME' => $INFO['userinfo']['name'], 214abbf0890SAndreas Gohr 'MAIL' => $INFO['userinfo']['mail'], 215abbf0890SAndreas Gohr ); 216abbf0890SAndreas Gohr $trep = array_merge($trep,(array) $textrep); 217abbf0890SAndreas Gohr $hrep = array( 218abbf0890SAndreas Gohr 'DATE' => '<i>'.hsc(dformat()).'</i>', 219abbf0890SAndreas Gohr 'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']), 220abbf0890SAndreas Gohr 'IPADDRESS' => '<code>'.hsc($ip).'</code>', 221f08086ecSAndreas Gohr 'HOSTNAME' => '<code>'.hsc($cip).'</code>', 222abbf0890SAndreas Gohr 'TITLE' => hsc($conf['title']), 223abbf0890SAndreas Gohr 'DOKUWIKIURL' => '<a href="'.DOKU_URL.'">'.DOKU_URL.'</a>', 224abbf0890SAndreas Gohr 'USER' => hsc($_SERVER['REMOTE_USER']), 225abbf0890SAndreas Gohr 'NAME' => hsc($INFO['userinfo']['name']), 226abbf0890SAndreas Gohr 'MAIL' => '<a href="mailto:"'.hsc($INFO['userinfo']['mail']).'">'. 227abbf0890SAndreas Gohr hsc($INFO['userinfo']['mail']).'</a>', 228abbf0890SAndreas Gohr ); 229abbf0890SAndreas Gohr $hrep = array_merge($hrep,(array) $htmlrep); 230abbf0890SAndreas Gohr 231abbf0890SAndreas Gohr // Apply replacements 232abbf0890SAndreas Gohr foreach ($trep as $key => $substitution) { 233abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); 234abbf0890SAndreas Gohr } 235abbf0890SAndreas Gohr foreach ($hrep as $key => $substitution) { 236abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@',$substitution, $html); 237abbf0890SAndreas Gohr } 238abbf0890SAndreas Gohr 239abbf0890SAndreas Gohr $this->setHTML($html); 240abbf0890SAndreas Gohr $this->setText($text); 241abbf0890SAndreas Gohr } 242abbf0890SAndreas Gohr 243abbf0890SAndreas Gohr /** 244bb01c27cSAndreas Gohr * Set the HTML part of the mail 245bb01c27cSAndreas Gohr * 246bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 247abbf0890SAndreas Gohr * 248abbf0890SAndreas Gohr * You probably want to use setBody() instead 249bb01c27cSAndreas Gohr */ 2501d045709SAndreas Gohr public function setHTML($html){ 251bb01c27cSAndreas Gohr $this->html = $html; 252bb01c27cSAndreas Gohr } 253bb01c27cSAndreas Gohr 254bb01c27cSAndreas Gohr /** 255bb01c27cSAndreas Gohr * Set the plain text part of the mail 256abbf0890SAndreas Gohr * 257abbf0890SAndreas Gohr * You probably want to use setBody() instead 258bb01c27cSAndreas Gohr */ 2591d045709SAndreas Gohr public function setText($text){ 260bb01c27cSAndreas Gohr $this->text = $text; 261bb01c27cSAndreas Gohr } 262bb01c27cSAndreas Gohr 263bb01c27cSAndreas Gohr /** 264a36fc348SAndreas Gohr * Add the To: recipients 265a36fc348SAndreas Gohr * 266a36fc348SAndreas Gohr * @see setAddress 267a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 268a36fc348SAndreas Gohr */ 269a36fc348SAndreas Gohr public function to($address){ 270a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 271a36fc348SAndreas Gohr } 272a36fc348SAndreas Gohr 273a36fc348SAndreas Gohr /** 274a36fc348SAndreas Gohr * Add the Cc: recipients 275a36fc348SAndreas Gohr * 276a36fc348SAndreas Gohr * @see setAddress 277a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 278a36fc348SAndreas Gohr */ 279a36fc348SAndreas Gohr public function cc($address){ 280a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 281a36fc348SAndreas Gohr } 282a36fc348SAndreas Gohr 283a36fc348SAndreas Gohr /** 284a36fc348SAndreas Gohr * Add the Bcc: recipients 285a36fc348SAndreas Gohr * 286a36fc348SAndreas Gohr * @see setAddress 287a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 288a36fc348SAndreas Gohr */ 289a36fc348SAndreas Gohr public function bcc($address){ 290a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 291a36fc348SAndreas Gohr } 292a36fc348SAndreas Gohr 293a36fc348SAndreas Gohr /** 294a36fc348SAndreas Gohr * Add the From: address 295a36fc348SAndreas Gohr * 296a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 297a36fc348SAndreas Gohr * to call this function 298a36fc348SAndreas Gohr * 299a36fc348SAndreas Gohr * @see setAddress 300a36fc348SAndreas Gohr * @param string $address from address 301a36fc348SAndreas Gohr */ 302a36fc348SAndreas Gohr public function from($address){ 303a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 304a36fc348SAndreas Gohr } 305a36fc348SAndreas Gohr 306a36fc348SAndreas Gohr /** 307a36fc348SAndreas Gohr * Add the mail's Subject: header 308a36fc348SAndreas Gohr * 309a36fc348SAndreas Gohr * @param string $subject the mail subject 310a36fc348SAndreas Gohr */ 311a36fc348SAndreas Gohr public function subject($subject){ 312a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 313a36fc348SAndreas Gohr } 314a36fc348SAndreas Gohr 315a36fc348SAndreas Gohr /** 3161d045709SAndreas Gohr * Sets an email address header with correct encoding 317bb01c27cSAndreas Gohr * 318bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 319bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 320bb01c27cSAndreas Gohr * 321bb01c27cSAndreas Gohr * Example: 322bb01c27cSAndreas Gohr * setAddress("föö <foo@bar.com>, me@somewhere.com","TBcc"); 323bb01c27cSAndreas Gohr * 324bb01c27cSAndreas Gohr * @param string $address Multiple adresses separated by commas 325a36fc348SAndreas Gohr * @param string returns the prepared header (can contain multiple lines) 326bb01c27cSAndreas Gohr */ 327a36fc348SAndreas Gohr public function cleanAddress($address){ 328bb01c27cSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 329bb01c27cSAndreas Gohr $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 330bb01c27cSAndreas Gohr 3311d045709SAndreas Gohr $address = preg_replace('/[\r\n\0]+/',' ',$address); // remove attack vectors 3321d045709SAndreas Gohr 333bb01c27cSAndreas Gohr $headers = ''; 334bb01c27cSAndreas Gohr $parts = explode(',',$address); 335bb01c27cSAndreas Gohr foreach ($parts as $part){ 336bb01c27cSAndreas Gohr $part = trim($part); 337bb01c27cSAndreas Gohr 338bb01c27cSAndreas Gohr // parse address 339bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 340bb01c27cSAndreas Gohr $text = trim($matches[1]); 341bb01c27cSAndreas Gohr $addr = $matches[2]; 342bb01c27cSAndreas Gohr }else{ 343bb01c27cSAndreas Gohr $addr = $part; 344bb01c27cSAndreas Gohr } 345bb01c27cSAndreas Gohr // skip empty ones 346bb01c27cSAndreas Gohr if(empty($addr)){ 347bb01c27cSAndreas Gohr continue; 348bb01c27cSAndreas Gohr } 349bb01c27cSAndreas Gohr 350bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 351bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)){ 352bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 353bb01c27cSAndreas Gohr continue; 354bb01c27cSAndreas Gohr } 355bb01c27cSAndreas Gohr 3561d045709SAndreas Gohr if(is_null($this->validator)){ 3571d045709SAndreas Gohr $this->validator = new EmailAddressValidator(); 3581d045709SAndreas Gohr $this->validator->allowLocalAddresses = true; 3591d045709SAndreas Gohr } 3601d045709SAndreas Gohr if(!$this->validator->check_email_address($addr)){ 361bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 362bb01c27cSAndreas Gohr continue; 363bb01c27cSAndreas Gohr } 364bb01c27cSAndreas Gohr 365bb01c27cSAndreas Gohr // text was given 366bb01c27cSAndreas Gohr if(!empty($text) && $names){ 367bb01c27cSAndreas Gohr // add address quotes 368bb01c27cSAndreas Gohr $addr = "<$addr>"; 369bb01c27cSAndreas Gohr 370bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 371bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 372bb01c27cSAndreas Gohr $text = utf8_strip($text); 373bb01c27cSAndreas Gohr } 374bb01c27cSAndreas Gohr 375bb01c27cSAndreas Gohr if(!utf8_isASCII($text)){ 376bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 377bb01c27cSAndreas Gohr } 378bb01c27cSAndreas Gohr }else{ 379bb01c27cSAndreas Gohr $text = ''; 380bb01c27cSAndreas Gohr } 381bb01c27cSAndreas Gohr 382bb01c27cSAndreas Gohr // add to header comma seperated 383bb01c27cSAndreas Gohr if($headers != ''){ 384bb01c27cSAndreas Gohr $headers .= ', '; 385bb01c27cSAndreas Gohr } 386bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 387bb01c27cSAndreas Gohr } 388bb01c27cSAndreas Gohr 389bb01c27cSAndreas Gohr if(empty($headers)) return false; 390bb01c27cSAndreas Gohr 391bb01c27cSAndreas Gohr return $headers; 392bb01c27cSAndreas Gohr } 393bb01c27cSAndreas Gohr 394bb01c27cSAndreas Gohr 395bb01c27cSAndreas Gohr /** 396bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 397bb01c27cSAndreas Gohr * 398bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 399bb01c27cSAndreas Gohr */ 400bb01c27cSAndreas Gohr protected function prepareAttachments(){ 401bb01c27cSAndreas Gohr $mime = ''; 402bb01c27cSAndreas Gohr $part = 1; 403bb01c27cSAndreas Gohr // embedded attachments 404bb01c27cSAndreas Gohr foreach($this->attach as $media){ 405bb01c27cSAndreas Gohr // create content id 406bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 407bb01c27cSAndreas Gohr 408bb01c27cSAndreas Gohr // replace wildcards 409bb01c27cSAndreas Gohr if($media['embed']){ 410bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%','cid:'.$cid,$this->html); 411bb01c27cSAndreas Gohr } 412bb01c27cSAndreas Gohr 413bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 414bb01c27cSAndreas Gohr $mime .= 'Content-Type: '.$media['mime'].';'.MAILHEADER_EOL; 415bb01c27cSAndreas Gohr $mime .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 416bb01c27cSAndreas Gohr $mime .= "Content-ID: <$cid>".MAILHEADER_EOL; 417bb01c27cSAndreas Gohr if($media['embed']){ 418bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: inline; filename="'.$media['name'].'"'.MAILHEADER_EOL; 419bb01c27cSAndreas Gohr }else{ 420bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: attachment; filename="'.$media['name'].'"'.MAILHEADER_EOL; 421bb01c27cSAndreas Gohr } 422bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 423bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']),74,MAILHEADER_EOL); 424bb01c27cSAndreas Gohr 425bb01c27cSAndreas Gohr $part++; 426bb01c27cSAndreas Gohr } 427bb01c27cSAndreas Gohr return $mime; 428bb01c27cSAndreas Gohr } 429bb01c27cSAndreas Gohr 4301d045709SAndreas Gohr /** 4311d045709SAndreas Gohr * Build the body and handles multi part mails 4321d045709SAndreas Gohr * 4331d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4341d045709SAndreas Gohr * 4351d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4361d045709SAndreas Gohr */ 4371d045709SAndreas Gohr protected function prepareBody(){ 4381d045709SAndreas Gohr global $conf; 4391d045709SAndreas Gohr 440*2398a2b5SAndreas Gohr // no HTML mails allowed? remove HTML body 441*2398a2b5SAndreas Gohr if(!$this->allowhtml){ 442*2398a2b5SAndreas Gohr $this->html = ''; 443*2398a2b5SAndreas Gohr } 444*2398a2b5SAndreas Gohr 445bb01c27cSAndreas Gohr // check for body 446bb01c27cSAndreas Gohr if(!$this->text && !$this->html){ 447bb01c27cSAndreas Gohr return false; 448bb01c27cSAndreas Gohr } 449bb01c27cSAndreas Gohr 450bb01c27cSAndreas Gohr // add general headers 451bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 452bb01c27cSAndreas Gohr 4531d045709SAndreas Gohr $body = ''; 4541d045709SAndreas Gohr 455bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)){ // we can send a simple single part message 456bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 457bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 4581d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 459bb01c27cSAndreas Gohr }else{ // multi part it is 4601d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 461bb01c27cSAndreas Gohr 462bb01c27cSAndreas Gohr // prepare the attachments 463bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 464bb01c27cSAndreas Gohr 465bb01c27cSAndreas Gohr // do we have alternative text content? 466bb01c27cSAndreas Gohr if($this->text && $this->html){ 467a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 468a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 469bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4701d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4711d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 472bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 473bb01c27cSAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 474bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 475a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 476a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'"'.MAILHEADER_EOL; 477bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 478bb01c27cSAndreas Gohr } 479bb01c27cSAndreas Gohr 4801d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4811d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4821d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 483bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 4841d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->html),74,MAILHEADER_EOL); 485bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 486bb01c27cSAndreas Gohr $body .= $attachments; 487bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 488bb01c27cSAndreas Gohr 489bb01c27cSAndreas Gohr // close open multipart/alternative boundary 490bb01c27cSAndreas Gohr if($this->text && $this->html){ 491bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 492bb01c27cSAndreas Gohr } 493bb01c27cSAndreas Gohr } 494bb01c27cSAndreas Gohr 495bb01c27cSAndreas Gohr return $body; 496bb01c27cSAndreas Gohr } 497bb01c27cSAndreas Gohr 498bb01c27cSAndreas Gohr /** 499a36fc348SAndreas Gohr * Cleanup and encode the headers array 500a36fc348SAndreas Gohr */ 501a36fc348SAndreas Gohr protected function cleanHeaders(){ 502a36fc348SAndreas Gohr global $conf; 503a36fc348SAndreas Gohr 504a36fc348SAndreas Gohr // clean up addresses 505a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 506a36fc348SAndreas Gohr $addrs = array('To','From','Cc','Bcc'); 507a36fc348SAndreas Gohr foreach($addrs as $addr){ 508a36fc348SAndreas Gohr if(isset($this->headers[$addr])){ 509a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 510a36fc348SAndreas Gohr } 511a36fc348SAndreas Gohr } 512a36fc348SAndreas Gohr 51345992a63SAndreas Gohr if(isset($this->headers['Subject'])){ 514a36fc348SAndreas Gohr // add prefix to subject 51554f30755SAndreas Gohr if(empty($conf['mailprefix'])){ 5168a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 51754f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 51854f30755SAndreas Gohr }else{ 5198a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 5208a215f09SAndreas Gohr } 5218a215f09SAndreas Gohr }else{ 522a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 52354f30755SAndreas Gohr } 524a36fc348SAndreas Gohr $len = strlen($prefix); 52545992a63SAndreas Gohr if(substr($this->headers['Subject'],0,$len) != $prefix){ 52645992a63SAndreas Gohr $this->headers['Subject'] = $prefix.' '.$this->headers['Subject']; 527a36fc348SAndreas Gohr } 528a36fc348SAndreas Gohr 529a36fc348SAndreas Gohr // encode subject 530a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 53145992a63SAndreas Gohr $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']); 53245992a63SAndreas Gohr $this->headers['Subject'] = utf8_strip($this->headers['Subject']); 533a36fc348SAndreas Gohr } 534a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])){ 53545992a63SAndreas Gohr $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 536a36fc348SAndreas Gohr } 537a36fc348SAndreas Gohr } 538a36fc348SAndreas Gohr 539a36fc348SAndreas Gohr // wrap headers 540a36fc348SAndreas Gohr foreach($this->headers as $key => $val){ 541a36fc348SAndreas Gohr $this->headers[$key] = wordwrap($val,78,MAILHEADER_EOL.' '); 542a36fc348SAndreas Gohr } 543a36fc348SAndreas Gohr } 544a36fc348SAndreas Gohr 545a36fc348SAndreas Gohr /** 546bb01c27cSAndreas Gohr * Create a string from the headers array 5471d045709SAndreas Gohr * 5481d045709SAndreas Gohr * @returns string the headers 549bb01c27cSAndreas Gohr */ 550bb01c27cSAndreas Gohr protected function prepareHeaders(){ 551bb01c27cSAndreas Gohr $headers = ''; 552bb01c27cSAndreas Gohr foreach($this->headers as $key => $val){ 553bb01c27cSAndreas Gohr $headers .= "$key: $val".MAILHEADER_EOL; 554bb01c27cSAndreas Gohr } 555bb01c27cSAndreas Gohr return $headers; 556bb01c27cSAndreas Gohr } 557bb01c27cSAndreas Gohr 558bb01c27cSAndreas Gohr /** 559bb01c27cSAndreas Gohr * return a full email with all headers 560bb01c27cSAndreas Gohr * 5611d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5621d045709SAndreas Gohr * used for MHT exports 5631d045709SAndreas Gohr * 5641d045709SAndreas Gohr * @return string the mail, false on errors 565bb01c27cSAndreas Gohr */ 566bb01c27cSAndreas Gohr public function dump(){ 567a36fc348SAndreas Gohr $this->cleanHeaders(); 568bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5691d045709SAndreas Gohr if($body === 'false') return false; 5701d045709SAndreas Gohr $headers = $this->prepareHeaders(); 571bb01c27cSAndreas Gohr 572bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 573bb01c27cSAndreas Gohr } 5741d045709SAndreas Gohr 5751d045709SAndreas Gohr /** 5761d045709SAndreas Gohr * Send the mail 5771d045709SAndreas Gohr * 5781d045709SAndreas Gohr * Call this after all data was set 5791d045709SAndreas Gohr * 58028d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 5811d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 5821d045709SAndreas Gohr */ 5831d045709SAndreas Gohr public function send(){ 58428d2ad80SAndreas Gohr $success = false; 585a36fc348SAndreas Gohr 58628d2ad80SAndreas Gohr // prepare hook data 58728d2ad80SAndreas Gohr $data = array( 58828d2ad80SAndreas Gohr // pass the whole mail class to plugin 58928d2ad80SAndreas Gohr 'mail' => $this, 59028d2ad80SAndreas Gohr // pass references for backward compatibility 59128d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 59228d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 59328d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 59428d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 59528d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 59628d2ad80SAndreas Gohr 'body' => &$this->text, 59728d2ad80SAndreas Gohr 'params' => &$this->sendparams, 59828d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 59928d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 60028d2ad80SAndreas Gohr 'success' => &$success, 60128d2ad80SAndreas Gohr ); 60228d2ad80SAndreas Gohr 60328d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 60428d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 60528d2ad80SAndreas Gohr if ($evt->advise_before(true)) { 60628d2ad80SAndreas Gohr // clean up before using the headers 607a36fc348SAndreas Gohr $this->cleanHeaders(); 608a36fc348SAndreas Gohr 6091d045709SAndreas Gohr // any recipients? 6101d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 6111d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 6121d045709SAndreas Gohr trim($this->headers['Bcc']) === '') return false; 6131d045709SAndreas Gohr 6141d045709SAndreas Gohr // The To: header is special 6151d045709SAndreas Gohr if(isset($this->headers['To'])){ 6161d045709SAndreas Gohr $to = $this->headers['To']; 6171d045709SAndreas Gohr unset($this->headers['To']); 6181d045709SAndreas Gohr }else{ 6191d045709SAndreas Gohr $to = ''; 6201d045709SAndreas Gohr } 6211d045709SAndreas Gohr 6221d045709SAndreas Gohr // so is the subject 6231d045709SAndreas Gohr if(isset($this->headers['Subject'])){ 6241d045709SAndreas Gohr $subject = $this->headers['Subject']; 6251d045709SAndreas Gohr unset($this->headers['Subject']); 6261d045709SAndreas Gohr }else{ 6271d045709SAndreas Gohr $subject = ''; 6281d045709SAndreas Gohr } 6291d045709SAndreas Gohr 6301d045709SAndreas Gohr // make the body 6311d045709SAndreas Gohr $body = $this->prepareBody(); 6321d045709SAndreas Gohr if($body === 'false') return false; 6331d045709SAndreas Gohr 6341d045709SAndreas Gohr // cook the headers 6351d045709SAndreas Gohr $headers = $this->prepareHeaders(); 63628d2ad80SAndreas Gohr // add any headers set by legacy plugins 63728d2ad80SAndreas Gohr if(trim($data['headers'])){ 63828d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 63928d2ad80SAndreas Gohr } 6401d045709SAndreas Gohr 6411d045709SAndreas Gohr // send the thing 6421d045709SAndreas Gohr if(is_null($this->sendparam)){ 64328d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers); 6441d045709SAndreas Gohr }else{ 64528d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers,$this->sendparam); 6461d045709SAndreas Gohr } 6471d045709SAndreas Gohr } 64828d2ad80SAndreas Gohr // any AFTER actions? 64928d2ad80SAndreas Gohr $evt->advise_after(); 65028d2ad80SAndreas Gohr return $success; 65128d2ad80SAndreas Gohr } 652bb01c27cSAndreas Gohr} 653