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