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).'?='; 42 43 $header = ''; 44 45 // use PHP mail's to field if pure ASCII-7 is available 46 $to = mail_encode_address($to,'To'); 47 if(preg_match('#=?UTF-8?=#',$to)){ 48 $header .= $to; 49 $to = null; 50 }else{ 51 $to = preg_replace('#^To: #','',$to); 52 } 53 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 return @mail($to,$subject,$body,$header,$params); 66} 67 68/** 69 * Encodes an email address header 70 * 71 * Unicode characters will be deaccented and encoded 72 * quoted_printable for headers. 73 * Addresses may not contain Non-ASCII data! 74 * 75 * Example: 76 * mail_encode_address("föö <foo@bar.com>, me@somewhere.com","To"); 77 * 78 * @param string $string Multiple headers seperated by commas 79 */ 80function mail_encode_address($string,$header='To'){ 81 $headers = ''; 82 $parts = split(',',$string); 83 foreach ($parts as $part){ 84 $part = trim($part); 85 86 // parse address 87 if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 88 $text = trim($matches[1]); 89 $addr = $matches[2]; 90 }else{ 91 $addr = $part; 92 } 93 94 // skip empty ones 95 if(empty($addr)){ 96 continue; 97 } 98 99 // FIXME: is there a way to encode the localpart of a emailaddress? 100 if(!utf8_isASCII($addr)){ 101 msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 102 continue; 103 } 104 105 if(!mail_isvalid($addr)){ 106 msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 107 continue; 108 } 109 110 // no text was given 111 if(empty($text)){ 112 $headers .= $header.': <'.$addr.'>'.MAILHEADER_EOL; 113 continue; 114 } 115 116 if(defined('MAILHEADER_ASCIIONLY')){ 117 $text = utf8_deaccent($text); 118 $text = utf8_strip($text); 119 } 120 121 122 // FIME: can make problems with long headers? 123 if(!utf8_isASCII($text)){ 124 $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text).'?='; 125 } 126 127 //construct header 128 $headers .= $header.': '.$text.' <'.$addr.'>'.MAILHEADER_EOL; 129 } 130 131 return $headers; 132} 133 134/** 135 * Uses a regular expresion to check if a given mail address is valid 136 * 137 * May not be completly RFC conform! 138 * 139 * @link http://www.webmasterworld.com/forum88/135.htm 140 * 141 * @param string $email the address to check 142 * @return bool true if address is valid 143 */ 144function mail_isvalid($email){ 145 return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 146} 147 148/** 149 * Quoted printable encoding 150 * 151 * @author <pob@medienrecht.org> 152 * @author <tamas.tompa@kirowski.com> 153 * @link http://www.php.net/manual/en/function.quoted-printable-decode.php 154 */ 155function mail_quotedprintable_encode($input='',$line_max=74,$space_conv=false){ 156 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); 157 $lines = preg_split("/(?:\r\n|\r|\n)/", $input); 158 $eol = "\n"; 159 $escape = "="; 160 $output = ""; 161 while( list(, $line) = each($lines) ) { 162 //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary 163 $linlen = strlen($line); 164 $newline = ""; 165 for($i = 0; $i < $linlen; $i++) { 166 $c = substr( $line, $i, 1 ); 167 $dec = ord( $c ); 168 if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E 169 $c = "=2E"; 170 } 171 if ( $dec == 32 ) { 172 if ( $i == ( $linlen - 1 ) ) { // convert space at eol only 173 $c = "=20"; 174 } else if ( $space_conv ) { 175 $c = "=20"; 176 } 177 } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required 178 $h2 = floor($dec/16); 179 $h1 = floor($dec%16); 180 $c = $escape.$hex["$h2"].$hex["$h1"]; 181 } 182 if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted 183 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay 184 $newline = ""; 185 // check if newline first character will be point or not 186 if ( $dec == 46 ) { 187 $c = "=2E"; 188 } 189 } 190 $newline .= $c; 191 } // end of for 192 $output .= $newline.$eol; 193 } // end of while 194 return trim($output); 195} 196 197 198 199//Setup VIM: ex: et ts=2 enc=utf-8 : 200