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