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 * @fixme the HTML head and body still needs to be set 134abbf0890SAndreas Gohr * @param string $text plain text body 135abbf0890SAndreas Gohr * @param array $textrep replacements to apply on the text part 136abbf0890SAndreas Gohr * @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep 137abbf0890SAndreas Gohr * @param array $html the HTML body, leave null to create it from $text 138abbf0890SAndreas Gohr */ 139abbf0890SAndreas Gohr public function setBody($text, $textrep=null, $htmlrep=null, $html=null){ 140abbf0890SAndreas Gohr global $INFO; 141abbf0890SAndreas Gohr global $conf; 142*76efd6d0SAndreas Gohr $htmlrep = (array) $htmlrep; 143*76efd6d0SAndreas 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*76efd6d0SAndreas Gohr // copy over all replacements missing for HTML (autolink URLs) 151*76efd6d0SAndreas Gohr foreach($textrep as $key => $value){ 152*76efd6d0SAndreas Gohr if(isset($htmlrep[$key])) continue; 153*76efd6d0SAndreas Gohr if(preg_match('/^https?:\/\//i',$value)){ 154*76efd6d0SAndreas Gohr $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>'; 155*76efd6d0SAndreas Gohr }else{ 156*76efd6d0SAndreas Gohr $htmlrep[$key] = hsc($value); 157*76efd6d0SAndreas Gohr } 158abbf0890SAndreas Gohr } 159abbf0890SAndreas Gohr 160abbf0890SAndreas Gohr // prepare default replacements 161abbf0890SAndreas Gohr $ip = clientIP(); 162abbf0890SAndreas Gohr $trep = array( 163abbf0890SAndreas Gohr 'DATE' => dformat(), 164abbf0890SAndreas Gohr 'BROWSER' => $_SERVER['HTTP_USER_AGENT'], 165abbf0890SAndreas Gohr 'IPADDRESS' => $ip, 166abbf0890SAndreas Gohr 'HOSTNAME' => gethostsbyaddrs($ip), 167abbf0890SAndreas Gohr 'TITLE' => $conf['title'], 168abbf0890SAndreas Gohr 'DOKUWIKIURL' => DOKU_URL, 169abbf0890SAndreas Gohr 'USER' => $_SERVER['REMOTE_USER'], 170abbf0890SAndreas Gohr 'NAME' => $INFO['userinfo']['name'], 171abbf0890SAndreas Gohr 'MAIL' => $INFO['userinfo']['mail'], 172abbf0890SAndreas Gohr ); 173abbf0890SAndreas Gohr $trep = array_merge($trep,(array) $textrep); 174abbf0890SAndreas Gohr $hrep = array( 175abbf0890SAndreas Gohr 'DATE' => '<i>'.hsc(dformat()).'</i>', 176abbf0890SAndreas Gohr 'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']), 177abbf0890SAndreas Gohr 'IPADDRESS' => '<code>'.hsc($ip).'</code>', 178abbf0890SAndreas Gohr 'HOSTNAME' => '<code>'.hsc(gethostsbyaddrs($ip)).'</code>', 179abbf0890SAndreas Gohr 'TITLE' => hsc($conf['title']), 180abbf0890SAndreas Gohr 'DOKUWIKIURL' => '<a href="'.DOKU_URL.'">'.DOKU_URL.'</a>', 181abbf0890SAndreas Gohr 'USER' => hsc($_SERVER['REMOTE_USER']), 182abbf0890SAndreas Gohr 'NAME' => hsc($INFO['userinfo']['name']), 183abbf0890SAndreas Gohr 'MAIL' => '<a href="mailto:"'.hsc($INFO['userinfo']['mail']).'">'. 184abbf0890SAndreas Gohr hsc($INFO['userinfo']['mail']).'</a>', 185abbf0890SAndreas Gohr ); 186abbf0890SAndreas Gohr $hrep = array_merge($hrep,(array) $htmlrep); 187abbf0890SAndreas Gohr 188abbf0890SAndreas Gohr // Apply replacements 189abbf0890SAndreas Gohr foreach ($trep as $key => $substitution) { 190abbf0890SAndreas Gohr $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); 191abbf0890SAndreas Gohr } 192abbf0890SAndreas Gohr foreach ($hrep as $key => $substitution) { 193abbf0890SAndreas Gohr $html = str_replace('@'.strtoupper($key).'@',$substitution, $html); 194abbf0890SAndreas Gohr } 195abbf0890SAndreas Gohr 196abbf0890SAndreas Gohr $this->setHTML($html); 197abbf0890SAndreas Gohr $this->setText($text); 198abbf0890SAndreas Gohr } 199abbf0890SAndreas Gohr 200abbf0890SAndreas Gohr /** 201bb01c27cSAndreas Gohr * Set the HTML part of the mail 202bb01c27cSAndreas Gohr * 203bb01c27cSAndreas Gohr * Placeholders can be used to reference embedded attachments 204abbf0890SAndreas Gohr * 205abbf0890SAndreas Gohr * You probably want to use setBody() instead 206bb01c27cSAndreas Gohr */ 2071d045709SAndreas Gohr public function setHTML($html){ 208bb01c27cSAndreas Gohr $this->html = $html; 209bb01c27cSAndreas Gohr } 210bb01c27cSAndreas Gohr 211bb01c27cSAndreas Gohr /** 212bb01c27cSAndreas Gohr * Set the plain text part of the mail 213abbf0890SAndreas Gohr * 214abbf0890SAndreas Gohr * You probably want to use setBody() instead 215bb01c27cSAndreas Gohr */ 2161d045709SAndreas Gohr public function setText($text){ 217bb01c27cSAndreas Gohr $this->text = $text; 218bb01c27cSAndreas Gohr } 219bb01c27cSAndreas Gohr 220bb01c27cSAndreas Gohr /** 221a36fc348SAndreas Gohr * Add the To: recipients 222a36fc348SAndreas Gohr * 223a36fc348SAndreas Gohr * @see setAddress 224a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 225a36fc348SAndreas Gohr */ 226a36fc348SAndreas Gohr public function to($address){ 227a36fc348SAndreas Gohr $this->setHeader('To', $address, false); 228a36fc348SAndreas Gohr } 229a36fc348SAndreas Gohr 230a36fc348SAndreas Gohr /** 231a36fc348SAndreas Gohr * Add the Cc: recipients 232a36fc348SAndreas Gohr * 233a36fc348SAndreas Gohr * @see setAddress 234a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 235a36fc348SAndreas Gohr */ 236a36fc348SAndreas Gohr public function cc($address){ 237a36fc348SAndreas Gohr $this->setHeader('Cc', $address, false); 238a36fc348SAndreas Gohr } 239a36fc348SAndreas Gohr 240a36fc348SAndreas Gohr /** 241a36fc348SAndreas Gohr * Add the Bcc: recipients 242a36fc348SAndreas Gohr * 243a36fc348SAndreas Gohr * @see setAddress 244a36fc348SAndreas Gohr * @param string $address Multiple adresses separated by commas 245a36fc348SAndreas Gohr */ 246a36fc348SAndreas Gohr public function bcc($address){ 247a36fc348SAndreas Gohr $this->setHeader('Bcc', $address, false); 248a36fc348SAndreas Gohr } 249a36fc348SAndreas Gohr 250a36fc348SAndreas Gohr /** 251a36fc348SAndreas Gohr * Add the From: address 252a36fc348SAndreas Gohr * 253a36fc348SAndreas Gohr * This is set to $conf['mailfrom'] when not specified so you shouldn't need 254a36fc348SAndreas Gohr * to call this function 255a36fc348SAndreas Gohr * 256a36fc348SAndreas Gohr * @see setAddress 257a36fc348SAndreas Gohr * @param string $address from address 258a36fc348SAndreas Gohr */ 259a36fc348SAndreas Gohr public function from($address){ 260a36fc348SAndreas Gohr $this->setHeader('From', $address, false); 261a36fc348SAndreas Gohr } 262a36fc348SAndreas Gohr 263a36fc348SAndreas Gohr /** 264a36fc348SAndreas Gohr * Add the mail's Subject: header 265a36fc348SAndreas Gohr * 266a36fc348SAndreas Gohr * @param string $subject the mail subject 267a36fc348SAndreas Gohr */ 268a36fc348SAndreas Gohr public function subject($subject){ 269a36fc348SAndreas Gohr $this->headers['Subject'] = $subject; 270a36fc348SAndreas Gohr } 271a36fc348SAndreas Gohr 272a36fc348SAndreas Gohr /** 2731d045709SAndreas Gohr * Sets an email address header with correct encoding 274bb01c27cSAndreas Gohr * 275bb01c27cSAndreas Gohr * Unicode characters will be deaccented and encoded base64 276bb01c27cSAndreas Gohr * for headers. Addresses may not contain Non-ASCII data! 277bb01c27cSAndreas Gohr * 278bb01c27cSAndreas Gohr * Example: 279bb01c27cSAndreas Gohr * setAddress("föö <foo@bar.com>, me@somewhere.com","TBcc"); 280bb01c27cSAndreas Gohr * 281bb01c27cSAndreas Gohr * @param string $address Multiple adresses separated by commas 282a36fc348SAndreas Gohr * @param string returns the prepared header (can contain multiple lines) 283bb01c27cSAndreas Gohr */ 284a36fc348SAndreas Gohr public function cleanAddress($address){ 285bb01c27cSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 286bb01c27cSAndreas Gohr $names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 287bb01c27cSAndreas Gohr 2881d045709SAndreas Gohr $address = preg_replace('/[\r\n\0]+/',' ',$address); // remove attack vectors 2891d045709SAndreas Gohr 290bb01c27cSAndreas Gohr $headers = ''; 291bb01c27cSAndreas Gohr $parts = explode(',',$address); 292bb01c27cSAndreas Gohr foreach ($parts as $part){ 293bb01c27cSAndreas Gohr $part = trim($part); 294bb01c27cSAndreas Gohr 295bb01c27cSAndreas Gohr // parse address 296bb01c27cSAndreas Gohr if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 297bb01c27cSAndreas Gohr $text = trim($matches[1]); 298bb01c27cSAndreas Gohr $addr = $matches[2]; 299bb01c27cSAndreas Gohr }else{ 300bb01c27cSAndreas Gohr $addr = $part; 301bb01c27cSAndreas Gohr } 302bb01c27cSAndreas Gohr // skip empty ones 303bb01c27cSAndreas Gohr if(empty($addr)){ 304bb01c27cSAndreas Gohr continue; 305bb01c27cSAndreas Gohr } 306bb01c27cSAndreas Gohr 307bb01c27cSAndreas Gohr // FIXME: is there a way to encode the localpart of a emailaddress? 308bb01c27cSAndreas Gohr if(!utf8_isASCII($addr)){ 309bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 310bb01c27cSAndreas Gohr continue; 311bb01c27cSAndreas Gohr } 312bb01c27cSAndreas Gohr 3131d045709SAndreas Gohr if(is_null($this->validator)){ 3141d045709SAndreas Gohr $this->validator = new EmailAddressValidator(); 3151d045709SAndreas Gohr $this->validator->allowLocalAddresses = true; 3161d045709SAndreas Gohr } 3171d045709SAndreas Gohr if(!$this->validator->check_email_address($addr)){ 318bb01c27cSAndreas Gohr msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 319bb01c27cSAndreas Gohr continue; 320bb01c27cSAndreas Gohr } 321bb01c27cSAndreas Gohr 322bb01c27cSAndreas Gohr // text was given 323bb01c27cSAndreas Gohr if(!empty($text) && $names){ 324bb01c27cSAndreas Gohr // add address quotes 325bb01c27cSAndreas Gohr $addr = "<$addr>"; 326bb01c27cSAndreas Gohr 327bb01c27cSAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 328bb01c27cSAndreas Gohr $text = utf8_deaccent($text); 329bb01c27cSAndreas Gohr $text = utf8_strip($text); 330bb01c27cSAndreas Gohr } 331bb01c27cSAndreas Gohr 332bb01c27cSAndreas Gohr if(!utf8_isASCII($text)){ 3331d045709SAndreas Gohr //FIXME check if this is needed for base64 too 334bb01c27cSAndreas Gohr // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" 335bb01c27cSAndreas Gohr /* 336bb01c27cSAndreas Gohr if (preg_match('/^"(.+)"$/', $text, $matches)) { 337bb01c27cSAndreas Gohr $text = '"=?UTF-8?Q?'.mail_quotedprintable_encode($matches[1], 0).'?="'; 338bb01c27cSAndreas Gohr } else { 339bb01c27cSAndreas Gohr $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text, 0).'?='; 340bb01c27cSAndreas Gohr } 341bb01c27cSAndreas Gohr */ 342bb01c27cSAndreas Gohr $text = '=?UTF-8?B?'.base64_encode($text).'?='; 343bb01c27cSAndreas Gohr } 344bb01c27cSAndreas Gohr }else{ 345bb01c27cSAndreas Gohr $text = ''; 346bb01c27cSAndreas Gohr } 347bb01c27cSAndreas Gohr 348bb01c27cSAndreas Gohr // add to header comma seperated 349bb01c27cSAndreas Gohr if($headers != ''){ 350bb01c27cSAndreas Gohr $headers .= ', '; 351bb01c27cSAndreas Gohr } 352bb01c27cSAndreas Gohr $headers .= $text.' '.$addr; 353bb01c27cSAndreas Gohr } 354bb01c27cSAndreas Gohr 355bb01c27cSAndreas Gohr if(empty($headers)) return false; 356bb01c27cSAndreas Gohr 357bb01c27cSAndreas Gohr return $headers; 358bb01c27cSAndreas Gohr } 359bb01c27cSAndreas Gohr 360bb01c27cSAndreas Gohr 361bb01c27cSAndreas Gohr /** 362bb01c27cSAndreas Gohr * Prepare the mime multiparts for all attachments 363bb01c27cSAndreas Gohr * 364bb01c27cSAndreas Gohr * Replaces placeholders in the HTML with the correct CIDs 365bb01c27cSAndreas Gohr */ 366bb01c27cSAndreas Gohr protected function prepareAttachments(){ 367bb01c27cSAndreas Gohr $mime = ''; 368bb01c27cSAndreas Gohr $part = 1; 369bb01c27cSAndreas Gohr // embedded attachments 370bb01c27cSAndreas Gohr foreach($this->attach as $media){ 371bb01c27cSAndreas Gohr // create content id 372bb01c27cSAndreas Gohr $cid = 'part'.$part.'.'.$this->partid; 373bb01c27cSAndreas Gohr 374bb01c27cSAndreas Gohr // replace wildcards 375bb01c27cSAndreas Gohr if($media['embed']){ 376bb01c27cSAndreas Gohr $this->html = str_replace('%%'.$media['embed'].'%%','cid:'.$cid,$this->html); 377bb01c27cSAndreas Gohr } 378bb01c27cSAndreas Gohr 379bb01c27cSAndreas Gohr $mime .= '--'.$this->boundary.MAILHEADER_EOL; 380bb01c27cSAndreas Gohr $mime .= 'Content-Type: '.$media['mime'].';'.MAILHEADER_EOL; 381bb01c27cSAndreas Gohr $mime .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 382bb01c27cSAndreas Gohr $mime .= "Content-ID: <$cid>".MAILHEADER_EOL; 383bb01c27cSAndreas Gohr if($media['embed']){ 384bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: inline; filename="'.$media['name'].'"'.MAILHEADER_EOL; 385bb01c27cSAndreas Gohr }else{ 386bb01c27cSAndreas Gohr $mime .= 'Content-Disposition: attachment; filename="'.$media['name'].'"'.MAILHEADER_EOL; 387bb01c27cSAndreas Gohr } 388bb01c27cSAndreas Gohr $mime .= MAILHEADER_EOL; //end of headers 389bb01c27cSAndreas Gohr $mime .= chunk_split(base64_encode($media['data']),74,MAILHEADER_EOL); 390bb01c27cSAndreas Gohr 391bb01c27cSAndreas Gohr $part++; 392bb01c27cSAndreas Gohr } 393bb01c27cSAndreas Gohr return $mime; 394bb01c27cSAndreas Gohr } 395bb01c27cSAndreas Gohr 3961d045709SAndreas Gohr /** 3971d045709SAndreas Gohr * Build the body and handles multi part mails 3981d045709SAndreas Gohr * 3991d045709SAndreas Gohr * Needs to be called before prepareHeaders! 4001d045709SAndreas Gohr * 4011d045709SAndreas Gohr * @return string the prepared mail body, false on errors 4021d045709SAndreas Gohr */ 4031d045709SAndreas Gohr protected function prepareBody(){ 4041d045709SAndreas Gohr global $conf; 4051d045709SAndreas Gohr 406bb01c27cSAndreas Gohr // check for body 407bb01c27cSAndreas Gohr if(!$this->text && !$this->html){ 408bb01c27cSAndreas Gohr return false; 409bb01c27cSAndreas Gohr } 410bb01c27cSAndreas Gohr 411bb01c27cSAndreas Gohr // add general headers 412bb01c27cSAndreas Gohr $this->headers['MIME-Version'] = '1.0'; 413bb01c27cSAndreas Gohr 4141d045709SAndreas Gohr $body = ''; 4151d045709SAndreas Gohr 416bb01c27cSAndreas Gohr if(!$this->html && !count($this->attach)){ // we can send a simple single part message 417bb01c27cSAndreas Gohr $this->headers['Content-Type'] = 'text/plain; charset=UTF-8'; 418bb01c27cSAndreas Gohr $this->headers['Content-Transfer-Encoding'] = 'base64'; 4191d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 420bb01c27cSAndreas Gohr }else{ // multi part it is 4211d045709SAndreas Gohr $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL; 422bb01c27cSAndreas Gohr 423bb01c27cSAndreas Gohr // prepare the attachments 424bb01c27cSAndreas Gohr $attachments = $this->prepareAttachments(); 425bb01c27cSAndreas Gohr 426bb01c27cSAndreas Gohr // do we have alternative text content? 427bb01c27cSAndreas Gohr if($this->text && $this->html){ 428a36fc348SAndreas Gohr $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL. 429a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'XX"'; 430bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 4311d045709SAndreas Gohr $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 4321d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 433bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 434bb01c27cSAndreas Gohr $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL); 435bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL; 436a36fc348SAndreas Gohr $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL. 437a36fc348SAndreas Gohr ' boundary="'.$this->boundary.'"'.MAILHEADER_EOL; 438bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 439bb01c27cSAndreas Gohr } 440bb01c27cSAndreas Gohr 4411d045709SAndreas Gohr $body .= '--'.$this->boundary.MAILHEADER_EOL; 4421d045709SAndreas Gohr $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL; 4431d045709SAndreas Gohr $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL; 444bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 4451d045709SAndreas Gohr $body .= chunk_split(base64_encode($this->html),74,MAILHEADER_EOL); 446bb01c27cSAndreas Gohr $body .= MAILHEADER_EOL; 447bb01c27cSAndreas Gohr $body .= $attachments; 448bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL; 449bb01c27cSAndreas Gohr 450bb01c27cSAndreas Gohr // close open multipart/alternative boundary 451bb01c27cSAndreas Gohr if($this->text && $this->html){ 452bb01c27cSAndreas Gohr $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL; 453bb01c27cSAndreas Gohr } 454bb01c27cSAndreas Gohr } 455bb01c27cSAndreas Gohr 456bb01c27cSAndreas Gohr return $body; 457bb01c27cSAndreas Gohr } 458bb01c27cSAndreas Gohr 459bb01c27cSAndreas Gohr /** 460a36fc348SAndreas Gohr * Cleanup and encode the headers array 461a36fc348SAndreas Gohr */ 462a36fc348SAndreas Gohr protected function cleanHeaders(){ 463a36fc348SAndreas Gohr global $conf; 464a36fc348SAndreas Gohr 465a36fc348SAndreas Gohr // clean up addresses 466a36fc348SAndreas Gohr if(empty($this->headers['From'])) $this->from($conf['mailfrom']); 467a36fc348SAndreas Gohr $addrs = array('To','From','Cc','Bcc'); 468a36fc348SAndreas Gohr foreach($addrs as $addr){ 469a36fc348SAndreas Gohr if(isset($this->headers[$addr])){ 470a36fc348SAndreas Gohr $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]); 471a36fc348SAndreas Gohr } 472a36fc348SAndreas Gohr } 473a36fc348SAndreas Gohr 474a36fc348SAndreas Gohr if(isset($subject)){ 475a36fc348SAndreas Gohr // add prefix to subject 47654f30755SAndreas Gohr if(empty($conf['mailprefix'])){ 4778a215f09SAndreas Gohr if(utf8_strlen($conf['title']) < 20) { 47854f30755SAndreas Gohr $prefix = '['.$conf['title'].']'; 47954f30755SAndreas Gohr }else{ 4808a215f09SAndreas Gohr $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]'; 4818a215f09SAndreas Gohr } 4828a215f09SAndreas Gohr }else{ 483a36fc348SAndreas Gohr $prefix = '['.$conf['mailprefix'].']'; 48454f30755SAndreas Gohr } 485a36fc348SAndreas Gohr $len = strlen($prefix); 486a36fc348SAndreas Gohr if(substr($this->headers['subject'],0,$len) != $prefix){ 487a36fc348SAndreas Gohr $this->headers['subject'] = $prefix.' '.$this->headers['subject']; 488a36fc348SAndreas Gohr } 489a36fc348SAndreas Gohr 490a36fc348SAndreas Gohr // encode subject 491a36fc348SAndreas Gohr if(defined('MAILHEADER_ASCIIONLY')){ 492a36fc348SAndreas Gohr $this->headers['subject'] = utf8_deaccent($this->headers['subject']); 493a36fc348SAndreas Gohr $this->headers['subject'] = utf8_strip($this->headers['subject']); 494a36fc348SAndreas Gohr } 495a36fc348SAndreas Gohr if(!utf8_isASCII($this->headers['Subject'])){ 496a36fc348SAndreas Gohr $subject = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?='; 497a36fc348SAndreas Gohr } 498a36fc348SAndreas Gohr } 499a36fc348SAndreas Gohr 500a36fc348SAndreas Gohr // wrap headers 501a36fc348SAndreas Gohr foreach($this->headers as $key => $val){ 502a36fc348SAndreas Gohr $this->headers[$key] = wordwrap($val,78,MAILHEADER_EOL.' '); 503a36fc348SAndreas Gohr } 504a36fc348SAndreas Gohr } 505a36fc348SAndreas Gohr 506a36fc348SAndreas Gohr /** 507bb01c27cSAndreas Gohr * Create a string from the headers array 5081d045709SAndreas Gohr * 5091d045709SAndreas Gohr * @returns string the headers 510bb01c27cSAndreas Gohr */ 511bb01c27cSAndreas Gohr protected function prepareHeaders(){ 512bb01c27cSAndreas Gohr $headers = ''; 513bb01c27cSAndreas Gohr foreach($this->headers as $key => $val){ 514bb01c27cSAndreas Gohr $headers .= "$key: $val".MAILHEADER_EOL; 515bb01c27cSAndreas Gohr } 516bb01c27cSAndreas Gohr return $headers; 517bb01c27cSAndreas Gohr } 518bb01c27cSAndreas Gohr 519bb01c27cSAndreas Gohr /** 520bb01c27cSAndreas Gohr * return a full email with all headers 521bb01c27cSAndreas Gohr * 5221d045709SAndreas Gohr * This is mainly intended for debugging and testing but could also be 5231d045709SAndreas Gohr * used for MHT exports 5241d045709SAndreas Gohr * 5251d045709SAndreas Gohr * @return string the mail, false on errors 526bb01c27cSAndreas Gohr */ 527bb01c27cSAndreas Gohr public function dump(){ 528a36fc348SAndreas Gohr $this->cleanHeaders(); 529bb01c27cSAndreas Gohr $body = $this->prepareBody(); 5301d045709SAndreas Gohr if($body === 'false') return false; 5311d045709SAndreas Gohr $headers = $this->prepareHeaders(); 532bb01c27cSAndreas Gohr 533bb01c27cSAndreas Gohr return $headers.MAILHEADER_EOL.$body; 534bb01c27cSAndreas Gohr } 5351d045709SAndreas Gohr 5361d045709SAndreas Gohr /** 5371d045709SAndreas Gohr * Send the mail 5381d045709SAndreas Gohr * 5391d045709SAndreas Gohr * Call this after all data was set 5401d045709SAndreas Gohr * 54128d2ad80SAndreas Gohr * @triggers MAIL_MESSAGE_SEND 5421d045709SAndreas Gohr * @return bool true if the mail was successfully passed to the MTA 5431d045709SAndreas Gohr */ 5441d045709SAndreas Gohr public function send(){ 54528d2ad80SAndreas Gohr $success = false; 546a36fc348SAndreas Gohr 54728d2ad80SAndreas Gohr // prepare hook data 54828d2ad80SAndreas Gohr $data = array( 54928d2ad80SAndreas Gohr // pass the whole mail class to plugin 55028d2ad80SAndreas Gohr 'mail' => $this, 55128d2ad80SAndreas Gohr // pass references for backward compatibility 55228d2ad80SAndreas Gohr 'to' => &$this->headers['To'], 55328d2ad80SAndreas Gohr 'cc' => &$this->headers['Cc'], 55428d2ad80SAndreas Gohr 'bcc' => &$this->headers['Bcc'], 55528d2ad80SAndreas Gohr 'from' => &$this->headers['From'], 55628d2ad80SAndreas Gohr 'subject' => &$this->headers['Subject'], 55728d2ad80SAndreas Gohr 'body' => &$this->text, 55828d2ad80SAndreas Gohr 'params' => &$this->sendparams, 55928d2ad80SAndreas Gohr 'headers' => '', // plugins shouldn't use this 56028d2ad80SAndreas Gohr // signal if we mailed successfully to AFTER event 56128d2ad80SAndreas Gohr 'success' => &$success, 56228d2ad80SAndreas Gohr ); 56328d2ad80SAndreas Gohr 56428d2ad80SAndreas Gohr // do our thing if BEFORE hook approves 56528d2ad80SAndreas Gohr $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data); 56628d2ad80SAndreas Gohr if ($evt->advise_before(true)) { 56728d2ad80SAndreas Gohr // clean up before using the headers 568a36fc348SAndreas Gohr $this->cleanHeaders(); 569a36fc348SAndreas Gohr 5701d045709SAndreas Gohr // any recipients? 5711d045709SAndreas Gohr if(trim($this->headers['To']) === '' && 5721d045709SAndreas Gohr trim($this->headers['Cc']) === '' && 5731d045709SAndreas Gohr trim($this->headers['Bcc']) === '') return false; 5741d045709SAndreas Gohr 5751d045709SAndreas Gohr // The To: header is special 5761d045709SAndreas Gohr if(isset($this->headers['To'])){ 5771d045709SAndreas Gohr $to = $this->headers['To']; 5781d045709SAndreas Gohr unset($this->headers['To']); 5791d045709SAndreas Gohr }else{ 5801d045709SAndreas Gohr $to = ''; 5811d045709SAndreas Gohr } 5821d045709SAndreas Gohr 5831d045709SAndreas Gohr // so is the subject 5841d045709SAndreas Gohr if(isset($this->headers['Subject'])){ 5851d045709SAndreas Gohr $subject = $this->headers['Subject']; 5861d045709SAndreas Gohr unset($this->headers['Subject']); 5871d045709SAndreas Gohr }else{ 5881d045709SAndreas Gohr $subject = ''; 5891d045709SAndreas Gohr } 5901d045709SAndreas Gohr 5911d045709SAndreas Gohr // make the body 5921d045709SAndreas Gohr $body = $this->prepareBody(); 5931d045709SAndreas Gohr if($body === 'false') return false; 5941d045709SAndreas Gohr 5951d045709SAndreas Gohr // cook the headers 5961d045709SAndreas Gohr $headers = $this->prepareHeaders(); 59728d2ad80SAndreas Gohr // add any headers set by legacy plugins 59828d2ad80SAndreas Gohr if(trim($data['headers'])){ 59928d2ad80SAndreas Gohr $headers .= MAILHEADER_EOL.trim($data['headers']); 60028d2ad80SAndreas Gohr } 6011d045709SAndreas Gohr 6021d045709SAndreas Gohr // send the thing 6031d045709SAndreas Gohr if(is_null($this->sendparam)){ 60428d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers); 6051d045709SAndreas Gohr }else{ 60628d2ad80SAndreas Gohr $success = @mail($to,$subject,$body,$headers,$this->sendparam); 6071d045709SAndreas Gohr } 6081d045709SAndreas Gohr } 60928d2ad80SAndreas Gohr // any AFTER actions? 61028d2ad80SAndreas Gohr $evt->advise_after(); 61128d2ad80SAndreas Gohr return $success; 61228d2ad80SAndreas Gohr } 613bb01c27cSAndreas Gohr} 614