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 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 1044f669e9Sandi 1129f3a5faSAndreas Gohr// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?) 1229f3a5faSAndreas Gohr// think different 1329f3a5faSAndreas Gohrif(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n"); 14e1906e6eSandi#define('MAILHEADER_ASCIIONLY',1); 1544f669e9Sandi 1644f669e9Sandi/** 17d530a62aSAndreas Gohr * Patterns for use in email detection and validation 18d530a62aSAndreas Gohr * 19d530a62aSAndreas Gohr * NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser 20d530a62aSAndreas Gohr * the pattern uses non-capturing groups as captured groups aren't allowed in the parser 21d530a62aSAndreas Gohr * select pattern delimiters with care! 22d530a62aSAndreas Gohr * 23d530a62aSAndreas Gohr * May not be completly RFC conform! 24d530a62aSAndreas Gohr * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4) 25d530a62aSAndreas Gohr * 26d530a62aSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 27d530a62aSAndreas Gohr * Check if a given mail address is valid 28d530a62aSAndreas Gohr */ 29d530a62aSAndreas Gohrif (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-zA-Z!#$%&'*+/=?^_`{|}~-"); 30*8838b30bSRainer Rillkeif (!defined('PREG_PATTERN_VALID_EMAIL')) define('PREG_PATTERN_VALID_EMAIL', '['.RFC2822_ATEXT.']+(?:\.['.RFC2822_ATEXT.']+)*@(?i:[0-9a-z][0-9a-z-]*\.)+(?i:[a-z]{2,63})'); 31d530a62aSAndreas Gohr 325ec3fefcSAndreas Gohr/** 335ec3fefcSAndreas Gohr * Prepare mailfrom replacement patterns 345ec3fefcSAndreas Gohr * 35465e809bSAndreas Gohr * Also prepares a mailfromnobody config that contains an autoconstructed address 36f7cefc02SAndreas Gohr * if the mailfrom one is userdependent and this might not be wanted (subscriptions) 37f7cefc02SAndreas Gohr * 385ec3fefcSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 395ec3fefcSAndreas Gohr */ 405ec3fefcSAndreas Gohrfunction mail_setup(){ 415ec3fefcSAndreas Gohr global $conf; 42609c41e4SElan Ruusamäe global $USERINFO; 43585bf44eSChristopher Smith /** @var Input $INPUT */ 44585bf44eSChristopher Smith global $INPUT; 45d530a62aSAndreas Gohr 46f7cefc02SAndreas Gohr // auto constructed address 47f7cefc02SAndreas Gohr $host = @parse_url(DOKU_URL,PHP_URL_HOST); 48f7cefc02SAndreas Gohr if(!$host) $host = 'example.com'; 49f7cefc02SAndreas Gohr $noreply = 'noreply@'.$host; 505ec3fefcSAndreas Gohr 51f7cefc02SAndreas Gohr $replace = array(); 52609c41e4SElan Ruusamäe if(!empty($USERINFO['mail'])){ 53609c41e4SElan Ruusamäe $replace['@MAIL@'] = $USERINFO['mail']; 545ec3fefcSAndreas Gohr }else{ 55f7cefc02SAndreas Gohr $replace['@MAIL@'] = $noreply; 565ec3fefcSAndreas Gohr } 575ec3fefcSAndreas Gohr 58585bf44eSChristopher Smith // use 'noreply' if no user 59585bf44eSChristopher Smith $replace['@USER@'] = $INPUT->server->str('REMOTE_USER', 'noreply', true); 605ec3fefcSAndreas Gohr 61609c41e4SElan Ruusamäe if(!empty($USERINFO['name'])){ 62609c41e4SElan Ruusamäe $replace['@NAME@'] = $USERINFO['name']; 635ec3fefcSAndreas Gohr }else{ 645ec3fefcSAndreas Gohr $replace['@NAME@'] = ''; 655ec3fefcSAndreas Gohr } 665ec3fefcSAndreas Gohr 67f7cefc02SAndreas Gohr // apply replacements 68f7cefc02SAndreas Gohr $from = str_replace(array_keys($replace), 695ec3fefcSAndreas Gohr array_values($replace), 705ec3fefcSAndreas Gohr $conf['mailfrom']); 71f7cefc02SAndreas Gohr 72f7cefc02SAndreas Gohr // any replacements done? set different mailfromnone 73f7cefc02SAndreas Gohr if($from != $conf['mailfrom']){ 74465e809bSAndreas Gohr $conf['mailfromnobody'] = $noreply; 75f7cefc02SAndreas Gohr }else{ 76465e809bSAndreas Gohr $conf['mailfromnobody'] = $from; 77f7cefc02SAndreas Gohr } 78f7cefc02SAndreas Gohr $conf['mailfrom'] = $from; 795ec3fefcSAndreas Gohr} 80d530a62aSAndreas Gohr 81d530a62aSAndreas Gohr/** 8244f669e9Sandi * UTF-8 autoencoding replacement for PHPs mail function 8344f669e9Sandi * 8444f669e9Sandi * Email address fields (To, From, Cc, Bcc can contain a textpart and an address 8544f669e9Sandi * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded 8644f669e9Sandi * automatically. You can seperate receivers by commas. 8744f669e9Sandi * 8844f669e9Sandi * @param string $to Receiver of the mail (multiple seperated by commas) 8944f669e9Sandi * @param string $subject Mailsubject 9044f669e9Sandi * @param string $body Messagebody 9144f669e9Sandi * @param string $from Sender address 9244f669e9Sandi * @param string $cc CarbonCopy receiver (multiple seperated by commas) 9344f669e9Sandi * @param string $bcc BlindCarbonCopy receiver (multiple seperated by commas) 9444f669e9Sandi * @param string $headers Additional Headers (seperated by MAILHEADER_EOL 9544f669e9Sandi * @param string $params Additonal Sendmail params (passed to mail()) 9644f669e9Sandi * 9744f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org> 9844f669e9Sandi * @see mail() 9944f669e9Sandi */ 10044f669e9Sandifunction mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){ 101348e3c03SChris Smith 1021986aa9eSChris Smith $message = compact('to','subject','body','from','cc','bcc','headers','params'); 103348e3c03SChris Smith return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action'); 1041986aa9eSChris Smith} 1051986aa9eSChris Smith 106348e3c03SChris Smithfunction _mail_send_action($data) { 107348e3c03SChris Smith 108348e3c03SChris Smith // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params 109348e3c03SChris Smith $to = $data['to']; 110348e3c03SChris Smith $subject = $data['subject']; 111348e3c03SChris Smith $body = $data['body']; 112348e3c03SChris Smith 113348e3c03SChris Smith // add robustness in case plugin removes any of these optional values 114348e3c03SChris Smith $from = isset($data['from']) ? $data['from'] : ''; 115348e3c03SChris Smith $cc = isset($data['cc']) ? $data['cc'] : ''; 116348e3c03SChris Smith $bcc = isset($data['bcc']) ? $data['bcc'] : ''; 117348e3c03SChris Smith $headers = isset($data['headers']) ? $data['headers'] : null; 118348e3c03SChris Smith $params = isset($data['params']) ? $data['params'] : null; 119348e3c03SChris Smith 12096569d48SMatthias Schulte // discard mail request if no recipients are available 12196569d48SMatthias Schulte if(trim($to) === '' && trim($cc) === '' && trim($bcc) === '') return false; 12296569d48SMatthias Schulte 123348e3c03SChris Smith // end additional code to support event ... original mail_send() code from here 124348e3c03SChris Smith 125e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 126e1906e6eSandi $subject = utf8_deaccent($subject); 127e1906e6eSandi $subject = utf8_strip($subject); 128e1906e6eSandi } 129e1906e6eSandi 130265f02e3SGuy Brand if(!utf8_isASCII($subject)) { 1317e8e923fSAndreas Gohr $enc_subj = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?='; 132265f02e3SGuy Brand // Spaces must be encoded according to rfc2047. Use the "_" shorthand 133ddcd5ab6SKazutaka Miyasaka $enc_subj = preg_replace('/ /', '_', $enc_subj); 1347e8e923fSAndreas Gohr 1357e8e923fSAndreas Gohr // quoted printable has length restriction, use base64 if needed 1367e8e923fSAndreas Gohr if(strlen($subject) > 74){ 1377e8e923fSAndreas Gohr $enc_subj = '=?UTF-8?B?'.base64_encode($subject).'?='; 1387e8e923fSAndreas Gohr } 1397e8e923fSAndreas Gohr 1407e8e923fSAndreas Gohr $subject = $enc_subj; 141265f02e3SGuy Brand } 14244f669e9Sandi 14344f669e9Sandi $header = ''; 144e1906e6eSandi 145f95e691eSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 146f95e691eSAndreas Gohr $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 147f95e691eSAndreas Gohr 148f95e691eSAndreas Gohr $to = mail_encode_address($to,'',$usenames); 14944f669e9Sandi $header .= mail_encode_address($from,'From'); 15044f669e9Sandi $header .= mail_encode_address($cc,'Cc'); 15144f669e9Sandi $header .= mail_encode_address($bcc,'Bcc'); 15244f669e9Sandi $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL; 15344f669e9Sandi $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 15444f669e9Sandi $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL; 15544f669e9Sandi $header .= $headers; 156e1906e6eSandi $header = trim($header); 15744f669e9Sandi 15844f669e9Sandi $body = mail_quotedprintable_encode($body); 15944f669e9Sandi 160d7785cceSandi if($params == null){ 161d7785cceSandi return @mail($to,$subject,$body,$header); 162d7785cceSandi }else{ 163e1906e6eSandi return @mail($to,$subject,$body,$header,$params); 16444f669e9Sandi } 165d7785cceSandi} 16644f669e9Sandi 16744f669e9Sandi/** 16844f669e9Sandi * Encodes an email address header 16944f669e9Sandi * 170e1906e6eSandi * Unicode characters will be deaccented and encoded 171e1906e6eSandi * quoted_printable for headers. 17244f669e9Sandi * Addresses may not contain Non-ASCII data! 17344f669e9Sandi * 17444f669e9Sandi * Example: 175a2021ad8Sandi * mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc"); 17644f669e9Sandi * 177f95e691eSAndreas Gohr * @param string $string Multiple adresses separated by commas 178f95e691eSAndreas Gohr * @param string $header Name of the header (To,Bcc,Cc,...) 179f95e691eSAndreas Gohr * @param boolean $names Allow named Recipients? 18044f669e9Sandi */ 181f95e691eSAndreas Gohrfunction mail_encode_address($string,$header='',$names=true){ 18244f669e9Sandi $headers = ''; 1834b7f9e70STom N Harris $parts = explode(',',$string); 18444f669e9Sandi foreach ($parts as $part){ 18544f669e9Sandi $part = trim($part); 18644f669e9Sandi 18744f669e9Sandi // parse address 18844f669e9Sandi if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 18944f669e9Sandi $text = trim($matches[1]); 19044f669e9Sandi $addr = $matches[2]; 19144f669e9Sandi }else{ 19244f669e9Sandi $addr = $part; 19344f669e9Sandi } 19444f669e9Sandi 19544f669e9Sandi // skip empty ones 19644f669e9Sandi if(empty($addr)){ 19744f669e9Sandi continue; 19844f669e9Sandi } 19944f669e9Sandi 20044f669e9Sandi // FIXME: is there a way to encode the localpart of a emailaddress? 20144f669e9Sandi if(!utf8_isASCII($addr)){ 20244f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 20344f669e9Sandi continue; 20444f669e9Sandi } 20544f669e9Sandi 20644f669e9Sandi if(!mail_isvalid($addr)){ 20744f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 20844f669e9Sandi continue; 20944f669e9Sandi } 21044f669e9Sandi 211a2021ad8Sandi // text was given 212f95e691eSAndreas Gohr if(!empty($text) && $names){ 213a2021ad8Sandi // add address quotes 214a2021ad8Sandi $addr = "<$addr>"; 21544f669e9Sandi 216e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 217e1906e6eSandi $text = utf8_deaccent($text); 218e1906e6eSandi $text = utf8_strip($text); 219e1906e6eSandi } 220e1906e6eSandi 22144f669e9Sandi if(!utf8_isASCII($text)){ 2220667123fSElan Ruusamäe // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" 2230667123fSElan Ruusamäe if (preg_match('/^"(.+)"$/', $text, $matches)) { 2240667123fSElan Ruusamäe $text = '"=?UTF-8?Q?'.mail_quotedprintable_encode($matches[1], 0).'?="'; 2250667123fSElan Ruusamäe } else { 22691275a65SAndreas Gohr $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text, 0).'?='; 22744f669e9Sandi } 2280667123fSElan Ruusamäe // additionally the space character should be encoded as =20 (or each 2290667123fSElan Ruusamäe // word QP encoded separately). 2300667123fSElan Ruusamäe // however this is needed only in mail headers, not globally in mail_quotedprintable_encode(). 2310667123fSElan Ruusamäe $text = str_replace(" ", "=20", $text); 2320667123fSElan Ruusamäe } 233628e6ba7SAndreas Gohr }else{ 234628e6ba7SAndreas Gohr $text = ''; 23544f669e9Sandi } 23644f669e9Sandi 2372300b3e6SAndreas Gohr // add to header comma seperated 2382300b3e6SAndreas Gohr if($headers != ''){ 2392300b3e6SAndreas Gohr $headers .= ','; 2402300b3e6SAndreas Gohr if($header) $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers 2412300b3e6SAndreas Gohr } 242ba36e50eSAndreas Gohr $headers .= $text.' '.$addr; 243a2021ad8Sandi } 244a2021ad8Sandi 245a2021ad8Sandi if(empty($headers)) return null; 246a2021ad8Sandi 247a2021ad8Sandi //if headername was given add it and close correctly 248a2021ad8Sandi if($header) $headers = $header.': '.$headers.MAILHEADER_EOL; 249a2021ad8Sandi 25044f669e9Sandi return $headers; 25144f669e9Sandi} 25244f669e9Sandi 25344f669e9Sandi/** 254e8f8d645SAndreas Gohr * Check if a given mail address is valid 25544f669e9Sandi * 25644f669e9Sandi * @param string $email the address to check 25744f669e9Sandi * @return bool true if address is valid 25844f669e9Sandi */ 25944f669e9Sandifunction mail_isvalid($email){ 260e8f8d645SAndreas Gohr $validator = new EmailAddressValidator; 26102700828SAndreas Gohr $validator->allowLocalAddresses = true; 262e8f8d645SAndreas Gohr return $validator->check_email_address($email); 26344f669e9Sandi} 26444f669e9Sandi 26544f669e9Sandi/** 26644f669e9Sandi * Quoted printable encoding 26744f669e9Sandi * 26891275a65SAndreas Gohr * @author umu <umuAThrz.tu-chemnitz.de> 26991275a65SAndreas Gohr * @link http://www.php.net/manual/en/function.imap-8bit.php#61216 27044f669e9Sandi */ 27191275a65SAndreas Gohrfunction mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) { 27291275a65SAndreas Gohr // split text into lines 27391275a65SAndreas Gohr $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText); 274db959ae3SAndreas Gohr $cnt = count($aLines); 27591275a65SAndreas Gohr 276db959ae3SAndreas Gohr for ($i=0;$i<$cnt;$i++) { 27791275a65SAndreas Gohr $sLine =& $aLines[$i]; 27891275a65SAndreas Gohr if (strlen($sLine)===0) continue; // do nothing, if empty 27991275a65SAndreas Gohr 28091275a65SAndreas Gohr $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e'; 28191275a65SAndreas Gohr 28291275a65SAndreas Gohr // imap_8bit encodes x09 everywhere, not only at lineends, 28391275a65SAndreas Gohr // for EBCDIC safeness encode !"#$@[\]^`{|}~, 28491275a65SAndreas Gohr // for complete safeness encode every character :) 28591275a65SAndreas Gohr if ($bEmulate_imap_8bit) 2869c107bd1SChristopher Smith $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/'; 28791275a65SAndreas Gohr 2889c107bd1SChristopher Smith $sLine = preg_replace_callback( $sRegExp, 'mail_quotedprintable_encode_callback', $sLine ); 28991275a65SAndreas Gohr 29091275a65SAndreas Gohr // encode x09,x20 at lineends 29191275a65SAndreas Gohr { 29291275a65SAndreas Gohr $iLength = strlen($sLine); 29391275a65SAndreas Gohr $iLastChar = ord($sLine{$iLength-1}); 29491275a65SAndreas Gohr 29591275a65SAndreas Gohr // !!!!!!!! 29691275a65SAndreas Gohr // imap_8_bit does not encode x20 at the very end of a text, 29791275a65SAndreas Gohr // here is, where I don't agree with imap_8_bit, 29891275a65SAndreas Gohr // please correct me, if I'm wrong, 29991275a65SAndreas Gohr // or comment next line for RFC2045 conformance, if you like 300db959ae3SAndreas Gohr if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))){ 30191275a65SAndreas Gohr if (($iLastChar==0x09)||($iLastChar==0x20)) { 30291275a65SAndreas Gohr $sLine{$iLength-1}='='; 30391275a65SAndreas Gohr $sLine .= ($iLastChar==0x09)?'09':'20'; 30444f669e9Sandi } 305db959ae3SAndreas Gohr } 30691275a65SAndreas Gohr } // imap_8bit encodes x20 before chr(13), too 30791275a65SAndreas Gohr // although IMHO not requested by RFC2045, why not do it safer :) 30891275a65SAndreas Gohr // and why not encode any x20 around chr(10) or chr(13) 30991275a65SAndreas Gohr if ($bEmulate_imap_8bit) { 31091275a65SAndreas Gohr $sLine=str_replace(' =0D','=20=0D',$sLine); 31191275a65SAndreas Gohr //$sLine=str_replace(' =0A','=20=0A',$sLine); 31291275a65SAndreas Gohr //$sLine=str_replace('=0D ','=0D=20',$sLine); 31391275a65SAndreas Gohr //$sLine=str_replace('=0A ','=0A=20',$sLine); 31444f669e9Sandi } 31544f669e9Sandi 31691275a65SAndreas Gohr // finally split into softlines no longer than $maxlen chars, 31791275a65SAndreas Gohr // for even more safeness one could encode x09,x20 31891275a65SAndreas Gohr // at the very first character of the line 31991275a65SAndreas Gohr // and after soft linebreaks, as well, 32091275a65SAndreas Gohr // but this wouldn't be caught by such an easy RegExp 32191275a65SAndreas Gohr if($maxlen){ 32291275a65SAndreas Gohr preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch ); 3231a6a1c04SAndreas Gohr $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's 32491275a65SAndreas Gohr } 32591275a65SAndreas Gohr } 32691275a65SAndreas Gohr 32791275a65SAndreas Gohr // join lines into text 3281a6a1c04SAndreas Gohr return implode(MAILHEADER_EOL,$aLines); 32991275a65SAndreas Gohr} 33044f669e9Sandi 3319c107bd1SChristopher Smithfunction mail_quotedprintable_encode_callback($matches){ 3329c107bd1SChristopher Smith return sprintf( "=%02X", ord ( $matches[0] ) ) ; 3339c107bd1SChristopher Smith} 334