1<?php 2/** 3 * Mail functions 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?) 10// think different 11if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n"); 12#define('MAILHEADER_ASCIIONLY',1); 13 14/** 15 * Patterns for use in email detection and validation 16 * 17 * NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser 18 * the pattern uses non-capturing groups as captured groups aren't allowed in the parser 19 * select pattern delimiters with care! 20 * 21 * May not be completly RFC conform! 22 * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4) 23 * 24 * @author Chris Smith <chris@jalakai.co.uk> 25 * Check if a given mail address is valid 26 */ 27if (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-zA-Z!#$%&'*+/=?^_`{|}~-"); 28if (!defined('PREG_PATTERN_VALID_EMAIL')) define('PREG_PATTERN_VALID_EMAIL', '['.RFC2822_ATEXT.']+(?:\.['.RFC2822_ATEXT.']+)*@(?i:[0-9a-z][0-9a-z-]*\.)+(?i:[a-z]{2,63})'); 29 30/** 31 * Prepare mailfrom replacement patterns 32 * 33 * Also prepares a mailfromnobody config that contains an autoconstructed address 34 * if the mailfrom one is userdependent and this might not be wanted (subscriptions) 35 * 36 * @author Andreas Gohr <andi@splitbrain.org> 37 */ 38function mail_setup(){ 39 global $conf; 40 global $USERINFO; 41 /** @var Input $INPUT */ 42 global $INPUT; 43 44 // auto constructed address 45 $host = @parse_url(DOKU_URL,PHP_URL_HOST); 46 if(!$host) $host = 'example.com'; 47 $noreply = 'noreply@'.$host; 48 49 $replace = array(); 50 if(!empty($USERINFO['mail'])){ 51 $replace['@MAIL@'] = $USERINFO['mail']; 52 }else{ 53 $replace['@MAIL@'] = $noreply; 54 } 55 56 // use 'noreply' if no user 57 $replace['@USER@'] = $INPUT->server->str('REMOTE_USER', 'noreply', true); 58 59 if(!empty($USERINFO['name'])){ 60 $replace['@NAME@'] = $USERINFO['name']; 61 }else{ 62 $replace['@NAME@'] = ''; 63 } 64 65 // apply replacements 66 $from = str_replace(array_keys($replace), 67 array_values($replace), 68 $conf['mailfrom']); 69 70 // any replacements done? set different mailfromnone 71 if($from != $conf['mailfrom']){ 72 $conf['mailfromnobody'] = $noreply; 73 }else{ 74 $conf['mailfromnobody'] = $from; 75 } 76 $conf['mailfrom'] = $from; 77} 78 79/** 80 * UTF-8 autoencoding replacement for PHPs mail function 81 * 82 * Email address fields (To, From, Cc, Bcc can contain a textpart and an address 83 * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded 84 * automatically. You can seperate receivers by commas. 85 * 86 * @param string $to Receiver of the mail (multiple seperated by commas) 87 * @param string $subject Mailsubject 88 * @param string $body Messagebody 89 * @param string $from Sender address 90 * @param string $cc CarbonCopy receiver (multiple seperated by commas) 91 * @param string $bcc BlindCarbonCopy receiver (multiple seperated by commas) 92 * @param string $headers Additional Headers (seperated by MAILHEADER_EOL 93 * @param string $params Additonal Sendmail params (passed to mail()) 94 * 95 * @author Andreas Gohr <andi@splitbrain.org> 96 * @see mail() 97 * 98 * @deprecated User the Mailer:: class instead 99 */ 100function mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){ 101 dbg_deprecated('class Mailer::'); 102 $message = compact('to','subject','body','from','cc','bcc','headers','params'); 103 return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action'); 104} 105 106/** 107 * @param $data 108 * @return bool 109 * 110 * @deprecated User the Mailer:: class instead 111 */ 112function _mail_send_action($data) { 113 dbg_deprecated('class Mailer::'); 114 // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params 115 $to = $data['to']; 116 $subject = $data['subject']; 117 $body = $data['body']; 118 119 // add robustness in case plugin removes any of these optional values 120 $from = isset($data['from']) ? $data['from'] : ''; 121 $cc = isset($data['cc']) ? $data['cc'] : ''; 122 $bcc = isset($data['bcc']) ? $data['bcc'] : ''; 123 $headers = isset($data['headers']) ? $data['headers'] : null; 124 $params = isset($data['params']) ? $data['params'] : null; 125 126 // discard mail request if no recipients are available 127 if(trim($to) === '' && trim($cc) === '' && trim($bcc) === '') return false; 128 129 // end additional code to support event ... original mail_send() code from here 130 131 if(defined('MAILHEADER_ASCIIONLY')){ 132 $subject = utf8_deaccent($subject); 133 $subject = utf8_strip($subject); 134 } 135 136 if(!utf8_isASCII($subject)) { 137 $enc_subj = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?='; 138 // Spaces must be encoded according to rfc2047. Use the "_" shorthand 139 $enc_subj = preg_replace('/ /', '_', $enc_subj); 140 141 // quoted printable has length restriction, use base64 if needed 142 if(strlen($subject) > 74){ 143 $enc_subj = '=?UTF-8?B?'.base64_encode($subject).'?='; 144 } 145 146 $subject = $enc_subj; 147 } 148 149 $header = ''; 150 151 // No named recipients for To: in Windows (see FS#652) 152 $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 153 154 $to = mail_encode_address($to,'',$usenames); 155 $header .= mail_encode_address($from,'From'); 156 $header .= mail_encode_address($cc,'Cc'); 157 $header .= mail_encode_address($bcc,'Bcc'); 158 $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL; 159 $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 160 $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL; 161 $header .= $headers; 162 $header = trim($header); 163 164 $body = mail_quotedprintable_encode($body); 165 166 if($params == null){ 167 return @mail($to,$subject,$body,$header); 168 }else{ 169 return @mail($to,$subject,$body,$header,$params); 170 } 171} 172 173/** 174 * Encodes an email address header 175 * 176 * Unicode characters will be deaccented and encoded 177 * quoted_printable for headers. 178 * Addresses may not contain Non-ASCII data! 179 * 180 * Example: 181 * mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc"); 182 * 183 * @param string $string Multiple adresses separated by commas 184 * @param string $header Name of the header (To,Bcc,Cc,...) 185 * @param boolean $names Allow named Recipients? 186 * 187 * @deprecated User the Mailer:: class instead 188 */ 189function mail_encode_address($string,$header='',$names=true){ 190 dbg_deprecated('class Mailer::'); 191 $headers = ''; 192 $parts = explode(',',$string); 193 foreach ($parts as $part){ 194 $part = trim($part); 195 196 // parse address 197 if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 198 $text = trim($matches[1]); 199 $addr = $matches[2]; 200 }else{ 201 $addr = $part; 202 } 203 204 // skip empty ones 205 if(empty($addr)){ 206 continue; 207 } 208 209 // FIXME: is there a way to encode the localpart of a emailaddress? 210 if(!utf8_isASCII($addr)){ 211 msg(hsc("E-Mail address <$addr> is not ASCII"),-1); 212 continue; 213 } 214 215 if(!mail_isvalid($addr)){ 216 msg(hsc("E-Mail address <$addr> is not valid"),-1); 217 continue; 218 } 219 220 // text was given 221 if(!empty($text) && $names){ 222 // add address quotes 223 $addr = "<$addr>"; 224 225 if(defined('MAILHEADER_ASCIIONLY')){ 226 $text = utf8_deaccent($text); 227 $text = utf8_strip($text); 228 } 229 230 if(!utf8_isASCII($text)){ 231 // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" 232 if (preg_match('/^"(.+)"$/', $text, $matches)) { 233 $text = '"=?UTF-8?Q?'.mail_quotedprintable_encode($matches[1], 0).'?="'; 234 } else { 235 $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text, 0).'?='; 236 } 237 // additionally the space character should be encoded as =20 (or each 238 // word QP encoded separately). 239 // however this is needed only in mail headers, not globally in mail_quotedprintable_encode(). 240 $text = str_replace(" ", "=20", $text); 241 } 242 }else{ 243 $text = ''; 244 } 245 246 // add to header comma seperated 247 if($headers != ''){ 248 $headers .= ','; 249 if($header) $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers 250 } 251 $headers .= $text.' '.$addr; 252 } 253 254 if(empty($headers)) return null; 255 256 //if headername was given add it and close correctly 257 if($header) $headers = $header.': '.$headers.MAILHEADER_EOL; 258 259 return $headers; 260} 261 262/** 263 * Check if a given mail address is valid 264 * 265 * @param string $email the address to check 266 * @return bool true if address is valid 267 */ 268function mail_isvalid($email) { 269 return EmailAddressValidator::checkEmailAddress($email, true); 270} 271 272/** 273 * Quoted printable encoding 274 * 275 * @author umu <umuAThrz.tu-chemnitz.de> 276 * @link http://php.net/manual/en/function.imap-8bit.php#61216 277 * 278 * @param string $sText 279 * @param int $maxlen 280 * @param bool $bEmulate_imap_8bit 281 * 282 * @return string 283 */ 284function mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) { 285 // split text into lines 286 $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText); 287 $cnt = count($aLines); 288 289 for ($i=0;$i<$cnt;$i++) { 290 $sLine =& $aLines[$i]; 291 if (strlen($sLine)===0) continue; // do nothing, if empty 292 293 $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e'; 294 295 // imap_8bit encodes x09 everywhere, not only at lineends, 296 // for EBCDIC safeness encode !"#$@[\]^`{|}~, 297 // for complete safeness encode every character :) 298 if ($bEmulate_imap_8bit) 299 $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/'; 300 301 $sLine = preg_replace_callback( $sRegExp, 'mail_quotedprintable_encode_callback', $sLine ); 302 303 // encode x09,x20 at lineends 304 { 305 $iLength = strlen($sLine); 306 $iLastChar = ord($sLine{$iLength-1}); 307 308 // !!!!!!!! 309 // imap_8_bit does not encode x20 at the very end of a text, 310 // here is, where I don't agree with imap_8_bit, 311 // please correct me, if I'm wrong, 312 // or comment next line for RFC2045 conformance, if you like 313 if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))){ 314 if (($iLastChar==0x09)||($iLastChar==0x20)) { 315 $sLine{$iLength-1}='='; 316 $sLine .= ($iLastChar==0x09)?'09':'20'; 317 } 318 } 319 } // imap_8bit encodes x20 before chr(13), too 320 // although IMHO not requested by RFC2045, why not do it safer :) 321 // and why not encode any x20 around chr(10) or chr(13) 322 if ($bEmulate_imap_8bit) { 323 $sLine=str_replace(' =0D','=20=0D',$sLine); 324 //$sLine=str_replace(' =0A','=20=0A',$sLine); 325 //$sLine=str_replace('=0D ','=0D=20',$sLine); 326 //$sLine=str_replace('=0A ','=0A=20',$sLine); 327 } 328 329 // finally split into softlines no longer than $maxlen chars, 330 // for even more safeness one could encode x09,x20 331 // at the very first character of the line 332 // and after soft linebreaks, as well, 333 // but this wouldn't be caught by such an easy RegExp 334 if($maxlen){ 335 preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch ); 336 $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's 337 } 338 } 339 340 // join lines into text 341 return implode(MAILHEADER_EOL,$aLines); 342} 343 344function mail_quotedprintable_encode_callback($matches){ 345 return sprintf( "=%02X", ord ( $matches[0] ) ) ; 346} 347