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