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"); 142ae68f97SAndreas Gohrif(!defined('QUOTEDPRINTABLE_EOL')) define('QUOTEDPRINTABLE_EOL',"\015\012"); 15e1906e6eSandi#define('MAILHEADER_ASCIIONLY',1); 1644f669e9Sandi 1744f669e9Sandi/** 18d530a62aSAndreas Gohr * Patterns for use in email detection and validation 19d530a62aSAndreas Gohr * 20d530a62aSAndreas Gohr * NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser 21d530a62aSAndreas Gohr * the pattern uses non-capturing groups as captured groups aren't allowed in the parser 22d530a62aSAndreas Gohr * select pattern delimiters with care! 23d530a62aSAndreas Gohr * 24d530a62aSAndreas Gohr * May not be completly RFC conform! 25d530a62aSAndreas Gohr * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4) 26d530a62aSAndreas Gohr * 27d530a62aSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 28d530a62aSAndreas Gohr * Check if a given mail address is valid 29d530a62aSAndreas Gohr */ 30d530a62aSAndreas Gohrif (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-zA-Z!#$%&'*+/=?^_`{|}~-"); 31d530a62aSAndreas Gohrif (!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,4}|museum|travel)'); 32d530a62aSAndreas Gohr 33*5ec3fefcSAndreas Gohr/** 34*5ec3fefcSAndreas Gohr * Prepare mailfrom replacement patterns 35*5ec3fefcSAndreas Gohr * 36*5ec3fefcSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 37*5ec3fefcSAndreas Gohr */ 38*5ec3fefcSAndreas Gohrfunction mail_setup(){ 39*5ec3fefcSAndreas Gohr global $conf; 40*5ec3fefcSAndreas Gohr global $INFO; 41d530a62aSAndreas Gohr 42*5ec3fefcSAndreas Gohr $replace = array(); 43*5ec3fefcSAndreas Gohr 44*5ec3fefcSAndreas Gohr if(!empty($INFO['userinfo']['mail'])){ 45*5ec3fefcSAndreas Gohr $replace['@MAIL@'] = $INFO['userinfo']['mail']; 46*5ec3fefcSAndreas Gohr }else{ 47*5ec3fefcSAndreas Gohr $replace['@MAIL@'] = 'noreply@'.parse_url(DOKU_URL,PHP_URL_HOST); 48*5ec3fefcSAndreas Gohr } 49*5ec3fefcSAndreas Gohr 50*5ec3fefcSAndreas Gohr if(!empty($_SERVER['REMOTE_USER'])){ 51*5ec3fefcSAndreas Gohr $replace['@USER@'] = $_SERVER['REMOTE_USER']; 52*5ec3fefcSAndreas Gohr }else{ 53*5ec3fefcSAndreas Gohr $replace['@USER@'] = 'noreply'; 54*5ec3fefcSAndreas Gohr } 55*5ec3fefcSAndreas Gohr 56*5ec3fefcSAndreas Gohr if(!empty($INFO['userinfo']['name'])){ 57*5ec3fefcSAndreas Gohr $replace['@NAME@'] = $INFO['userinfo']['name']; 58*5ec3fefcSAndreas Gohr }else{ 59*5ec3fefcSAndreas Gohr $replace['@NAME@'] = ''; 60*5ec3fefcSAndreas Gohr } 61*5ec3fefcSAndreas Gohr 62*5ec3fefcSAndreas Gohr $conf['mailfrom'] = str_replace(array_keys($replace), 63*5ec3fefcSAndreas Gohr array_values($replace), 64*5ec3fefcSAndreas Gohr $conf['mailfrom']); 65*5ec3fefcSAndreas Gohr} 66d530a62aSAndreas Gohr 67d530a62aSAndreas Gohr/** 6844f669e9Sandi * UTF-8 autoencoding replacement for PHPs mail function 6944f669e9Sandi * 7044f669e9Sandi * Email address fields (To, From, Cc, Bcc can contain a textpart and an address 7144f669e9Sandi * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded 7244f669e9Sandi * automatically. You can seperate receivers by commas. 7344f669e9Sandi * 7444f669e9Sandi * @param string $to Receiver of the mail (multiple seperated by commas) 7544f669e9Sandi * @param string $subject Mailsubject 7644f669e9Sandi * @param string $body Messagebody 7744f669e9Sandi * @param string $from Sender address 7844f669e9Sandi * @param string $cc CarbonCopy receiver (multiple seperated by commas) 7944f669e9Sandi * @param string $bcc BlindCarbonCopy receiver (multiple seperated by commas) 8044f669e9Sandi * @param string $headers Additional Headers (seperated by MAILHEADER_EOL 8144f669e9Sandi * @param string $params Additonal Sendmail params (passed to mail()) 8244f669e9Sandi * 8344f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org> 8444f669e9Sandi * @see mail() 8544f669e9Sandi */ 8644f669e9Sandifunction mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){ 87348e3c03SChris Smith 881986aa9eSChris Smith $message = compact('to','subject','body','from','cc','bcc','headers','params'); 89348e3c03SChris Smith return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action'); 901986aa9eSChris Smith} 911986aa9eSChris Smith 92348e3c03SChris Smithfunction _mail_send_action($data) { 93348e3c03SChris Smith 94348e3c03SChris Smith // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params 95348e3c03SChris Smith $to = $data['to']; 96348e3c03SChris Smith $subject = $data['subject']; 97348e3c03SChris Smith $body = $data['body']; 98348e3c03SChris Smith 99348e3c03SChris Smith // add robustness in case plugin removes any of these optional values 100348e3c03SChris Smith $from = isset($data['from']) ? $data['from'] : ''; 101348e3c03SChris Smith $cc = isset($data['cc']) ? $data['cc'] : ''; 102348e3c03SChris Smith $bcc = isset($data['bcc']) ? $data['bcc'] : ''; 103348e3c03SChris Smith $headers = isset($data['headers']) ? $data['headers'] : null; 104348e3c03SChris Smith $params = isset($data['params']) ? $data['params'] : null; 105348e3c03SChris Smith 106348e3c03SChris Smith // end additional code to support event ... original mail_send() code from here 107348e3c03SChris Smith 108e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 109e1906e6eSandi $subject = utf8_deaccent($subject); 110e1906e6eSandi $subject = utf8_strip($subject); 111e1906e6eSandi } 112e1906e6eSandi 113265f02e3SGuy Brand if(!utf8_isASCII($subject)) { 11491275a65SAndreas Gohr $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?='; 115265f02e3SGuy Brand // Spaces must be encoded according to rfc2047. Use the "_" shorthand 116265f02e3SGuy Brand $subject = preg_replace('/ /', '_', $subject); 117265f02e3SGuy Brand } 11844f669e9Sandi 11944f669e9Sandi $header = ''; 120e1906e6eSandi 121f95e691eSAndreas Gohr // No named recipients for To: in Windows (see FS#652) 122f95e691eSAndreas Gohr $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true; 123f95e691eSAndreas Gohr 124f95e691eSAndreas Gohr $to = mail_encode_address($to,'',$usenames); 12544f669e9Sandi $header .= mail_encode_address($from,'From'); 12644f669e9Sandi $header .= mail_encode_address($cc,'Cc'); 12744f669e9Sandi $header .= mail_encode_address($bcc,'Bcc'); 12844f669e9Sandi $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL; 12944f669e9Sandi $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL; 13044f669e9Sandi $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL; 13144f669e9Sandi $header .= $headers; 132e1906e6eSandi $header = trim($header); 13344f669e9Sandi 13444f669e9Sandi $body = mail_quotedprintable_encode($body); 13544f669e9Sandi 136d7785cceSandi if($params == null){ 137d7785cceSandi return @mail($to,$subject,$body,$header); 138d7785cceSandi }else{ 139e1906e6eSandi return @mail($to,$subject,$body,$header,$params); 14044f669e9Sandi } 141d7785cceSandi} 14244f669e9Sandi 14344f669e9Sandi/** 14444f669e9Sandi * Encodes an email address header 14544f669e9Sandi * 146e1906e6eSandi * Unicode characters will be deaccented and encoded 147e1906e6eSandi * quoted_printable for headers. 14844f669e9Sandi * Addresses may not contain Non-ASCII data! 14944f669e9Sandi * 15044f669e9Sandi * Example: 151a2021ad8Sandi * mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc"); 15244f669e9Sandi * 153f95e691eSAndreas Gohr * @param string $string Multiple adresses separated by commas 154f95e691eSAndreas Gohr * @param string $header Name of the header (To,Bcc,Cc,...) 155f95e691eSAndreas Gohr * @param boolean $names Allow named Recipients? 15644f669e9Sandi */ 157f95e691eSAndreas Gohrfunction mail_encode_address($string,$header='',$names=true){ 15844f669e9Sandi $headers = ''; 1594b7f9e70STom N Harris $parts = explode(',',$string); 16044f669e9Sandi foreach ($parts as $part){ 16144f669e9Sandi $part = trim($part); 16244f669e9Sandi 16344f669e9Sandi // parse address 16444f669e9Sandi if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){ 16544f669e9Sandi $text = trim($matches[1]); 16644f669e9Sandi $addr = $matches[2]; 16744f669e9Sandi }else{ 16844f669e9Sandi $addr = $part; 16944f669e9Sandi } 17044f669e9Sandi 17144f669e9Sandi // skip empty ones 17244f669e9Sandi if(empty($addr)){ 17344f669e9Sandi continue; 17444f669e9Sandi } 17544f669e9Sandi 17644f669e9Sandi // FIXME: is there a way to encode the localpart of a emailaddress? 17744f669e9Sandi if(!utf8_isASCII($addr)){ 17844f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1); 17944f669e9Sandi continue; 18044f669e9Sandi } 18144f669e9Sandi 18244f669e9Sandi if(!mail_isvalid($addr)){ 18344f669e9Sandi msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1); 18444f669e9Sandi continue; 18544f669e9Sandi } 18644f669e9Sandi 187a2021ad8Sandi // text was given 188f95e691eSAndreas Gohr if(!empty($text) && $names){ 189a2021ad8Sandi // add address quotes 190a2021ad8Sandi $addr = "<$addr>"; 19144f669e9Sandi 192e1906e6eSandi if(defined('MAILHEADER_ASCIIONLY')){ 193e1906e6eSandi $text = utf8_deaccent($text); 194e1906e6eSandi $text = utf8_strip($text); 195e1906e6eSandi } 196e1906e6eSandi 19744f669e9Sandi if(!utf8_isASCII($text)){ 19891275a65SAndreas Gohr $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text,0).'?='; 19944f669e9Sandi } 200628e6ba7SAndreas Gohr }else{ 201628e6ba7SAndreas Gohr $text = ''; 20244f669e9Sandi } 20344f669e9Sandi 2042300b3e6SAndreas Gohr // add to header comma seperated 2052300b3e6SAndreas Gohr if($headers != ''){ 2062300b3e6SAndreas Gohr $headers .= ','; 2072300b3e6SAndreas Gohr if($header) $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers 2082300b3e6SAndreas Gohr } 209ba36e50eSAndreas Gohr $headers .= $text.' '.$addr; 210a2021ad8Sandi } 211a2021ad8Sandi 212a2021ad8Sandi if(empty($headers)) return null; 213a2021ad8Sandi 214a2021ad8Sandi //if headername was given add it and close correctly 215a2021ad8Sandi if($header) $headers = $header.': '.$headers.MAILHEADER_EOL; 216a2021ad8Sandi 21744f669e9Sandi return $headers; 21844f669e9Sandi} 21944f669e9Sandi 22044f669e9Sandi/** 221e8f8d645SAndreas Gohr * Check if a given mail address is valid 22244f669e9Sandi * 22344f669e9Sandi * @param string $email the address to check 22444f669e9Sandi * @return bool true if address is valid 22544f669e9Sandi */ 22644f669e9Sandifunction mail_isvalid($email){ 227e8f8d645SAndreas Gohr $validator = new EmailAddressValidator; 228e8f8d645SAndreas Gohr return $validator->check_email_address($email); 22944f669e9Sandi} 23044f669e9Sandi 23144f669e9Sandi/** 23244f669e9Sandi * Quoted printable encoding 23344f669e9Sandi * 23491275a65SAndreas Gohr * @author umu <umuAThrz.tu-chemnitz.de> 23591275a65SAndreas Gohr * @link http://www.php.net/manual/en/function.imap-8bit.php#61216 23644f669e9Sandi */ 23791275a65SAndreas Gohrfunction mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) { 23891275a65SAndreas Gohr // split text into lines 23991275a65SAndreas Gohr $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText); 240db959ae3SAndreas Gohr $cnt = count($aLines); 24191275a65SAndreas Gohr 242db959ae3SAndreas Gohr for ($i=0;$i<$cnt;$i++) { 24391275a65SAndreas Gohr $sLine =& $aLines[$i]; 24491275a65SAndreas Gohr if (strlen($sLine)===0) continue; // do nothing, if empty 24591275a65SAndreas Gohr 24691275a65SAndreas Gohr $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e'; 24791275a65SAndreas Gohr 24891275a65SAndreas Gohr // imap_8bit encodes x09 everywhere, not only at lineends, 24991275a65SAndreas Gohr // for EBCDIC safeness encode !"#$@[\]^`{|}~, 25091275a65SAndreas Gohr // for complete safeness encode every character :) 25191275a65SAndreas Gohr if ($bEmulate_imap_8bit) 25291275a65SAndreas Gohr $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e'; 25391275a65SAndreas Gohr 25491275a65SAndreas Gohr $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;'; 25591275a65SAndreas Gohr $sLine = preg_replace( $sRegExp, $sReplmt, $sLine ); 25691275a65SAndreas Gohr 25791275a65SAndreas Gohr // encode x09,x20 at lineends 25891275a65SAndreas Gohr { 25991275a65SAndreas Gohr $iLength = strlen($sLine); 26091275a65SAndreas Gohr $iLastChar = ord($sLine{$iLength-1}); 26191275a65SAndreas Gohr 26291275a65SAndreas Gohr // !!!!!!!! 26391275a65SAndreas Gohr // imap_8_bit does not encode x20 at the very end of a text, 26491275a65SAndreas Gohr // here is, where I don't agree with imap_8_bit, 26591275a65SAndreas Gohr // please correct me, if I'm wrong, 26691275a65SAndreas Gohr // or comment next line for RFC2045 conformance, if you like 267db959ae3SAndreas Gohr if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))){ 26891275a65SAndreas Gohr if (($iLastChar==0x09)||($iLastChar==0x20)) { 26991275a65SAndreas Gohr $sLine{$iLength-1}='='; 27091275a65SAndreas Gohr $sLine .= ($iLastChar==0x09)?'09':'20'; 27144f669e9Sandi } 272db959ae3SAndreas Gohr } 27391275a65SAndreas Gohr } // imap_8bit encodes x20 before chr(13), too 27491275a65SAndreas Gohr // although IMHO not requested by RFC2045, why not do it safer :) 27591275a65SAndreas Gohr // and why not encode any x20 around chr(10) or chr(13) 27691275a65SAndreas Gohr if ($bEmulate_imap_8bit) { 27791275a65SAndreas Gohr $sLine=str_replace(' =0D','=20=0D',$sLine); 27891275a65SAndreas Gohr //$sLine=str_replace(' =0A','=20=0A',$sLine); 27991275a65SAndreas Gohr //$sLine=str_replace('=0D ','=0D=20',$sLine); 28091275a65SAndreas Gohr //$sLine=str_replace('=0A ','=0A=20',$sLine); 28144f669e9Sandi } 28244f669e9Sandi 28391275a65SAndreas Gohr // finally split into softlines no longer than $maxlen chars, 28491275a65SAndreas Gohr // for even more safeness one could encode x09,x20 28591275a65SAndreas Gohr // at the very first character of the line 28691275a65SAndreas Gohr // and after soft linebreaks, as well, 28791275a65SAndreas Gohr // but this wouldn't be caught by such an easy RegExp 28891275a65SAndreas Gohr if($maxlen){ 28991275a65SAndreas Gohr preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch ); 2902ae68f97SAndreas Gohr $sLine = implode( '=' . QUOTEDPRINTABLE_EOL, $aMatch[0] ); // add soft crlf's 29191275a65SAndreas Gohr } 29291275a65SAndreas Gohr } 29391275a65SAndreas Gohr 29491275a65SAndreas Gohr // join lines into text 2952ae68f97SAndreas Gohr return implode(QUOTEDPRINTABLE_EOL,$aLines); 29691275a65SAndreas Gohr} 29744f669e9Sandi 298