1ed7b5f09Sandi<?php 244f669e9Sandi/** 344f669e9Sandi * Mail functions 444f669e9Sandi * 544f669e9Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 644f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org> 744f669e9Sandi */ 844f669e9Sandi 9ed7b5f09Sandi if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 10ed7b5f09Sandi require_once(DOKU_INC.'inc/utf8.php'); 1144f669e9Sandi 12*6ec64ed3Sandi define('MAILHEADER_EOL',"\n"); //end of line for mail headers 13e1906e6eSandi #define('MAILHEADER_ASCIIONLY',1); 1444f669e9Sandi 1544f669e9Sandi/** 1644f669e9Sandi * UTF-8 autoencoding replacement for PHPs mail function 1744f669e9Sandi * 1844f669e9Sandi * Email address fields (To, From, Cc, Bcc can contain a textpart and an address 1944f669e9Sandi * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded 2044f669e9Sandi * automatically. You can seperate receivers by commas. 2144f669e9Sandi * 2244f669e9Sandi * @param string $to Receiver of the mail (multiple seperated by commas) 2344f669e9Sandi * @param string $subject Mailsubject 2444f669e9Sandi * @param string $body Messagebody 2544f669e9Sandi * @param string $from Sender address 2644f669e9Sandi * @param string $cc CarbonCopy receiver (multiple seperated by commas) 2744f669e9Sandi * @param string $bcc BlindCarbonCopy receiver (multiple seperated by commas) 2844f669e9Sandi * @param string $headers Additional Headers (seperated by MAILHEADER_EOL 2944f669e9Sandi * @param string $params Additonal Sendmail params (passed to mail()) 3044f669e9Sandi * 3144f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org> 3244f669e9Sandi * @see mail() 3344f669e9Sandi */ 3444f669e9Sandifunction mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){ 35e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 36e1906e6eSandi $subject = utf8_deaccent($subject); 37e1906e6eSandi $subject = utf8_strip($subject); 38e1906e6eSandi } 39e1906e6eSandi 40e1906e6eSandi if(!utf8_isASCII($subject)) 41e1906e6eSandi $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject).'?='; 4244f669e9Sandi 4344f669e9Sandi $header = ''; 44e1906e6eSandi 45a2021ad8Sandi $to = mail_encode_address($to); 4644f669e9Sandi $header .= mail_encode_address($from,'From'); 4744f669e9Sandi $header .= mail_encode_address($cc,'Cc'); 4844f669e9Sandi $header .= mail_encode_address($bcc,'Bcc'); 4944f669e9Sandi $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL; 5044f669e9Sandi $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 5144f669e9Sandi $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL; 5244f669e9Sandi $header .= $headers; 53e1906e6eSandi $header = trim($header); 5444f669e9Sandi 5544f669e9Sandi $body = mail_quotedprintable_encode($body); 5644f669e9Sandi 57e1906e6eSandi return @mail($to,$subject,$body,$header,$params); 5844f669e9Sandi} 5944f669e9Sandi 6044f669e9Sandi/** 6144f669e9Sandi * Encodes an email address header 6244f669e9Sandi * 63e1906e6eSandi * Unicode characters will be deaccented and encoded 64e1906e6eSandi * quoted_printable for headers. 6544f669e9Sandi * Addresses may not contain Non-ASCII data! 6644f669e9Sandi * 6744f669e9Sandi * Example: 68a2021ad8Sandi * mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc"); 6944f669e9Sandi * 7044f669e9Sandi * @param string $string Multiple headers seperated by commas 7144f669e9Sandi */ 72a2021ad8Sandifunction mail_encode_address($string,$header=''){ 7344f669e9Sandi $headers = ''; 7444f669e9Sandi $parts = split(',',$string); 7544f669e9Sandi foreach ($parts as $part){ 7644f669e9Sandi $part = trim($part); 7744f669e9Sandi 7844f669e9Sandi // parse address 7944f669e9Sandi if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 8044f669e9Sandi $text = trim($matches[1]); 8144f669e9Sandi $addr = $matches[2]; 8244f669e9Sandi }else{ 8344f669e9Sandi $addr = $part; 8444f669e9Sandi } 8544f669e9Sandi 8644f669e9Sandi // skip empty ones 8744f669e9Sandi if(empty($addr)){ 8844f669e9Sandi continue; 8944f669e9Sandi } 9044f669e9Sandi 9144f669e9Sandi // FIXME: is there a way to encode the localpart of a emailaddress? 9244f669e9Sandi if(!utf8_isASCII($addr)){ 9344f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 9444f669e9Sandi continue; 9544f669e9Sandi } 9644f669e9Sandi 9744f669e9Sandi if(!mail_isvalid($addr)){ 9844f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 9944f669e9Sandi continue; 10044f669e9Sandi } 10144f669e9Sandi 102a2021ad8Sandi // text was given 103a2021ad8Sandi if(!empty($text)){ 104a2021ad8Sandi // add address quotes 105a2021ad8Sandi $addr = "<$addr>"; 10644f669e9Sandi 107e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 108e1906e6eSandi $text = utf8_deaccent($text); 109e1906e6eSandi $text = utf8_strip($text); 110e1906e6eSandi } 111e1906e6eSandi 11244f669e9Sandi if(!utf8_isASCII($text)){ 11344f669e9Sandi $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text).'?='; 11444f669e9Sandi } 11544f669e9Sandi } 11644f669e9Sandi 117a2021ad8Sandi // add to header comma seperated and in new line to avoid too long headers 118a2021ad8Sandi if($headers != '') $headers .= ','.MAILHEADER_EOL.' '; 119a2021ad8Sandi $headers .= $text.$addr; 120a2021ad8Sandi } 121a2021ad8Sandi 122a2021ad8Sandi if(empty($headers)) return null; 123a2021ad8Sandi 124a2021ad8Sandi //if headername was given add it and close correctly 125a2021ad8Sandi if($header) $headers = $header.': '.$headers.MAILHEADER_EOL; 126a2021ad8Sandi 12744f669e9Sandi return $headers; 12844f669e9Sandi} 12944f669e9Sandi 13044f669e9Sandi/** 13144f669e9Sandi * Uses a regular expresion to check if a given mail address is valid 13244f669e9Sandi * 13344f669e9Sandi * May not be completly RFC conform! 13444f669e9Sandi * 13544f669e9Sandi * @link http://www.webmasterworld.com/forum88/135.htm 13644f669e9Sandi * 13744f669e9Sandi * @param string $email the address to check 13844f669e9Sandi * @return bool true if address is valid 13944f669e9Sandi */ 14044f669e9Sandifunction mail_isvalid($email){ 14144f669e9Sandi return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 14244f669e9Sandi} 14344f669e9Sandi 14444f669e9Sandi/** 14544f669e9Sandi * Quoted printable encoding 14644f669e9Sandi * 14744f669e9Sandi * @author <pob@medienrecht.org> 14844f669e9Sandi * @author <tamas.tompa@kirowski.com> 14944f669e9Sandi * @link http://www.php.net/manual/en/function.quoted-printable-decode.php 15044f669e9Sandi */ 15144f669e9Sandifunction mail_quotedprintable_encode($input='',$line_max=74,$space_conv=false){ 15244f669e9Sandi $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); 15344f669e9Sandi $lines = preg_split("/(?:\r\n|\r|\n)/", $input); 15444f669e9Sandi $eol = "\n"; 15544f669e9Sandi $escape = "="; 15644f669e9Sandi $output = ""; 15744f669e9Sandi while( list(, $line) = each($lines) ) { 15844f669e9Sandi //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary 15944f669e9Sandi $linlen = strlen($line); 16044f669e9Sandi $newline = ""; 16144f669e9Sandi for($i = 0; $i < $linlen; $i++) { 16244f669e9Sandi $c = substr( $line, $i, 1 ); 16344f669e9Sandi $dec = ord( $c ); 16444f669e9Sandi if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E 16544f669e9Sandi $c = "=2E"; 16644f669e9Sandi } 16744f669e9Sandi if ( $dec == 32 ) { 16844f669e9Sandi if ( $i == ( $linlen - 1 ) ) { // convert space at eol only 16944f669e9Sandi $c = "=20"; 17044f669e9Sandi } else if ( $space_conv ) { 17144f669e9Sandi $c = "=20"; 17244f669e9Sandi } 17344f669e9Sandi } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required 17444f669e9Sandi $h2 = floor($dec/16); 17544f669e9Sandi $h1 = floor($dec%16); 17644f669e9Sandi $c = $escape.$hex["$h2"].$hex["$h1"]; 17744f669e9Sandi } 17844f669e9Sandi if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted 17944f669e9Sandi $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay 18044f669e9Sandi $newline = ""; 18144f669e9Sandi // check if newline first character will be point or not 18244f669e9Sandi if ( $dec == 46 ) { 18344f669e9Sandi $c = "=2E"; 18444f669e9Sandi } 18544f669e9Sandi } 18644f669e9Sandi $newline .= $c; 18744f669e9Sandi } // end of for 18844f669e9Sandi $output .= $newline.$eol; 18944f669e9Sandi } // end of while 19044f669e9Sandi return trim($output); 19144f669e9Sandi} 19244f669e9Sandi 19344f669e9Sandi 194340756e4Sandi 195340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 196