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