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 // 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).'?='; 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 <pob@medienrecht.org> 159 * @author <tamas.tompa@kirowski.com> 160 * @link http://www.php.net/manual/en/function.quoted-printable-decode.php 161 */ 162function mail_quotedprintable_encode($input='',$line_max=74,$space_conv=false){ 163 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); 164 $lines = preg_split("/(?:\r\n|\r|\n)/", $input); 165 $eol = "\n"; 166 $escape = "="; 167 $output = ""; 168 while( list(, $line) = each($lines) ) { 169 //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary 170 $linlen = strlen($line); 171 $newline = ""; 172 for($i = 0; $i < $linlen; $i++) { 173 $c = substr( $line, $i, 1 ); 174 $dec = ord( $c ); 175 if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E 176 $c = "=2E"; 177 } 178 if ( $dec == 32 ) { 179 if ( $i == ( $linlen - 1 ) ) { // convert space at eol only 180 $c = "=20"; 181 } else if ( $space_conv ) { 182 $c = "=20"; 183 } 184 } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required 185 $h2 = floor($dec/16); 186 $h1 = floor($dec%16); 187 $c = $escape.$hex["$h2"].$hex["$h1"]; 188 } 189 if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted 190 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay 191 $newline = ""; 192 // check if newline first character will be point or not 193 if ( $dec == 46 ) { 194 $c = "=2E"; 195 } 196 } 197 $newline .= $c; 198 } // end of for 199 $output .= $newline.$eol; 200 } // end of while 201 return trim($output); 202} 203 204 205 206//Setup VIM: ex: et ts=2 enc=utf-8 : 207