[ Index ] |
|
Code source de DokuWiki 2006-11-06 |
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 */ 36 function 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 */ 83 function 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 * 148 * @link http://www.webmasterworld.com/forum88/135.htm 149 * 150 * @param string $email the address to check 151 * @return bool true if address is valid 152 */ 153 function mail_isvalid($email){ 154 return eregi("^[0-9a-z]([+-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 155 } 156 157 /** 158 * Quoted printable encoding 159 * 160 * @author umu <umuAThrz.tu-chemnitz.de> 161 * @link http://www.php.net/manual/en/function.imap-8bit.php#61216 162 */ 163 function mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) { 164 // split text into lines 165 $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText); 166 167 for ($i=0;$i<count($aLines);$i++) { 168 $sLine =& $aLines[$i]; 169 if (strlen($sLine)===0) continue; // do nothing, if empty 170 171 $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e'; 172 173 // imap_8bit encodes x09 everywhere, not only at lineends, 174 // for EBCDIC safeness encode !"#$@[\]^`{|}~, 175 // for complete safeness encode every character :) 176 if ($bEmulate_imap_8bit) 177 $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e'; 178 179 $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;'; 180 $sLine = preg_replace( $sRegExp, $sReplmt, $sLine ); 181 182 // encode x09,x20 at lineends 183 { 184 $iLength = strlen($sLine); 185 $iLastChar = ord($sLine{$iLength-1}); 186 187 // !!!!!!!! 188 // imap_8_bit does not encode x20 at the very end of a text, 189 // here is, where I don't agree with imap_8_bit, 190 // please correct me, if I'm wrong, 191 // or comment next line for RFC2045 conformance, if you like 192 if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))) 193 194 if (($iLastChar==0x09)||($iLastChar==0x20)) { 195 $sLine{$iLength-1}='='; 196 $sLine .= ($iLastChar==0x09)?'09':'20'; 197 } 198 } // imap_8bit encodes x20 before chr(13), too 199 // although IMHO not requested by RFC2045, why not do it safer :) 200 // and why not encode any x20 around chr(10) or chr(13) 201 if ($bEmulate_imap_8bit) { 202 $sLine=str_replace(' =0D','=20=0D',$sLine); 203 //$sLine=str_replace(' =0A','=20=0A',$sLine); 204 //$sLine=str_replace('=0D ','=0D=20',$sLine); 205 //$sLine=str_replace('=0A ','=0A=20',$sLine); 206 } 207 208 // finally split into softlines no longer than $maxlen chars, 209 // for even more safeness one could encode x09,x20 210 // at the very first character of the line 211 // and after soft linebreaks, as well, 212 // but this wouldn't be caught by such an easy RegExp 213 if($maxlen){ 214 preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch ); 215 $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's 216 } 217 } 218 219 // join lines into text 220 return implode(MAILHEADER_EOL,$aLines); 221 } 222 223 224 //Setup VIM: ex: et ts=2 enc=utf-8 :
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 20:47:31 2007 | par Balluche grâce à PHPXref 0.7 |