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