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