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