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 /** 125*abbf0890SAndreas Gohr * Set the text and HTML body and apply replacements 126*abbf0890SAndreas Gohr * 127*abbf0890SAndreas Gohr * This function applies a whole bunch of default replacements in addition 128*abbf0890SAndreas Gohr * to the ones specidifed as parameters 129*abbf0890SAndreas Gohr * 130*abbf0890SAndreas Gohr * If you pass the HTML part or HTML replacements yourself you have to make 131*abbf0890SAndreas Gohr * sure you encode all HTML special chars correctly 132*abbf0890SAndreas Gohr * 133*abbf0890SAndreas Gohr * @fixme the HTML head and body still needs to be set 134*abbf0890SAndreas Gohr * @param string $text plain text body 135*abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 136*abbf0890SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep 137*abbf0890SAndreas Gohr * @param array $html the HTML body, leave null to create it from $text 138*abbf0890SAndreas Gohr */ 139*abbf0890SAndreas Gohr public function setBody($text, $textrep=null, $htmlrep=null, $html=null){ 140*abbf0890SAndreas Gohr global $INFO; 141*abbf0890SAndreas Gohr global $conf; 142*abbf0890SAndreas Gohr 143*abbf0890SAndreas Gohr // create HTML from text if not given 144*abbf0890SAndreas Gohr if(is_null($html)){ 145*abbf0890SAndreas Gohr $html = hsc($text); 146*abbf0890SAndreas Gohr $html = nl2br($text); 147*abbf0890SAndreas Gohr } 148*abbf0890SAndreas Gohr if(!is_null($textrep) && is_null($htmlrep)){ 149*abbf0890SAndreas Gohr $htmlrep = array_map('hsc',$textrep); 150*abbf0890SAndreas Gohr } 151*abbf0890SAndreas Gohr 152*abbf0890SAndreas Gohr // prepare default replacements 153*abbf0890SAndreas Gohr $ip = clientIP(); 154*abbf0890SAndreas Gohr $trep = array( 155*abbf0890SAndreas Gohr 'DATE' => dformat(), 156*abbf0890SAndreas Gohr 'BROWSER' => $_SERVER['HTTP_USER_AGENT'], 157*abbf0890SAndreas Gohr 'IPADDRESS' => $ip, 158*abbf0890SAndreas Gohr 'HOSTNAME' => gethostsbyaddrs($ip), 159*abbf0890SAndreas Gohr 'TITLE' => $conf['title'], 160*abbf0890SAndreas Gohr 'DOKUWIKIURL' => DOKU_URL, 161*abbf0890SAndreas Gohr 'USER' => $_SERVER['REMOTE_USER'], 162*abbf0890SAndreas Gohr 'NAME' => $INFO['userinfo']['name'], 163*abbf0890SAndreas Gohr 'MAIL' => $INFO['userinfo']['mail'], 164*abbf0890SAndreas Gohr ); 165*abbf0890SAndreas Gohr $trep = array_merge($trep,(array) $textrep); 166*abbf0890SAndreas Gohr $hrep = array( 167*abbf0890SAndreas Gohr 'DATE' => '<i>'.hsc(dformat()).'</i>', 168*abbf0890SAndreas Gohr 'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']), 169*abbf0890SAndreas Gohr 'IPADDRESS' => '<code>'.hsc($ip).'</code>', 170*abbf0890SAndreas Gohr 'HOSTNAME' => '<code>'.hsc(gethostsbyaddrs($ip)).'</code>', 171*abbf0890SAndreas Gohr 'TITLE' => hsc($conf['title']), 172*abbf0890SAndreas Gohr 'DOKUWIKIURL' => '<a href="'.DOKU_URL.'">'.DOKU_URL.'</a>', 173*abbf0890SAndreas Gohr 'USER' => hsc($_SERVER['REMOTE_USER']), 174*abbf0890SAndreas Gohr 'NAME' => hsc($INFO['userinfo']['name']), 175*abbf0890SAndreas Gohr 'MAIL' => '<a href="mailto:"'.hsc($INFO['userinfo']['mail']).'">'. 176*abbf0890SAndreas Gohr hsc($INFO['userinfo']['mail']).'</a>', 177*abbf0890SAndreas Gohr ); 178*abbf0890SAndreas Gohr $hrep = array_merge($hrep,(array) $htmlrep); 179*abbf0890SAndreas Gohr 180*abbf0890SAndreas Gohr // Apply replacements 181*abbf0890SAndreas Gohr foreach ($trep as $key => $substitution) { 182*abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); 183*abbf0890SAndreas Gohr } 184*abbf0890SAndreas Gohr foreach ($hrep as $key => $substitution) { 185*abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@',$substitution, $html); 186*abbf0890SAndreas Gohr } 187*abbf0890SAndreas Gohr 188*abbf0890SAndreas Gohr $this->setHTML($html); 189*abbf0890SAndreas Gohr $this->setText($text); 190*abbf0890SAndreas Gohr } 191*abbf0890SAndreas Gohr 192*abbf0890SAndreas Gohr /** 193bb01c27cSAndreas Gohr * Set the HTML part of the mail 194bb01c27cSAndreas Gohr * 195bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 196*abbf0890SAndreas Gohr * 197*abbf0890SAndreas Gohr * You probably want to use setBody() instead 198bb01c27cSAndreas Gohr */ 1991d045709SAndreas Gohr public function setHTML($html){ 200bb01c27cSAndreas Gohr $this->html = $html; 201bb01c27cSAndreas Gohr } 202bb01c27cSAndreas Gohr 203bb01c27cSAndreas Gohr /** 204bb01c27cSAndreas Gohr * Set the plain text part of the mail 205*abbf0890SAndreas Gohr * 206*abbf0890SAndreas Gohr * You probably want to use setBody() instead 207bb01c27cSAndreas Gohr */ 2081d045709SAndreas Gohr public function setText($text){ 209bb01c27cSAndreas Gohr $this->text = $text; 210bb01c27cSAndreas Gohr } 211bb01c27cSAndreas Gohr 212bb01c27cSAndreas Gohr /** 213a36fc348SAndreas Gohr * Add the To: recipients 214a36fc348SAndreas Gohr * 215a36fc348SAndreas Gohr * @see setAddress 216a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 217a36fc348SAndreas Gohr */ 218a36fc348SAndreas Gohr public function to($address){ 219a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 220a36fc348SAndreas Gohr } 221a36fc348SAndreas Gohr 222a36fc348SAndreas Gohr /** 223a36fc348SAndreas Gohr * Add the Cc: recipients 224a36fc348SAndreas Gohr * 225a36fc348SAndreas Gohr * @see setAddress 226a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 227a36fc348SAndreas Gohr */ 228a36fc348SAndreas Gohr public function cc($address){ 229a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 230a36fc348SAndreas Gohr } 231a36fc348SAndreas Gohr 232a36fc348SAndreas Gohr /** 233a36fc348SAndreas Gohr * Add the Bcc: recipients 234a36fc348SAndreas Gohr * 235a36fc348SAndreas Gohr * @see setAddress 236a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 237a36fc348SAndreas Gohr */ 238a36fc348SAndreas Gohr public function bcc($address){ 239a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 240a36fc348SAndreas Gohr } 241a36fc348SAndreas Gohr 242a36fc348SAndreas Gohr /** 243a36fc348SAndreas Gohr * Add the From: address 244a36fc348SAndreas Gohr * 245a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 246a36fc348SAndreas Gohr * to call this function 247a36fc348SAndreas Gohr * 248a36fc348SAndreas Gohr * @see setAddress 249a36fc348SAndreas Gohr * @param string $address from address 250a36fc348SAndreas Gohr */ 251a36fc348SAndreas Gohr public function from($address){ 252a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 253a36fc348SAndreas Gohr } 254a36fc348SAndreas Gohr 255a36fc348SAndreas Gohr /** 256a36fc348SAndreas Gohr * Add the mail's Subject: header 257a36fc348SAndreas Gohr * 258a36fc348SAndreas Gohr * @param string $subject the mail subject 259a36fc348SAndreas Gohr */ 260a36fc348SAndreas Gohr public function subject($subject){ 261a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 262a36fc348SAndreas Gohr } 263a36fc348SAndreas Gohr 264a36fc348SAndreas Gohr /** 2651d045709SAndreas Gohr * Sets an email address header with correct encoding 266bb01c27cSAndreas Gohr * 267bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 268bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 269bb01c27cSAndreas Gohr * 270bb01c27cSAndreas Gohr * Example: 271bb01c27cSAndreas Gohr * setAddress("föö <foo@bar.com>, me@somewhere.com","TBcc"); 272bb01c27cSAndreas Gohr * 273bb01c27cSAndreas Gohr * @param string $address Multiple adresses separated by commas 274a36fc348SAndreas Gohr * @param string returns the prepared header (can contain multiple lines) 275bb01c27cSAndreas Gohr */ 276a36fc348SAndreas Gohr public function cleanAddress($address){ 277bb01c27cSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 278bb01c27cSAndreas Gohr $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 279bb01c27cSAndreas Gohr 2801d045709SAndreas Gohr $address = preg_replace('/[\r\n\0]+/',' ',$address); // remove attack vectors 2811d045709SAndreas Gohr 282bb01c27cSAndreas Gohr $headers = ''; 283bb01c27cSAndreas Gohr $parts = explode(',',$address); 284bb01c27cSAndreas Gohr foreach ($parts as $part){ 285bb01c27cSAndreas Gohr $part = trim($part); 286bb01c27cSAndreas Gohr 287bb01c27cSAndreas Gohr // parse address 288bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 289bb01c27cSAndreas Gohr $text = trim($matches[1]); 290bb01c27cSAndreas Gohr $addr = $matches[2]; 291bb01c27cSAndreas Gohr }else{ 292bb01c27cSAndreas Gohr $addr = $part; 293bb01c27cSAndreas Gohr } 294bb01c27cSAndreas Gohr // skip empty ones 295bb01c27cSAndreas Gohr if(empty($addr)){ 296bb01c27cSAndreas Gohr continue; 297bb01c27cSAndreas Gohr } 298bb01c27cSAndreas Gohr 299bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 300bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)){ 301bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 302bb01c27cSAndreas Gohr continue; 303bb01c27cSAndreas Gohr } 304bb01c27cSAndreas Gohr 3051d045709SAndreas Gohr if(is_null($this->validator)){ 3061d045709SAndreas Gohr $this->validator = new EmailAddressValidator(); 3071d045709SAndreas Gohr $this->validator->allowLocalAddresses = true; 3081d045709SAndreas Gohr } 3091d045709SAndreas Gohr if(!$this->validator->check_email_address($addr)){ 310bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 311bb01c27cSAndreas Gohr continue; 312bb01c27cSAndreas Gohr } 313bb01c27cSAndreas Gohr 314bb01c27cSAndreas Gohr // text was given 315bb01c27cSAndreas Gohr if(!empty($text) && $names){ 316bb01c27cSAndreas Gohr // add address quotes 317bb01c27cSAndreas Gohr $addr = "<$addr>"; 318bb01c27cSAndreas Gohr 319bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 320bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 321bb01c27cSAndreas Gohr $text = utf8_strip($text); 322bb01c27cSAndreas Gohr } 323bb01c27cSAndreas Gohr 324bb01c27cSAndreas Gohr if(!utf8_isASCII($text)){ 3251d045709SAndreas Gohr //FIXME check if this is needed for base64 too 326bb01c27cSAndreas Gohr // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" 327bb01c27cSAndreas Gohr /* 328bb01c27cSAndreas Gohr if (preg_match('/^"(.+)"$/', $text, $matches)) { 329bb01c27cSAndreas Gohr $text = '"=?UTF-8?Q?'.mail_quotedprintable_encode($matches[1], 0).'?="'; 330bb01c27cSAndreas Gohr } else { 331bb01c27cSAndreas Gohr $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text, 0).'?='; 332bb01c27cSAndreas Gohr } 333bb01c27cSAndreas Gohr */ 334bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 335bb01c27cSAndreas Gohr } 336bb01c27cSAndreas Gohr }else{ 337bb01c27cSAndreas Gohr $text = ''; 338bb01c27cSAndreas Gohr } 339bb01c27cSAndreas Gohr 340bb01c27cSAndreas Gohr // add to header comma seperated 341bb01c27cSAndreas Gohr if($headers != ''){ 342bb01c27cSAndreas Gohr $headers .= ', '; 343bb01c27cSAndreas Gohr } 344bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 345bb01c27cSAndreas Gohr } 346bb01c27cSAndreas Gohr 347bb01c27cSAndreas Gohr if(empty($headers)) return false; 348bb01c27cSAndreas Gohr 349bb01c27cSAndreas Gohr return $headers; 350bb01c27cSAndreas Gohr } 351bb01c27cSAndreas Gohr 352bb01c27cSAndreas Gohr 353bb01c27cSAndreas Gohr /** 354bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 355bb01c27cSAndreas Gohr * 356bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 357bb01c27cSAndreas Gohr */ 358bb01c27cSAndreas Gohr protected function prepareAttachments(){ 359bb01c27cSAndreas Gohr $mime = ''; 360bb01c27cSAndreas Gohr $part = 1; 361bb01c27cSAndreas Gohr // embedded attachments 362bb01c27cSAndreas Gohr foreach($this->attach as $media){ 363bb01c27cSAndreas Gohr // create content id 364bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 365bb01c27cSAndreas Gohr 366bb01c27cSAndreas Gohr // replace wildcards 367bb01c27cSAndreas Gohr if($media['embed']){ 368bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%','cid:'.$cid,$this->html); 369bb01c27cSAndreas Gohr } 370bb01c27cSAndreas Gohr 371bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 372bb01c27cSAndreas Gohr $mime .= 'Content-Type: '.$media['mime'].';'.MAILHEADER_EOL; 373bb01c27cSAndreas Gohr $mime .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 374bb01c27cSAndreas Gohr $mime .= "Content-ID: <$cid>".MAILHEADER_EOL; 375bb01c27cSAndreas Gohr if($media['embed']){ 376bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: inline; filename="'.$media['name'].'"'.MAILHEADER_EOL; 377bb01c27cSAndreas Gohr }else{ 378bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: attachment; filename="'.$media['name'].'"'.MAILHEADER_EOL; 379bb01c27cSAndreas Gohr } 380bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 381bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']),74,MAILHEADER_EOL); 382bb01c27cSAndreas Gohr 383bb01c27cSAndreas Gohr $part++; 384bb01c27cSAndreas Gohr } 385bb01c27cSAndreas Gohr return $mime; 386bb01c27cSAndreas Gohr } 387bb01c27cSAndreas Gohr 3881d045709SAndreas Gohr /** 3891d045709SAndreas Gohr * Build the body and handles multi part mails 3901d045709SAndreas Gohr * 3911d045709SAndreas Gohr * Needs to be called before prepareHeaders! 3921d045709SAndreas Gohr * 3931d045709SAndreas Gohr * @return string the prepared mail body, false on errors 3941d045709SAndreas Gohr */ 3951d045709SAndreas Gohr protected function prepareBody(){ 3961d045709SAndreas Gohr global $conf; 3971d045709SAndreas Gohr 398bb01c27cSAndreas Gohr // check for body 399bb01c27cSAndreas Gohr if(!$this->text && !$this->html){ 400bb01c27cSAndreas Gohr return false; 401bb01c27cSAndreas Gohr } 402bb01c27cSAndreas Gohr 403bb01c27cSAndreas Gohr // add general headers 404bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 405bb01c27cSAndreas Gohr 4061d045709SAndreas Gohr $body = ''; 4071d045709SAndreas Gohr 408bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)){ // we can send a simple single part message 409bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 410bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 4111d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 412bb01c27cSAndreas Gohr }else{ // multi part it is 4131d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 414bb01c27cSAndreas Gohr 415bb01c27cSAndreas Gohr // prepare the attachments 416bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 417bb01c27cSAndreas Gohr 418bb01c27cSAndreas Gohr // do we have alternative text content? 419bb01c27cSAndreas Gohr if($this->text && $this->html){ 420a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 421a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 422bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4231d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4241d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 425bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 426bb01c27cSAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 427bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 428a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 429a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'"'.MAILHEADER_EOL; 430bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 431bb01c27cSAndreas Gohr } 432bb01c27cSAndreas Gohr 4331d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4341d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4351d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 436bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 4371d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->html),74,MAILHEADER_EOL); 438bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 439bb01c27cSAndreas Gohr $body .= $attachments; 440bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 441bb01c27cSAndreas Gohr 442bb01c27cSAndreas Gohr // close open multipart/alternative boundary 443bb01c27cSAndreas Gohr if($this->text && $this->html){ 444bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 445bb01c27cSAndreas Gohr } 446bb01c27cSAndreas Gohr } 447bb01c27cSAndreas Gohr 448bb01c27cSAndreas Gohr return $body; 449bb01c27cSAndreas Gohr } 450bb01c27cSAndreas Gohr 451bb01c27cSAndreas Gohr /** 452a36fc348SAndreas Gohr * Cleanup and encode the headers array 453a36fc348SAndreas Gohr */ 454a36fc348SAndreas Gohr protected function cleanHeaders(){ 455a36fc348SAndreas Gohr global $conf; 456a36fc348SAndreas Gohr 457a36fc348SAndreas Gohr // clean up addresses 458a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 459a36fc348SAndreas Gohr $addrs = array('To','From','Cc','Bcc'); 460a36fc348SAndreas Gohr foreach($addrs as $addr){ 461a36fc348SAndreas Gohr if(isset($this->headers[$addr])){ 462a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 463a36fc348SAndreas Gohr } 464a36fc348SAndreas Gohr } 465a36fc348SAndreas Gohr 466a36fc348SAndreas Gohr if(isset($subject)){ 467a36fc348SAndreas Gohr // add prefix to subject 46854f30755SAndreas Gohr if(empty($conf['mailprefix'])){ 4698a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 47054f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 47154f30755SAndreas Gohr }else{ 4728a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 4738a215f09SAndreas Gohr } 4748a215f09SAndreas Gohr }else{ 475a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 47654f30755SAndreas Gohr } 477a36fc348SAndreas Gohr $len = strlen($prefix); 478a36fc348SAndreas Gohr if(substr($this->headers['subject'],0,$len) != $prefix){ 479a36fc348SAndreas Gohr $this->headers['subject'] = $prefix.' '.$this->headers['subject']; 480a36fc348SAndreas Gohr } 481a36fc348SAndreas Gohr 482a36fc348SAndreas Gohr // encode subject 483a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 484a36fc348SAndreas Gohr $this->headers['subject'] = utf8_deaccent($this->headers['subject']); 485a36fc348SAndreas Gohr $this->headers['subject'] = utf8_strip($this->headers['subject']); 486a36fc348SAndreas Gohr } 487a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])){ 488a36fc348SAndreas Gohr $subject = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 489a36fc348SAndreas Gohr } 490a36fc348SAndreas Gohr } 491a36fc348SAndreas Gohr 492a36fc348SAndreas Gohr // wrap headers 493a36fc348SAndreas Gohr foreach($this->headers as $key => $val){ 494a36fc348SAndreas Gohr $this->headers[$key] = wordwrap($val,78,MAILHEADER_EOL.' '); 495a36fc348SAndreas Gohr } 496a36fc348SAndreas Gohr } 497a36fc348SAndreas Gohr 498a36fc348SAndreas Gohr /** 499bb01c27cSAndreas Gohr * Create a string from the headers array 5001d045709SAndreas Gohr * 5011d045709SAndreas Gohr * @returns string the headers 502bb01c27cSAndreas Gohr */ 503bb01c27cSAndreas Gohr protected function prepareHeaders(){ 504bb01c27cSAndreas Gohr $headers = ''; 505bb01c27cSAndreas Gohr foreach($this->headers as $key => $val){ 506bb01c27cSAndreas Gohr $headers .= "$key: $val".MAILHEADER_EOL; 507bb01c27cSAndreas Gohr } 508bb01c27cSAndreas Gohr return $headers; 509bb01c27cSAndreas Gohr } 510bb01c27cSAndreas Gohr 511bb01c27cSAndreas Gohr /** 512bb01c27cSAndreas Gohr * return a full email with all headers 513bb01c27cSAndreas Gohr * 5141d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5151d045709SAndreas Gohr * used for MHT exports 5161d045709SAndreas Gohr * 5171d045709SAndreas Gohr * @return string the mail, false on errors 518bb01c27cSAndreas Gohr */ 519bb01c27cSAndreas Gohr public function dump(){ 520a36fc348SAndreas Gohr $this->cleanHeaders(); 521bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5221d045709SAndreas Gohr if($body === 'false') return false; 5231d045709SAndreas Gohr $headers = $this->prepareHeaders(); 524bb01c27cSAndreas Gohr 525bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 526bb01c27cSAndreas Gohr } 5271d045709SAndreas Gohr 5281d045709SAndreas Gohr /** 5291d045709SAndreas Gohr * Send the mail 5301d045709SAndreas Gohr * 5311d045709SAndreas Gohr * Call this after all data was set 5321d045709SAndreas Gohr * 53328d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 5341d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 5351d045709SAndreas Gohr */ 5361d045709SAndreas Gohr public function send(){ 53728d2ad80SAndreas Gohr $success = false; 538a36fc348SAndreas Gohr 53928d2ad80SAndreas Gohr // prepare hook data 54028d2ad80SAndreas Gohr $data = array( 54128d2ad80SAndreas Gohr // pass the whole mail class to plugin 54228d2ad80SAndreas Gohr 'mail' => $this, 54328d2ad80SAndreas Gohr // pass references for backward compatibility 54428d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 54528d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 54628d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 54728d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 54828d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 54928d2ad80SAndreas Gohr 'body' => &$this->text, 55028d2ad80SAndreas Gohr 'params' => &$this->sendparams, 55128d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 55228d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 55328d2ad80SAndreas Gohr 'success' => &$success, 55428d2ad80SAndreas Gohr ); 55528d2ad80SAndreas Gohr 55628d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 55728d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 55828d2ad80SAndreas Gohr if ($evt->advise_before(true)) { 55928d2ad80SAndreas Gohr // clean up before using the headers 560a36fc348SAndreas Gohr $this->cleanHeaders(); 561a36fc348SAndreas Gohr 5621d045709SAndreas Gohr // any recipients? 5631d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 5641d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 5651d045709SAndreas Gohr trim($this->headers['Bcc']) === '') return false; 5661d045709SAndreas Gohr 5671d045709SAndreas Gohr // The To: header is special 5681d045709SAndreas Gohr if(isset($this->headers['To'])){ 5691d045709SAndreas Gohr $to = $this->headers['To']; 5701d045709SAndreas Gohr unset($this->headers['To']); 5711d045709SAndreas Gohr }else{ 5721d045709SAndreas Gohr $to = ''; 5731d045709SAndreas Gohr } 5741d045709SAndreas Gohr 5751d045709SAndreas Gohr // so is the subject 5761d045709SAndreas Gohr if(isset($this->headers['Subject'])){ 5771d045709SAndreas Gohr $subject = $this->headers['Subject']; 5781d045709SAndreas Gohr unset($this->headers['Subject']); 5791d045709SAndreas Gohr }else{ 5801d045709SAndreas Gohr $subject = ''; 5811d045709SAndreas Gohr } 5821d045709SAndreas Gohr 5831d045709SAndreas Gohr // make the body 5841d045709SAndreas Gohr $body = $this->prepareBody(); 5851d045709SAndreas Gohr if($body === 'false') return false; 5861d045709SAndreas Gohr 5871d045709SAndreas Gohr // cook the headers 5881d045709SAndreas Gohr $headers = $this->prepareHeaders(); 58928d2ad80SAndreas Gohr // add any headers set by legacy plugins 59028d2ad80SAndreas Gohr if(trim($data['headers'])){ 59128d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 59228d2ad80SAndreas Gohr } 5931d045709SAndreas Gohr 5941d045709SAndreas Gohr // send the thing 5951d045709SAndreas Gohr if(is_null($this->sendparam)){ 59628d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers); 5971d045709SAndreas Gohr }else{ 59828d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers,$this->sendparam); 5991d045709SAndreas Gohr } 6001d045709SAndreas Gohr } 60128d2ad80SAndreas Gohr // any AFTER actions? 60228d2ad80SAndreas Gohr $evt->advise_after(); 60328d2ad80SAndreas Gohr return $success; 60428d2ad80SAndreas Gohr } 605bb01c27cSAndreas Gohr} 606