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