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