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 900976812SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 10ed7b5f09Sandi require_once(DOKU_INC.'inc/utf8.php'); 1144f669e9Sandi 1229f3a5faSAndreas Gohr // end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?) 1329f3a5faSAndreas Gohr // think different 1429f3a5faSAndreas Gohr if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n"); 15e1906e6eSandi #define('MAILHEADER_ASCIIONLY',1); 1644f669e9Sandi 1744f669e9Sandi/** 1844f669e9Sandi * UTF-8 autoencoding replacement for PHPs mail function 1944f669e9Sandi * 2044f669e9Sandi * Email address fields (To, From, Cc, Bcc can contain a textpart and an address 2144f669e9Sandi * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded 2244f669e9Sandi * automatically. You can seperate receivers by commas. 2344f669e9Sandi * 2444f669e9Sandi * @param string $to Receiver of the mail (multiple seperated by commas) 2544f669e9Sandi * @param string $subject Mailsubject 2644f669e9Sandi * @param string $body Messagebody 2744f669e9Sandi * @param string $from Sender address 2844f669e9Sandi * @param string $cc CarbonCopy receiver (multiple seperated by commas) 2944f669e9Sandi * @param string $bcc BlindCarbonCopy receiver (multiple seperated by commas) 3044f669e9Sandi * @param string $headers Additional Headers (seperated by MAILHEADER_EOL 3144f669e9Sandi * @param string $params Additonal Sendmail params (passed to mail()) 3244f669e9Sandi * 3344f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org> 3444f669e9Sandi * @see mail() 3544f669e9Sandi */ 3644f669e9Sandifunction mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){ 37*348e3c03SChris Smith 381986aa9eSChris Smith $message = compact('to','subject','body','from','cc','bcc','headers','params'); 39*348e3c03SChris Smith return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action'); 401986aa9eSChris Smith} 411986aa9eSChris Smith 42*348e3c03SChris Smithfunction _mail_send_action($data) { 43*348e3c03SChris Smith 44*348e3c03SChris Smith // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params 45*348e3c03SChris Smith $to = $data['to']; 46*348e3c03SChris Smith $subject = $data['subject']; 47*348e3c03SChris Smith $body = $data['body']; 48*348e3c03SChris Smith 49*348e3c03SChris Smith // add robustness in case plugin removes any of these optional values 50*348e3c03SChris Smith $from = isset($data['from']) ? $data['from'] : ''; 51*348e3c03SChris Smith $cc = isset($data['cc']) ? $data['cc'] : ''; 52*348e3c03SChris Smith $bcc = isset($data['bcc']) ? $data['bcc'] : ''; 53*348e3c03SChris Smith $headers = isset($data['headers']) ? $data['headers'] : null; 54*348e3c03SChris Smith $params = isset($data['params']) ? $data['params'] : null; 55*348e3c03SChris Smith 56*348e3c03SChris Smith // end additional code to support event ... original mail_send() code from here 57*348e3c03SChris Smith 58e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 59e1906e6eSandi $subject = utf8_deaccent($subject); 60e1906e6eSandi $subject = utf8_strip($subject); 61e1906e6eSandi } 62e1906e6eSandi 63265f02e3SGuy Brand if(!utf8_isASCII($subject)) { 6491275a65SAndreas Gohr $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?='; 65265f02e3SGuy Brand // Spaces must be encoded according to rfc2047. Use the "_" shorthand 66265f02e3SGuy Brand $subject = preg_replace('/ /', '_', $subject); 67265f02e3SGuy Brand } 6844f669e9Sandi 6944f669e9Sandi $header = ''; 70e1906e6eSandi 71f95e691eSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 72f95e691eSAndreas Gohr $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 73f95e691eSAndreas Gohr 74f95e691eSAndreas Gohr $to = mail_encode_address($to,'',$usenames); 7544f669e9Sandi $header .= mail_encode_address($from,'From'); 7644f669e9Sandi $header .= mail_encode_address($cc,'Cc'); 7744f669e9Sandi $header .= mail_encode_address($bcc,'Bcc'); 7844f669e9Sandi $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL; 7944f669e9Sandi $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 8044f669e9Sandi $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL; 8144f669e9Sandi $header .= $headers; 82e1906e6eSandi $header = trim($header); 8344f669e9Sandi 8444f669e9Sandi $body = mail_quotedprintable_encode($body); 8544f669e9Sandi 86d7785cceSandi if($params == null){ 87d7785cceSandi return @mail($to,$subject,$body,$header); 88d7785cceSandi }else{ 89e1906e6eSandi return @mail($to,$subject,$body,$header,$params); 9044f669e9Sandi } 91d7785cceSandi} 9244f669e9Sandi 9344f669e9Sandi/** 9444f669e9Sandi * Encodes an email address header 9544f669e9Sandi * 96e1906e6eSandi * Unicode characters will be deaccented and encoded 97e1906e6eSandi * quoted_printable for headers. 9844f669e9Sandi * Addresses may not contain Non-ASCII data! 9944f669e9Sandi * 10044f669e9Sandi * Example: 101a2021ad8Sandi * mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc"); 10244f669e9Sandi * 103f95e691eSAndreas Gohr * @param string $string Multiple adresses separated by commas 104f95e691eSAndreas Gohr * @param string $header Name of the header (To,Bcc,Cc,...) 105f95e691eSAndreas Gohr * @param boolean $names Allow named Recipients? 10644f669e9Sandi */ 107f95e691eSAndreas Gohrfunction mail_encode_address($string,$header='',$names=true){ 10844f669e9Sandi $headers = ''; 10944f669e9Sandi $parts = split(',',$string); 11044f669e9Sandi foreach ($parts as $part){ 11144f669e9Sandi $part = trim($part); 11244f669e9Sandi 11344f669e9Sandi // parse address 11444f669e9Sandi if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 11544f669e9Sandi $text = trim($matches[1]); 11644f669e9Sandi $addr = $matches[2]; 11744f669e9Sandi }else{ 11844f669e9Sandi $addr = $part; 11944f669e9Sandi } 12044f669e9Sandi 12144f669e9Sandi // skip empty ones 12244f669e9Sandi if(empty($addr)){ 12344f669e9Sandi continue; 12444f669e9Sandi } 12544f669e9Sandi 12644f669e9Sandi // FIXME: is there a way to encode the localpart of a emailaddress? 12744f669e9Sandi if(!utf8_isASCII($addr)){ 12844f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 12944f669e9Sandi continue; 13044f669e9Sandi } 13144f669e9Sandi 13244f669e9Sandi if(!mail_isvalid($addr)){ 13344f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 13444f669e9Sandi continue; 13544f669e9Sandi } 13644f669e9Sandi 137a2021ad8Sandi // text was given 138f95e691eSAndreas Gohr if(!empty($text) && $names){ 139a2021ad8Sandi // add address quotes 140a2021ad8Sandi $addr = "<$addr>"; 14144f669e9Sandi 142e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 143e1906e6eSandi $text = utf8_deaccent($text); 144e1906e6eSandi $text = utf8_strip($text); 145e1906e6eSandi } 146e1906e6eSandi 14744f669e9Sandi if(!utf8_isASCII($text)){ 14891275a65SAndreas Gohr $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text,0).'?='; 14944f669e9Sandi } 150628e6ba7SAndreas Gohr }else{ 151628e6ba7SAndreas Gohr $text = ''; 15244f669e9Sandi } 15344f669e9Sandi 154a2021ad8Sandi // add to header comma seperated and in new line to avoid too long headers 155a2021ad8Sandi if($headers != '') $headers .= ','.MAILHEADER_EOL.' '; 156ba36e50eSAndreas Gohr $headers .= $text.' '.$addr; 157a2021ad8Sandi } 158a2021ad8Sandi 159a2021ad8Sandi if(empty($headers)) return null; 160a2021ad8Sandi 161a2021ad8Sandi //if headername was given add it and close correctly 162a2021ad8Sandi if($header) $headers = $header.': '.$headers.MAILHEADER_EOL; 163a2021ad8Sandi 16444f669e9Sandi return $headers; 16544f669e9Sandi} 16644f669e9Sandi 16744f669e9Sandi/** 16844f669e9Sandi * Uses a regular expresion to check if a given mail address is valid 16944f669e9Sandi * 17044f669e9Sandi * May not be completly RFC conform! 1710a1d30bfSchris * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4) 17244f669e9Sandi * 1730a1d30bfSchris * @author Chris Smith <chris@jalakai.co.uk> 17444f669e9Sandi * 17544f669e9Sandi * @param string $email the address to check 17644f669e9Sandi * @return bool true if address is valid 17744f669e9Sandi */ 1780a1d30bfSchris 1790a1d30bfSchris// patterns for use in email detection and validation 1800a1d30bfSchris// NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser 1810a1d30bfSchris// the pattern uses non-capturing groups as captured groups aren't allowed in the parser 1820a1d30bfSchris// select pattern delimiters with care! 183591d30adSChris Smithif (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-z!#$%&'*+/=?^_`{|}~-"); 184591d30adSChris Smithif (!defined('PREG_PATTERN_VALID_EMAIL')) define('PREG_PATTERN_VALID_EMAIL', '['.RFC2822_ATEXT.']+(?:\.['.RFC2822_ATEXT.']+)*@(?:[0-9a-z][0-9a-z-]*\.)+(?:[a-z]{2,4}|museum|travel)'); 1850a1d30bfSchris 18644f669e9Sandifunction mail_isvalid($email){ 187591d30adSChris Smith return preg_match('<^'.PREG_PATTERN_VALID_EMAIL.'$>i', $email); 18844f669e9Sandi} 18944f669e9Sandi 19044f669e9Sandi/** 19144f669e9Sandi * Quoted printable encoding 19244f669e9Sandi * 19391275a65SAndreas Gohr * @author umu <umuAThrz.tu-chemnitz.de> 19491275a65SAndreas Gohr * @link http://www.php.net/manual/en/function.imap-8bit.php#61216 19544f669e9Sandi */ 19691275a65SAndreas Gohrfunction mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) { 19791275a65SAndreas Gohr // split text into lines 19891275a65SAndreas Gohr $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText); 19991275a65SAndreas Gohr 20091275a65SAndreas Gohr for ($i=0;$i<count($aLines);$i++) { 20191275a65SAndreas Gohr $sLine =& $aLines[$i]; 20291275a65SAndreas Gohr if (strlen($sLine)===0) continue; // do nothing, if empty 20391275a65SAndreas Gohr 20491275a65SAndreas Gohr $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e'; 20591275a65SAndreas Gohr 20691275a65SAndreas Gohr // imap_8bit encodes x09 everywhere, not only at lineends, 20791275a65SAndreas Gohr // for EBCDIC safeness encode !"#$@[\]^`{|}~, 20891275a65SAndreas Gohr // for complete safeness encode every character :) 20991275a65SAndreas Gohr if ($bEmulate_imap_8bit) 21091275a65SAndreas Gohr $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e'; 21191275a65SAndreas Gohr 21291275a65SAndreas Gohr $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;'; 21391275a65SAndreas Gohr $sLine = preg_replace( $sRegExp, $sReplmt, $sLine ); 21491275a65SAndreas Gohr 21591275a65SAndreas Gohr // encode x09,x20 at lineends 21691275a65SAndreas Gohr { 21791275a65SAndreas Gohr $iLength = strlen($sLine); 21891275a65SAndreas Gohr $iLastChar = ord($sLine{$iLength-1}); 21991275a65SAndreas Gohr 22091275a65SAndreas Gohr // !!!!!!!! 22191275a65SAndreas Gohr // imap_8_bit does not encode x20 at the very end of a text, 22291275a65SAndreas Gohr // here is, where I don't agree with imap_8_bit, 22391275a65SAndreas Gohr // please correct me, if I'm wrong, 22491275a65SAndreas Gohr // or comment next line for RFC2045 conformance, if you like 22591275a65SAndreas Gohr if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))) 22691275a65SAndreas Gohr 22791275a65SAndreas Gohr if (($iLastChar==0x09)||($iLastChar==0x20)) { 22891275a65SAndreas Gohr $sLine{$iLength-1}='='; 22991275a65SAndreas Gohr $sLine .= ($iLastChar==0x09)?'09':'20'; 23044f669e9Sandi } 23191275a65SAndreas Gohr } // imap_8bit encodes x20 before chr(13), too 23291275a65SAndreas Gohr // although IMHO not requested by RFC2045, why not do it safer :) 23391275a65SAndreas Gohr // and why not encode any x20 around chr(10) or chr(13) 23491275a65SAndreas Gohr if ($bEmulate_imap_8bit) { 23591275a65SAndreas Gohr $sLine=str_replace(' =0D','=20=0D',$sLine); 23691275a65SAndreas Gohr //$sLine=str_replace(' =0A','=20=0A',$sLine); 23791275a65SAndreas Gohr //$sLine=str_replace('=0D ','=0D=20',$sLine); 23891275a65SAndreas Gohr //$sLine=str_replace('=0A ','=0A=20',$sLine); 23944f669e9Sandi } 24044f669e9Sandi 24191275a65SAndreas Gohr // finally split into softlines no longer than $maxlen chars, 24291275a65SAndreas Gohr // for even more safeness one could encode x09,x20 24391275a65SAndreas Gohr // at the very first character of the line 24491275a65SAndreas Gohr // and after soft linebreaks, as well, 24591275a65SAndreas Gohr // but this wouldn't be caught by such an easy RegExp 24691275a65SAndreas Gohr if($maxlen){ 24791275a65SAndreas Gohr preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch ); 24829f3a5faSAndreas Gohr $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's 24991275a65SAndreas Gohr } 25091275a65SAndreas Gohr } 25191275a65SAndreas Gohr 25291275a65SAndreas Gohr // join lines into text 25329f3a5faSAndreas Gohr return implode(MAILHEADER_EOL,$aLines); 25491275a65SAndreas Gohr} 25544f669e9Sandi 256340756e4Sandi 257340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 258