xref: /dokuwiki/inc/mail.php (revision d530a62a501f6dfec02b4c3378cc82ae7d25ce9d)
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');
11e8f8d645SAndreas Gohr  require_once(DOKU_INC.'inc/EmailAddressValidator.php');
1244f669e9Sandi
1329f3a5faSAndreas Gohr  // end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?)
1429f3a5faSAndreas Gohr  // think different
1529f3a5faSAndreas Gohr  if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n");
16e1906e6eSandi  #define('MAILHEADER_ASCIIONLY',1);
1744f669e9Sandi
1844f669e9Sandi/**
19*d530a62aSAndreas Gohr * Patterns for use in email detection and validation
20*d530a62aSAndreas Gohr *
21*d530a62aSAndreas Gohr * NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser
22*d530a62aSAndreas Gohr * the pattern uses non-capturing groups as captured groups aren't allowed in the parser
23*d530a62aSAndreas Gohr * select pattern delimiters with care!
24*d530a62aSAndreas Gohr *
25*d530a62aSAndreas Gohr * May not be completly RFC conform!
26*d530a62aSAndreas Gohr * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4)
27*d530a62aSAndreas Gohr *
28*d530a62aSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk>
29*d530a62aSAndreas Gohr * Check if a given mail address is valid
30*d530a62aSAndreas Gohr*/
31*d530a62aSAndreas Gohrif (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-zA-Z!#$%&'*+/=?^_`{|}~-");
32*d530a62aSAndreas 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)');
33*d530a62aSAndreas Gohr
34*d530a62aSAndreas Gohr
35*d530a62aSAndreas Gohr
36*d530a62aSAndreas Gohr/**
3744f669e9Sandi * UTF-8 autoencoding replacement for PHPs mail function
3844f669e9Sandi *
3944f669e9Sandi * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
4044f669e9Sandi * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
4144f669e9Sandi * automatically. You can seperate receivers by commas.
4244f669e9Sandi *
4344f669e9Sandi * @param string $to      Receiver of the mail (multiple seperated by commas)
4444f669e9Sandi * @param string $subject Mailsubject
4544f669e9Sandi * @param string $body    Messagebody
4644f669e9Sandi * @param string $from    Sender address
4744f669e9Sandi * @param string $cc      CarbonCopy receiver (multiple seperated by commas)
4844f669e9Sandi * @param string $bcc     BlindCarbonCopy receiver (multiple seperated by commas)
4944f669e9Sandi * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
5044f669e9Sandi * @param string $params  Additonal Sendmail params (passed to mail())
5144f669e9Sandi *
5244f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org>
5344f669e9Sandi * @see    mail()
5444f669e9Sandi */
5544f669e9Sandifunction mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
56348e3c03SChris Smith
571986aa9eSChris Smith  $message = compact('to','subject','body','from','cc','bcc','headers','params');
58348e3c03SChris Smith  return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action');
591986aa9eSChris Smith}
601986aa9eSChris Smith
61348e3c03SChris Smithfunction _mail_send_action($data) {
62348e3c03SChris Smith
63348e3c03SChris Smith  // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params
64348e3c03SChris Smith  $to = $data['to'];
65348e3c03SChris Smith  $subject = $data['subject'];
66348e3c03SChris Smith  $body = $data['body'];
67348e3c03SChris Smith
68348e3c03SChris Smith  // add robustness in case plugin removes any of these optional values
69348e3c03SChris Smith  $from = isset($data['from']) ? $data['from'] : '';
70348e3c03SChris Smith  $cc = isset($data['cc']) ? $data['cc'] : '';
71348e3c03SChris Smith  $bcc = isset($data['bcc']) ? $data['bcc'] : '';
72348e3c03SChris Smith  $headers = isset($data['headers']) ? $data['headers'] : null;
73348e3c03SChris Smith  $params = isset($data['params']) ? $data['params'] : null;
74348e3c03SChris Smith
75348e3c03SChris Smith  // end additional code to support event ... original mail_send() code from here
76348e3c03SChris Smith
77e1906e6eSandi  if(defined('MAILHEADER_ASCIIONLY')){
78e1906e6eSandi    $subject = utf8_deaccent($subject);
79e1906e6eSandi    $subject = utf8_strip($subject);
80e1906e6eSandi  }
81e1906e6eSandi
82265f02e3SGuy Brand  if(!utf8_isASCII($subject)) {
8391275a65SAndreas Gohr    $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?=';
84265f02e3SGuy Brand    // Spaces must be encoded according to rfc2047. Use the "_" shorthand
85265f02e3SGuy Brand    $subject = preg_replace('/ /', '_', $subject);
86265f02e3SGuy Brand  }
8744f669e9Sandi
8844f669e9Sandi  $header  = '';
89e1906e6eSandi
90f95e691eSAndreas Gohr  // No named recipients for To: in Windows (see FS#652)
91f95e691eSAndreas Gohr  $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;
92f95e691eSAndreas Gohr
93f2531e7eSAndreas Gohr  // On Unix set the envelope headers correctly:
94f2531e7eSAndreas Gohr  if($usenames){
952300b3e6SAndreas Gohr    if($from) $params = ((string) $params).' -f '.escapeshellarg(mail_encode_address($from,'',false));
962300b3e6SAndreas Gohr    if($to)   $params = ((string) $params).' '.escapeshellarg(mail_encode_address($to,'',false));
97f2531e7eSAndreas Gohr  }
98f2531e7eSAndreas Gohr
99f95e691eSAndreas Gohr  $to = mail_encode_address($to,'',$usenames);
10044f669e9Sandi  $header .= mail_encode_address($from,'From');
10144f669e9Sandi  $header .= mail_encode_address($cc,'Cc');
10244f669e9Sandi  $header .= mail_encode_address($bcc,'Bcc');
10344f669e9Sandi  $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
10444f669e9Sandi  $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
10544f669e9Sandi  $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
10644f669e9Sandi  $header .= $headers;
107e1906e6eSandi  $header  = trim($header);
10844f669e9Sandi
10944f669e9Sandi  $body = mail_quotedprintable_encode($body);
11044f669e9Sandi
111d7785cceSandi  if($params == null){
112d7785cceSandi    return @mail($to,$subject,$body,$header);
113d7785cceSandi  }else{
114e1906e6eSandi    return @mail($to,$subject,$body,$header,$params);
11544f669e9Sandi  }
116d7785cceSandi}
11744f669e9Sandi
11844f669e9Sandi/**
11944f669e9Sandi * Encodes an email address header
12044f669e9Sandi *
121e1906e6eSandi * Unicode characters will be deaccented and encoded
122e1906e6eSandi * quoted_printable for headers.
12344f669e9Sandi * Addresses may not contain Non-ASCII data!
12444f669e9Sandi *
12544f669e9Sandi * Example:
126a2021ad8Sandi *   mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc");
12744f669e9Sandi *
128f95e691eSAndreas Gohr * @param string  $string Multiple adresses separated by commas
129f95e691eSAndreas Gohr * @param string  $header Name of the header (To,Bcc,Cc,...)
130f95e691eSAndreas Gohr * @param boolean $names  Allow named Recipients?
13144f669e9Sandi */
132f95e691eSAndreas Gohrfunction mail_encode_address($string,$header='',$names=true){
13344f669e9Sandi  $headers = '';
13444f669e9Sandi  $parts = split(',',$string);
13544f669e9Sandi  foreach ($parts as $part){
13644f669e9Sandi    $part = trim($part);
13744f669e9Sandi
13844f669e9Sandi    // parse address
13944f669e9Sandi    if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
14044f669e9Sandi      $text = trim($matches[1]);
14144f669e9Sandi      $addr = $matches[2];
14244f669e9Sandi    }else{
14344f669e9Sandi      $addr = $part;
14444f669e9Sandi    }
14544f669e9Sandi
14644f669e9Sandi    // skip empty ones
14744f669e9Sandi    if(empty($addr)){
14844f669e9Sandi      continue;
14944f669e9Sandi    }
15044f669e9Sandi
15144f669e9Sandi    // FIXME: is there a way to encode the localpart of a emailaddress?
15244f669e9Sandi    if(!utf8_isASCII($addr)){
15344f669e9Sandi      msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
15444f669e9Sandi      continue;
15544f669e9Sandi    }
15644f669e9Sandi
15744f669e9Sandi    if(!mail_isvalid($addr)){
15844f669e9Sandi      msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
15944f669e9Sandi      continue;
16044f669e9Sandi    }
16144f669e9Sandi
162a2021ad8Sandi    // text was given
163f95e691eSAndreas Gohr    if(!empty($text) && $names){
164a2021ad8Sandi      // add address quotes
165a2021ad8Sandi      $addr = "<$addr>";
16644f669e9Sandi
167e1906e6eSandi      if(defined('MAILHEADER_ASCIIONLY')){
168e1906e6eSandi        $text = utf8_deaccent($text);
169e1906e6eSandi        $text = utf8_strip($text);
170e1906e6eSandi      }
171e1906e6eSandi
17244f669e9Sandi      if(!utf8_isASCII($text)){
17391275a65SAndreas Gohr        $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text,0).'?=';
17444f669e9Sandi      }
175628e6ba7SAndreas Gohr    }else{
176628e6ba7SAndreas Gohr      $text = '';
17744f669e9Sandi    }
17844f669e9Sandi
1792300b3e6SAndreas Gohr    // add to header comma seperated
1802300b3e6SAndreas Gohr    if($headers != ''){
1812300b3e6SAndreas Gohr        $headers .= ',';
1822300b3e6SAndreas Gohr        if($header) $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers
1832300b3e6SAndreas Gohr    }
184ba36e50eSAndreas Gohr    $headers .= $text.' '.$addr;
185a2021ad8Sandi  }
186a2021ad8Sandi
187a2021ad8Sandi  if(empty($headers)) return null;
188a2021ad8Sandi
189a2021ad8Sandi  //if headername was given add it and close correctly
190a2021ad8Sandi  if($header) $headers = $header.': '.$headers.MAILHEADER_EOL;
191a2021ad8Sandi
19244f669e9Sandi  return $headers;
19344f669e9Sandi}
19444f669e9Sandi
19544f669e9Sandi/**
196e8f8d645SAndreas Gohr * Check if a given mail address is valid
19744f669e9Sandi *
19844f669e9Sandi * @param   string $email the address to check
19944f669e9Sandi * @return  bool          true if address is valid
20044f669e9Sandi */
20144f669e9Sandifunction mail_isvalid($email){
202e8f8d645SAndreas Gohr    $validator = new EmailAddressValidator;
203e8f8d645SAndreas Gohr    return $validator->check_email_address($email);
20444f669e9Sandi}
20544f669e9Sandi
20644f669e9Sandi/**
20744f669e9Sandi * Quoted printable encoding
20844f669e9Sandi *
20991275a65SAndreas Gohr * @author umu <umuAThrz.tu-chemnitz.de>
21091275a65SAndreas Gohr * @link   http://www.php.net/manual/en/function.imap-8bit.php#61216
21144f669e9Sandi */
21291275a65SAndreas Gohrfunction mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) {
21391275a65SAndreas Gohr  // split text into lines
21491275a65SAndreas Gohr  $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText);
21591275a65SAndreas Gohr
21691275a65SAndreas Gohr  for ($i=0;$i<count($aLines);$i++) {
21791275a65SAndreas Gohr    $sLine =& $aLines[$i];
21891275a65SAndreas Gohr    if (strlen($sLine)===0) continue; // do nothing, if empty
21991275a65SAndreas Gohr
22091275a65SAndreas Gohr    $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
22191275a65SAndreas Gohr
22291275a65SAndreas Gohr    // imap_8bit encodes x09 everywhere, not only at lineends,
22391275a65SAndreas Gohr    // for EBCDIC safeness encode !"#$@[\]^`{|}~,
22491275a65SAndreas Gohr    // for complete safeness encode every character :)
22591275a65SAndreas Gohr    if ($bEmulate_imap_8bit)
22691275a65SAndreas Gohr      $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
22791275a65SAndreas Gohr
22891275a65SAndreas Gohr    $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
22991275a65SAndreas Gohr    $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
23091275a65SAndreas Gohr
23191275a65SAndreas Gohr    // encode x09,x20 at lineends
23291275a65SAndreas Gohr    {
23391275a65SAndreas Gohr      $iLength = strlen($sLine);
23491275a65SAndreas Gohr      $iLastChar = ord($sLine{$iLength-1});
23591275a65SAndreas Gohr
23691275a65SAndreas Gohr      //              !!!!!!!!
23791275a65SAndreas Gohr      // imap_8_bit does not encode x20 at the very end of a text,
23891275a65SAndreas Gohr      // here is, where I don't agree with imap_8_bit,
23991275a65SAndreas Gohr      // please correct me, if I'm wrong,
24091275a65SAndreas Gohr      // or comment next line for RFC2045 conformance, if you like
24191275a65SAndreas Gohr      if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
24291275a65SAndreas Gohr
24391275a65SAndreas Gohr      if (($iLastChar==0x09)||($iLastChar==0x20)) {
24491275a65SAndreas Gohr        $sLine{$iLength-1}='=';
24591275a65SAndreas Gohr        $sLine .= ($iLastChar==0x09)?'09':'20';
24644f669e9Sandi      }
24791275a65SAndreas Gohr    }    // imap_8bit encodes x20 before chr(13), too
24891275a65SAndreas Gohr    // although IMHO not requested by RFC2045, why not do it safer :)
24991275a65SAndreas Gohr    // and why not encode any x20 around chr(10) or chr(13)
25091275a65SAndreas Gohr    if ($bEmulate_imap_8bit) {
25191275a65SAndreas Gohr      $sLine=str_replace(' =0D','=20=0D',$sLine);
25291275a65SAndreas Gohr      //$sLine=str_replace(' =0A','=20=0A',$sLine);
25391275a65SAndreas Gohr      //$sLine=str_replace('=0D ','=0D=20',$sLine);
25491275a65SAndreas Gohr      //$sLine=str_replace('=0A ','=0A=20',$sLine);
25544f669e9Sandi    }
25644f669e9Sandi
25791275a65SAndreas Gohr    // finally split into softlines no longer than $maxlen chars,
25891275a65SAndreas Gohr    // for even more safeness one could encode x09,x20
25991275a65SAndreas Gohr    // at the very first character of the line
26091275a65SAndreas Gohr    // and after soft linebreaks, as well,
26191275a65SAndreas Gohr    // but this wouldn't be caught by such an easy RegExp
26291275a65SAndreas Gohr    if($maxlen){
26391275a65SAndreas Gohr      preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch );
26429f3a5faSAndreas Gohr      $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's
26591275a65SAndreas Gohr    }
26691275a65SAndreas Gohr  }
26791275a65SAndreas Gohr
26891275a65SAndreas Gohr  // join lines into text
26929f3a5faSAndreas Gohr  return implode(MAILHEADER_EOL,$aLines);
27091275a65SAndreas Gohr}
27144f669e9Sandi
272340756e4Sandi
273340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
274