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; 291d045709SAndreas Gohr 301d045709SAndreas Gohr /** 311d045709SAndreas Gohr * Constructor 321d045709SAndreas Gohr * 331d045709SAndreas Gohr * Initializes the boundary strings and part counters 341d045709SAndreas Gohr */ 351d045709SAndreas Gohr public function __construct(){ 361d045709SAndreas Gohr if(isset($_SERVER['SERVER_NAME'])){ 371d045709SAndreas Gohr $server = $_SERVER['SERVER_NAME']; 381d045709SAndreas Gohr }else{ 391d045709SAndreas Gohr $server = 'localhost'; 401d045709SAndreas Gohr } 411d045709SAndreas Gohr 421d045709SAndreas Gohr $this->partid = md5(uniqid(rand(),true)).'@'.$server; 43bb01c27cSAndreas Gohr $this->boundary = '----------'.md5(uniqid(rand(),true)); 44bb01c27cSAndreas Gohr } 45bb01c27cSAndreas Gohr 46bb01c27cSAndreas Gohr /** 47bb01c27cSAndreas Gohr * Attach a file 48bb01c27cSAndreas Gohr * 49bb01c27cSAndreas Gohr * @param $path Path to the file to attach 50bb01c27cSAndreas Gohr * @param $mime Mimetype of the attached file 51bb01c27cSAndreas Gohr * @param $name The filename to use 52bb01c27cSAndreas Gohr * @param $embed Unique key to reference this file from the HTML part 53bb01c27cSAndreas Gohr */ 54bb01c27cSAndreas Gohr public function attachFile($path,$mime,$name='',$embed=''){ 55bb01c27cSAndreas Gohr if(!$name){ 56bb01c27cSAndreas Gohr $name = basename($path); 57bb01c27cSAndreas Gohr } 58bb01c27cSAndreas Gohr 59bb01c27cSAndreas Gohr $this->attach[] = array( 60bb01c27cSAndreas Gohr 'data' => file_get_contents($path), 61bb01c27cSAndreas Gohr 'mime' => $mime, 62bb01c27cSAndreas Gohr 'name' => $name, 63bb01c27cSAndreas Gohr 'embed' => $embed 64bb01c27cSAndreas Gohr ); 65bb01c27cSAndreas Gohr } 66bb01c27cSAndreas Gohr 67bb01c27cSAndreas Gohr /** 68bb01c27cSAndreas Gohr * Attach a file 69bb01c27cSAndreas Gohr * 70bb01c27cSAndreas Gohr * @param $path The file contents to attach 71bb01c27cSAndreas Gohr * @param $mime Mimetype of the attached file 72bb01c27cSAndreas Gohr * @param $name The filename to use 73bb01c27cSAndreas Gohr * @param $embed Unique key to reference this file from the HTML part 74bb01c27cSAndreas Gohr */ 75bb01c27cSAndreas Gohr public function attachContent($data,$mime,$name='',$embed=''){ 76bb01c27cSAndreas Gohr if(!$name){ 77bb01c27cSAndreas Gohr list($junk,$ext) = split('/',$mime); 78bb01c27cSAndreas Gohr $name = count($this->attach).".$ext"; 79bb01c27cSAndreas Gohr } 80bb01c27cSAndreas Gohr 81bb01c27cSAndreas Gohr $this->attach[] = array( 82bb01c27cSAndreas Gohr 'data' => $data, 83bb01c27cSAndreas Gohr 'mime' => $mime, 84bb01c27cSAndreas Gohr 'name' => $name, 85bb01c27cSAndreas Gohr 'embed' => $embed 86bb01c27cSAndreas Gohr ); 87bb01c27cSAndreas Gohr } 88bb01c27cSAndreas Gohr 89bb01c27cSAndreas Gohr /** 901d045709SAndreas Gohr * Add an arbitrary header to the mail 911d045709SAndreas Gohr * 92a36fc348SAndreas Gohr * If an empy value is passed, the header is removed 93a36fc348SAndreas Gohr * 941d045709SAndreas Gohr * @param string $header the header name (no trailing colon!) 951d045709SAndreas Gohr * @param string $value the value of the header 961d045709SAndreas Gohr * @param bool $clean remove all non-ASCII chars and line feeds? 971d045709SAndreas Gohr */ 981d045709SAndreas Gohr public function setHeader($header,$value,$clean=true){ 991d045709SAndreas Gohr $header = ucwords(strtolower($header)); // streamline casing 1001d045709SAndreas Gohr if($clean){ 1011d045709SAndreas Gohr $header = preg_replace('/[^\w \-\.\+\@]+/','',$header); 1021d045709SAndreas Gohr $value = preg_replace('/[^\w \-\.\+\@]+/','',$value); 1031d045709SAndreas Gohr } 104a36fc348SAndreas Gohr 105a36fc348SAndreas Gohr // empty value deletes 106a36fc348SAndreas Gohr $value = trim($value); 107a36fc348SAndreas Gohr if($value === ''){ 108a36fc348SAndreas Gohr if(isset($this->headers[$header])) unset($this->headers[$header]); 109a36fc348SAndreas Gohr }else{ 1101d045709SAndreas Gohr $this->headers[$header] = $value; 1111d045709SAndreas Gohr } 112a36fc348SAndreas Gohr } 1131d045709SAndreas Gohr 1141d045709SAndreas Gohr /** 1151d045709SAndreas Gohr * Set additional parameters to be passed to sendmail 1161d045709SAndreas Gohr * 1171d045709SAndreas Gohr * Whatever is set here is directly passed to PHP's mail() command as last 1181d045709SAndreas Gohr * parameter. Depending on the PHP setup this might break mailing alltogether 1191d045709SAndreas Gohr */ 1201d045709SAndreas Gohr public function setParameters($param){ 1211d045709SAndreas Gohr $this->sendparam = $param; 1221d045709SAndreas Gohr } 1231d045709SAndreas Gohr 1241d045709SAndreas Gohr /** 125abbf0890SAndreas Gohr * Set the text and HTML body and apply replacements 126abbf0890SAndreas Gohr * 127abbf0890SAndreas Gohr * This function applies a whole bunch of default replacements in addition 128abbf0890SAndreas Gohr * to the ones specidifed as parameters 129abbf0890SAndreas Gohr * 130abbf0890SAndreas Gohr * If you pass the HTML part or HTML replacements yourself you have to make 131abbf0890SAndreas Gohr * sure you encode all HTML special chars correctly 132abbf0890SAndreas Gohr * 133abbf0890SAndreas Gohr * @param string $text plain text body 134abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 135abbf0890SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep 136abbf0890SAndreas Gohr * @param array $html the HTML body, leave null to create it from $text 137*f08086ecSAndreas Gohr * @param bool $wrap wrap the HTML in the default header/Footer 138abbf0890SAndreas Gohr */ 139*f08086ecSAndreas Gohr public function setBody($text, $textrep=null, $htmlrep=null, $html=null, $wrap=true){ 140abbf0890SAndreas Gohr global $INFO; 141abbf0890SAndreas Gohr global $conf; 14276efd6d0SAndreas Gohr $htmlrep = (array) $htmlrep; 14376efd6d0SAndreas Gohr $textrep = (array) $textrep; 144abbf0890SAndreas Gohr 145abbf0890SAndreas Gohr // create HTML from text if not given 146abbf0890SAndreas Gohr if(is_null($html)){ 147abbf0890SAndreas Gohr $html = hsc($text); 148abbf0890SAndreas Gohr $html = nl2br($text); 149abbf0890SAndreas Gohr } 150*f08086ecSAndreas Gohr if($wrap){ 151*f08086ecSAndreas Gohr $wrap = rawLocale('mailwrap','html'); 152*f08086ecSAndreas Gohr $html = preg_replace('/\n-- \n.*$/m','',$html); //strip signature 153*f08086ecSAndreas Gohr $html = str_replace('@HTMLBODY@',$html,$wrap); 154*f08086ecSAndreas Gohr } 155*f08086ecSAndreas Gohr 15676efd6d0SAndreas Gohr // copy over all replacements missing for HTML (autolink URLs) 15776efd6d0SAndreas Gohr foreach($textrep as $key => $value){ 15876efd6d0SAndreas Gohr if(isset($htmlrep[$key])) continue; 15976efd6d0SAndreas Gohr if(preg_match('/^https?:\/\//i',$value)){ 16076efd6d0SAndreas Gohr $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>'; 16176efd6d0SAndreas Gohr }else{ 16276efd6d0SAndreas Gohr $htmlrep[$key] = hsc($value); 16376efd6d0SAndreas Gohr } 164abbf0890SAndreas Gohr } 165abbf0890SAndreas Gohr 166abbf0890SAndreas Gohr // prepare default replacements 167abbf0890SAndreas Gohr $ip = clientIP(); 168*f08086ecSAndreas Gohr $cip = gethostsbyaddrs($ip); 169abbf0890SAndreas Gohr $trep = array( 170abbf0890SAndreas Gohr 'DATE' => dformat(), 171abbf0890SAndreas Gohr 'BROWSER' => $_SERVER['HTTP_USER_AGENT'], 172abbf0890SAndreas Gohr 'IPADDRESS' => $ip, 173*f08086ecSAndreas Gohr 'HOSTNAME' => $cip, 174abbf0890SAndreas Gohr 'TITLE' => $conf['title'], 175abbf0890SAndreas Gohr 'DOKUWIKIURL' => DOKU_URL, 176abbf0890SAndreas Gohr 'USER' => $_SERVER['REMOTE_USER'], 177abbf0890SAndreas Gohr 'NAME' => $INFO['userinfo']['name'], 178abbf0890SAndreas Gohr 'MAIL' => $INFO['userinfo']['mail'], 179abbf0890SAndreas Gohr ); 180abbf0890SAndreas Gohr $trep = array_merge($trep,(array) $textrep); 181abbf0890SAndreas Gohr $hrep = array( 182abbf0890SAndreas Gohr 'DATE' => '<i>'.hsc(dformat()).'</i>', 183abbf0890SAndreas Gohr 'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']), 184abbf0890SAndreas Gohr 'IPADDRESS' => '<code>'.hsc($ip).'</code>', 185*f08086ecSAndreas Gohr 'HOSTNAME' => '<code>'.hsc($cip).'</code>', 186abbf0890SAndreas Gohr 'TITLE' => hsc($conf['title']), 187abbf0890SAndreas Gohr 'DOKUWIKIURL' => '<a href="'.DOKU_URL.'">'.DOKU_URL.'</a>', 188abbf0890SAndreas Gohr 'USER' => hsc($_SERVER['REMOTE_USER']), 189abbf0890SAndreas Gohr 'NAME' => hsc($INFO['userinfo']['name']), 190abbf0890SAndreas Gohr 'MAIL' => '<a href="mailto:"'.hsc($INFO['userinfo']['mail']).'">'. 191abbf0890SAndreas Gohr hsc($INFO['userinfo']['mail']).'</a>', 192abbf0890SAndreas Gohr ); 193abbf0890SAndreas Gohr $hrep = array_merge($hrep,(array) $htmlrep); 194abbf0890SAndreas Gohr 195abbf0890SAndreas Gohr // Apply replacements 196abbf0890SAndreas Gohr foreach ($trep as $key => $substitution) { 197abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); 198abbf0890SAndreas Gohr } 199abbf0890SAndreas Gohr foreach ($hrep as $key => $substitution) { 200abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@',$substitution, $html); 201abbf0890SAndreas Gohr } 202abbf0890SAndreas Gohr 203abbf0890SAndreas Gohr $this->setHTML($html); 204abbf0890SAndreas Gohr $this->setText($text); 205abbf0890SAndreas Gohr } 206abbf0890SAndreas Gohr 207abbf0890SAndreas Gohr /** 208bb01c27cSAndreas Gohr * Set the HTML part of the mail 209bb01c27cSAndreas Gohr * 210bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 211abbf0890SAndreas Gohr * 212abbf0890SAndreas Gohr * You probably want to use setBody() instead 213bb01c27cSAndreas Gohr */ 2141d045709SAndreas Gohr public function setHTML($html){ 215bb01c27cSAndreas Gohr $this->html = $html; 216bb01c27cSAndreas Gohr } 217bb01c27cSAndreas Gohr 218bb01c27cSAndreas Gohr /** 219bb01c27cSAndreas Gohr * Set the plain text part of the mail 220abbf0890SAndreas Gohr * 221abbf0890SAndreas Gohr * You probably want to use setBody() instead 222bb01c27cSAndreas Gohr */ 2231d045709SAndreas Gohr public function setText($text){ 224bb01c27cSAndreas Gohr $this->text = $text; 225bb01c27cSAndreas Gohr } 226bb01c27cSAndreas Gohr 227bb01c27cSAndreas Gohr /** 228a36fc348SAndreas Gohr * Add the To: recipients 229a36fc348SAndreas Gohr * 230a36fc348SAndreas Gohr * @see setAddress 231a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 232a36fc348SAndreas Gohr */ 233a36fc348SAndreas Gohr public function to($address){ 234a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 235a36fc348SAndreas Gohr } 236a36fc348SAndreas Gohr 237a36fc348SAndreas Gohr /** 238a36fc348SAndreas Gohr * Add the Cc: recipients 239a36fc348SAndreas Gohr * 240a36fc348SAndreas Gohr * @see setAddress 241a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 242a36fc348SAndreas Gohr */ 243a36fc348SAndreas Gohr public function cc($address){ 244a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 245a36fc348SAndreas Gohr } 246a36fc348SAndreas Gohr 247a36fc348SAndreas Gohr /** 248a36fc348SAndreas Gohr * Add the Bcc: recipients 249a36fc348SAndreas Gohr * 250a36fc348SAndreas Gohr * @see setAddress 251a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 252a36fc348SAndreas Gohr */ 253a36fc348SAndreas Gohr public function bcc($address){ 254a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 255a36fc348SAndreas Gohr } 256a36fc348SAndreas Gohr 257a36fc348SAndreas Gohr /** 258a36fc348SAndreas Gohr * Add the From: address 259a36fc348SAndreas Gohr * 260a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 261a36fc348SAndreas Gohr * to call this function 262a36fc348SAndreas Gohr * 263a36fc348SAndreas Gohr * @see setAddress 264a36fc348SAndreas Gohr * @param string $address from address 265a36fc348SAndreas Gohr */ 266a36fc348SAndreas Gohr public function from($address){ 267a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 268a36fc348SAndreas Gohr } 269a36fc348SAndreas Gohr 270a36fc348SAndreas Gohr /** 271a36fc348SAndreas Gohr * Add the mail's Subject: header 272a36fc348SAndreas Gohr * 273a36fc348SAndreas Gohr * @param string $subject the mail subject 274a36fc348SAndreas Gohr */ 275a36fc348SAndreas Gohr public function subject($subject){ 276a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 277a36fc348SAndreas Gohr } 278a36fc348SAndreas Gohr 279a36fc348SAndreas Gohr /** 2801d045709SAndreas Gohr * Sets an email address header with correct encoding 281bb01c27cSAndreas Gohr * 282bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 283bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 284bb01c27cSAndreas Gohr * 285bb01c27cSAndreas Gohr * Example: 286bb01c27cSAndreas Gohr * setAddress("föö <foo@bar.com>, me@somewhere.com","TBcc"); 287bb01c27cSAndreas Gohr * 288bb01c27cSAndreas Gohr * @param string $address Multiple adresses separated by commas 289a36fc348SAndreas Gohr * @param string returns the prepared header (can contain multiple lines) 290bb01c27cSAndreas Gohr */ 291a36fc348SAndreas Gohr public function cleanAddress($address){ 292bb01c27cSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 293bb01c27cSAndreas Gohr $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 294bb01c27cSAndreas Gohr 2951d045709SAndreas Gohr $address = preg_replace('/[\r\n\0]+/',' ',$address); // remove attack vectors 2961d045709SAndreas Gohr 297bb01c27cSAndreas Gohr $headers = ''; 298bb01c27cSAndreas Gohr $parts = explode(',',$address); 299bb01c27cSAndreas Gohr foreach ($parts as $part){ 300bb01c27cSAndreas Gohr $part = trim($part); 301bb01c27cSAndreas Gohr 302bb01c27cSAndreas Gohr // parse address 303bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 304bb01c27cSAndreas Gohr $text = trim($matches[1]); 305bb01c27cSAndreas Gohr $addr = $matches[2]; 306bb01c27cSAndreas Gohr }else{ 307bb01c27cSAndreas Gohr $addr = $part; 308bb01c27cSAndreas Gohr } 309bb01c27cSAndreas Gohr // skip empty ones 310bb01c27cSAndreas Gohr if(empty($addr)){ 311bb01c27cSAndreas Gohr continue; 312bb01c27cSAndreas Gohr } 313bb01c27cSAndreas Gohr 314bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 315bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)){ 316bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 317bb01c27cSAndreas Gohr continue; 318bb01c27cSAndreas Gohr } 319bb01c27cSAndreas Gohr 3201d045709SAndreas Gohr if(is_null($this->validator)){ 3211d045709SAndreas Gohr $this->validator = new EmailAddressValidator(); 3221d045709SAndreas Gohr $this->validator->allowLocalAddresses = true; 3231d045709SAndreas Gohr } 3241d045709SAndreas Gohr if(!$this->validator->check_email_address($addr)){ 325bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 326bb01c27cSAndreas Gohr continue; 327bb01c27cSAndreas Gohr } 328bb01c27cSAndreas Gohr 329bb01c27cSAndreas Gohr // text was given 330bb01c27cSAndreas Gohr if(!empty($text) && $names){ 331bb01c27cSAndreas Gohr // add address quotes 332bb01c27cSAndreas Gohr $addr = "<$addr>"; 333bb01c27cSAndreas Gohr 334bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 335bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 336bb01c27cSAndreas Gohr $text = utf8_strip($text); 337bb01c27cSAndreas Gohr } 338bb01c27cSAndreas Gohr 339bb01c27cSAndreas Gohr if(!utf8_isASCII($text)){ 3401d045709SAndreas Gohr //FIXME check if this is needed for base64 too 341bb01c27cSAndreas Gohr // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" 342bb01c27cSAndreas Gohr /* 343bb01c27cSAndreas Gohr if (preg_match('/^"(.+)"$/', $text, $matches)) { 344bb01c27cSAndreas Gohr $text = '"=?UTF-8?Q?'.mail_quotedprintable_encode($matches[1], 0).'?="'; 345bb01c27cSAndreas Gohr } else { 346bb01c27cSAndreas Gohr $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text, 0).'?='; 347bb01c27cSAndreas Gohr } 348bb01c27cSAndreas Gohr */ 349bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 350bb01c27cSAndreas Gohr } 351bb01c27cSAndreas Gohr }else{ 352bb01c27cSAndreas Gohr $text = ''; 353bb01c27cSAndreas Gohr } 354bb01c27cSAndreas Gohr 355bb01c27cSAndreas Gohr // add to header comma seperated 356bb01c27cSAndreas Gohr if($headers != ''){ 357bb01c27cSAndreas Gohr $headers .= ', '; 358bb01c27cSAndreas Gohr } 359bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 360bb01c27cSAndreas Gohr } 361bb01c27cSAndreas Gohr 362bb01c27cSAndreas Gohr if(empty($headers)) return false; 363bb01c27cSAndreas Gohr 364bb01c27cSAndreas Gohr return $headers; 365bb01c27cSAndreas Gohr } 366bb01c27cSAndreas Gohr 367bb01c27cSAndreas Gohr 368bb01c27cSAndreas Gohr /** 369bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 370bb01c27cSAndreas Gohr * 371bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 372bb01c27cSAndreas Gohr */ 373bb01c27cSAndreas Gohr protected function prepareAttachments(){ 374bb01c27cSAndreas Gohr $mime = ''; 375bb01c27cSAndreas Gohr $part = 1; 376bb01c27cSAndreas Gohr // embedded attachments 377bb01c27cSAndreas Gohr foreach($this->attach as $media){ 378bb01c27cSAndreas Gohr // create content id 379bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 380bb01c27cSAndreas Gohr 381bb01c27cSAndreas Gohr // replace wildcards 382bb01c27cSAndreas Gohr if($media['embed']){ 383bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%','cid:'.$cid,$this->html); 384bb01c27cSAndreas Gohr } 385bb01c27cSAndreas Gohr 386bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 387bb01c27cSAndreas Gohr $mime .= 'Content-Type: '.$media['mime'].';'.MAILHEADER_EOL; 388bb01c27cSAndreas Gohr $mime .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 389bb01c27cSAndreas Gohr $mime .= "Content-ID: <$cid>".MAILHEADER_EOL; 390bb01c27cSAndreas Gohr if($media['embed']){ 391bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: inline; filename="'.$media['name'].'"'.MAILHEADER_EOL; 392bb01c27cSAndreas Gohr }else{ 393bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: attachment; filename="'.$media['name'].'"'.MAILHEADER_EOL; 394bb01c27cSAndreas Gohr } 395bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 396bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']),74,MAILHEADER_EOL); 397bb01c27cSAndreas Gohr 398bb01c27cSAndreas Gohr $part++; 399bb01c27cSAndreas Gohr } 400bb01c27cSAndreas Gohr return $mime; 401bb01c27cSAndreas Gohr } 402bb01c27cSAndreas Gohr 4031d045709SAndreas Gohr /** 4041d045709SAndreas Gohr * Build the body and handles multi part mails 4051d045709SAndreas Gohr * 4061d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4071d045709SAndreas Gohr * 4081d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4091d045709SAndreas Gohr */ 4101d045709SAndreas Gohr protected function prepareBody(){ 4111d045709SAndreas Gohr global $conf; 4121d045709SAndreas Gohr 413bb01c27cSAndreas Gohr // check for body 414bb01c27cSAndreas Gohr if(!$this->text && !$this->html){ 415bb01c27cSAndreas Gohr return false; 416bb01c27cSAndreas Gohr } 417bb01c27cSAndreas Gohr 418bb01c27cSAndreas Gohr // add general headers 419bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 420bb01c27cSAndreas Gohr 4211d045709SAndreas Gohr $body = ''; 4221d045709SAndreas Gohr 423bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)){ // we can send a simple single part message 424bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 425bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 4261d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 427bb01c27cSAndreas Gohr }else{ // multi part it is 4281d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 429bb01c27cSAndreas Gohr 430bb01c27cSAndreas Gohr // prepare the attachments 431bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 432bb01c27cSAndreas Gohr 433bb01c27cSAndreas Gohr // do we have alternative text content? 434bb01c27cSAndreas Gohr if($this->text && $this->html){ 435a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 436a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 437bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4381d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4391d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 440bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 441bb01c27cSAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 442bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 443a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 444a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'"'.MAILHEADER_EOL; 445bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 446bb01c27cSAndreas Gohr } 447bb01c27cSAndreas Gohr 4481d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4491d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4501d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 451bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 4521d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->html),74,MAILHEADER_EOL); 453bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 454bb01c27cSAndreas Gohr $body .= $attachments; 455bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 456bb01c27cSAndreas Gohr 457bb01c27cSAndreas Gohr // close open multipart/alternative boundary 458bb01c27cSAndreas Gohr if($this->text && $this->html){ 459bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 460bb01c27cSAndreas Gohr } 461bb01c27cSAndreas Gohr } 462bb01c27cSAndreas Gohr 463bb01c27cSAndreas Gohr return $body; 464bb01c27cSAndreas Gohr } 465bb01c27cSAndreas Gohr 466bb01c27cSAndreas Gohr /** 467a36fc348SAndreas Gohr * Cleanup and encode the headers array 468a36fc348SAndreas Gohr */ 469a36fc348SAndreas Gohr protected function cleanHeaders(){ 470a36fc348SAndreas Gohr global $conf; 471a36fc348SAndreas Gohr 472a36fc348SAndreas Gohr // clean up addresses 473a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 474a36fc348SAndreas Gohr $addrs = array('To','From','Cc','Bcc'); 475a36fc348SAndreas Gohr foreach($addrs as $addr){ 476a36fc348SAndreas Gohr if(isset($this->headers[$addr])){ 477a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 478a36fc348SAndreas Gohr } 479a36fc348SAndreas Gohr } 480a36fc348SAndreas Gohr 481a36fc348SAndreas Gohr if(isset($subject)){ 482a36fc348SAndreas Gohr // add prefix to subject 48354f30755SAndreas Gohr if(empty($conf['mailprefix'])){ 4848a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 48554f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 48654f30755SAndreas Gohr }else{ 4878a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 4888a215f09SAndreas Gohr } 4898a215f09SAndreas Gohr }else{ 490a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 49154f30755SAndreas Gohr } 492a36fc348SAndreas Gohr $len = strlen($prefix); 493a36fc348SAndreas Gohr if(substr($this->headers['subject'],0,$len) != $prefix){ 494a36fc348SAndreas Gohr $this->headers['subject'] = $prefix.' '.$this->headers['subject']; 495a36fc348SAndreas Gohr } 496a36fc348SAndreas Gohr 497a36fc348SAndreas Gohr // encode subject 498a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 499a36fc348SAndreas Gohr $this->headers['subject'] = utf8_deaccent($this->headers['subject']); 500a36fc348SAndreas Gohr $this->headers['subject'] = utf8_strip($this->headers['subject']); 501a36fc348SAndreas Gohr } 502a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])){ 503a36fc348SAndreas Gohr $subject = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 504a36fc348SAndreas Gohr } 505a36fc348SAndreas Gohr } 506a36fc348SAndreas Gohr 507a36fc348SAndreas Gohr // wrap headers 508a36fc348SAndreas Gohr foreach($this->headers as $key => $val){ 509a36fc348SAndreas Gohr $this->headers[$key] = wordwrap($val,78,MAILHEADER_EOL.' '); 510a36fc348SAndreas Gohr } 511a36fc348SAndreas Gohr } 512a36fc348SAndreas Gohr 513a36fc348SAndreas Gohr /** 514bb01c27cSAndreas Gohr * Create a string from the headers array 5151d045709SAndreas Gohr * 5161d045709SAndreas Gohr * @returns string the headers 517bb01c27cSAndreas Gohr */ 518bb01c27cSAndreas Gohr protected function prepareHeaders(){ 519bb01c27cSAndreas Gohr $headers = ''; 520bb01c27cSAndreas Gohr foreach($this->headers as $key => $val){ 521bb01c27cSAndreas Gohr $headers .= "$key: $val".MAILHEADER_EOL; 522bb01c27cSAndreas Gohr } 523bb01c27cSAndreas Gohr return $headers; 524bb01c27cSAndreas Gohr } 525bb01c27cSAndreas Gohr 526bb01c27cSAndreas Gohr /** 527bb01c27cSAndreas Gohr * return a full email with all headers 528bb01c27cSAndreas Gohr * 5291d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5301d045709SAndreas Gohr * used for MHT exports 5311d045709SAndreas Gohr * 5321d045709SAndreas Gohr * @return string the mail, false on errors 533bb01c27cSAndreas Gohr */ 534bb01c27cSAndreas Gohr public function dump(){ 535a36fc348SAndreas Gohr $this->cleanHeaders(); 536bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5371d045709SAndreas Gohr if($body === 'false') return false; 5381d045709SAndreas Gohr $headers = $this->prepareHeaders(); 539bb01c27cSAndreas Gohr 540bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 541bb01c27cSAndreas Gohr } 5421d045709SAndreas Gohr 5431d045709SAndreas Gohr /** 5441d045709SAndreas Gohr * Send the mail 5451d045709SAndreas Gohr * 5461d045709SAndreas Gohr * Call this after all data was set 5471d045709SAndreas Gohr * 54828d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 5491d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 5501d045709SAndreas Gohr */ 5511d045709SAndreas Gohr public function send(){ 55228d2ad80SAndreas Gohr $success = false; 553a36fc348SAndreas Gohr 55428d2ad80SAndreas Gohr // prepare hook data 55528d2ad80SAndreas Gohr $data = array( 55628d2ad80SAndreas Gohr // pass the whole mail class to plugin 55728d2ad80SAndreas Gohr 'mail' => $this, 55828d2ad80SAndreas Gohr // pass references for backward compatibility 55928d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 56028d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 56128d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 56228d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 56328d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 56428d2ad80SAndreas Gohr 'body' => &$this->text, 56528d2ad80SAndreas Gohr 'params' => &$this->sendparams, 56628d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 56728d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 56828d2ad80SAndreas Gohr 'success' => &$success, 56928d2ad80SAndreas Gohr ); 57028d2ad80SAndreas Gohr 57128d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 57228d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 57328d2ad80SAndreas Gohr if ($evt->advise_before(true)) { 57428d2ad80SAndreas Gohr // clean up before using the headers 575a36fc348SAndreas Gohr $this->cleanHeaders(); 576a36fc348SAndreas Gohr 5771d045709SAndreas Gohr // any recipients? 5781d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 5791d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 5801d045709SAndreas Gohr trim($this->headers['Bcc']) === '') return false; 5811d045709SAndreas Gohr 5821d045709SAndreas Gohr // The To: header is special 5831d045709SAndreas Gohr if(isset($this->headers['To'])){ 5841d045709SAndreas Gohr $to = $this->headers['To']; 5851d045709SAndreas Gohr unset($this->headers['To']); 5861d045709SAndreas Gohr }else{ 5871d045709SAndreas Gohr $to = ''; 5881d045709SAndreas Gohr } 5891d045709SAndreas Gohr 5901d045709SAndreas Gohr // so is the subject 5911d045709SAndreas Gohr if(isset($this->headers['Subject'])){ 5921d045709SAndreas Gohr $subject = $this->headers['Subject']; 5931d045709SAndreas Gohr unset($this->headers['Subject']); 5941d045709SAndreas Gohr }else{ 5951d045709SAndreas Gohr $subject = ''; 5961d045709SAndreas Gohr } 5971d045709SAndreas Gohr 5981d045709SAndreas Gohr // make the body 5991d045709SAndreas Gohr $body = $this->prepareBody(); 6001d045709SAndreas Gohr if($body === 'false') return false; 6011d045709SAndreas Gohr 6021d045709SAndreas Gohr // cook the headers 6031d045709SAndreas Gohr $headers = $this->prepareHeaders(); 60428d2ad80SAndreas Gohr // add any headers set by legacy plugins 60528d2ad80SAndreas Gohr if(trim($data['headers'])){ 60628d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 60728d2ad80SAndreas Gohr } 6081d045709SAndreas Gohr 6091d045709SAndreas Gohr // send the thing 6101d045709SAndreas Gohr if(is_null($this->sendparam)){ 61128d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers); 6121d045709SAndreas Gohr }else{ 61328d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers,$this->sendparam); 6141d045709SAndreas Gohr } 6151d045709SAndreas Gohr } 61628d2ad80SAndreas Gohr // any AFTER actions? 61728d2ad80SAndreas Gohr $evt->advise_after(); 61828d2ad80SAndreas Gohr return $success; 61928d2ad80SAndreas Gohr } 620bb01c27cSAndreas Gohr} 621