xref: /dokuwiki/inc/mail.php (revision e8f8d645be4a431ee7114859b9aedae042f52ead)
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');
11*e8f8d645SAndreas 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/**
1944f669e9Sandi * UTF-8 autoencoding replacement for PHPs mail function
2044f669e9Sandi *
2144f669e9Sandi * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
2244f669e9Sandi * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
2344f669e9Sandi * automatically. You can seperate receivers by commas.
2444f669e9Sandi *
2544f669e9Sandi * @param string $to      Receiver of the mail (multiple seperated by commas)
2644f669e9Sandi * @param string $subject Mailsubject
2744f669e9Sandi * @param string $body    Messagebody
2844f669e9Sandi * @param string $from    Sender address
2944f669e9Sandi * @param string $cc      CarbonCopy receiver (multiple seperated by commas)
3044f669e9Sandi * @param string $bcc     BlindCarbonCopy receiver (multiple seperated by commas)
3144f669e9Sandi * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
3244f669e9Sandi * @param string $params  Additonal Sendmail params (passed to mail())
3344f669e9Sandi *
3444f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org>
3544f669e9Sandi * @see    mail()
3644f669e9Sandi */
3744f669e9Sandifunction mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
38348e3c03SChris Smith
391986aa9eSChris Smith  $message = compact('to','subject','body','from','cc','bcc','headers','params');
40348e3c03SChris Smith  return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action');
411986aa9eSChris Smith}
421986aa9eSChris Smith
43348e3c03SChris Smithfunction _mail_send_action($data) {
44348e3c03SChris Smith
45348e3c03SChris Smith  // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params
46348e3c03SChris Smith  $to = $data['to'];
47348e3c03SChris Smith  $subject = $data['subject'];
48348e3c03SChris Smith  $body = $data['body'];
49348e3c03SChris Smith
50348e3c03SChris Smith  // add robustness in case plugin removes any of these optional values
51348e3c03SChris Smith  $from = isset($data['from']) ? $data['from'] : '';
52348e3c03SChris Smith  $cc = isset($data['cc']) ? $data['cc'] : '';
53348e3c03SChris Smith  $bcc = isset($data['bcc']) ? $data['bcc'] : '';
54348e3c03SChris Smith  $headers = isset($data['headers']) ? $data['headers'] : null;
55348e3c03SChris Smith  $params = isset($data['params']) ? $data['params'] : null;
56348e3c03SChris Smith
57348e3c03SChris Smith  // end additional code to support event ... original mail_send() code from here
58348e3c03SChris Smith
59e1906e6eSandi  if(defined('MAILHEADER_ASCIIONLY')){
60e1906e6eSandi    $subject = utf8_deaccent($subject);
61e1906e6eSandi    $subject = utf8_strip($subject);
62e1906e6eSandi  }
63e1906e6eSandi
64265f02e3SGuy Brand  if(!utf8_isASCII($subject)) {
6591275a65SAndreas Gohr    $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?=';
66265f02e3SGuy Brand    // Spaces must be encoded according to rfc2047. Use the "_" shorthand
67265f02e3SGuy Brand    $subject = preg_replace('/ /', '_', $subject);
68265f02e3SGuy Brand  }
6944f669e9Sandi
7044f669e9Sandi  $header  = '';
71e1906e6eSandi
72f95e691eSAndreas Gohr  // No named recipients for To: in Windows (see FS#652)
73f95e691eSAndreas Gohr  $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;
74f95e691eSAndreas Gohr
75f2531e7eSAndreas Gohr  // On Unix set the envelope headers correctly:
76f2531e7eSAndreas Gohr  if($usenames){
772300b3e6SAndreas Gohr    if($from) $params = ((string) $params).' -f '.escapeshellarg(mail_encode_address($from,'',false));
782300b3e6SAndreas Gohr    if($to)   $params = ((string) $params).' '.escapeshellarg(mail_encode_address($to,'',false));
79f2531e7eSAndreas Gohr  }
80f2531e7eSAndreas Gohr
81f95e691eSAndreas Gohr  $to = mail_encode_address($to,'',$usenames);
8244f669e9Sandi  $header .= mail_encode_address($from,'From');
8344f669e9Sandi  $header .= mail_encode_address($cc,'Cc');
8444f669e9Sandi  $header .= mail_encode_address($bcc,'Bcc');
8544f669e9Sandi  $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
8644f669e9Sandi  $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
8744f669e9Sandi  $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
8844f669e9Sandi  $header .= $headers;
89e1906e6eSandi  $header  = trim($header);
9044f669e9Sandi
9144f669e9Sandi  $body = mail_quotedprintable_encode($body);
9244f669e9Sandi
93d7785cceSandi  if($params == null){
94d7785cceSandi    return @mail($to,$subject,$body,$header);
95d7785cceSandi  }else{
96e1906e6eSandi    return @mail($to,$subject,$body,$header,$params);
9744f669e9Sandi  }
98d7785cceSandi}
9944f669e9Sandi
10044f669e9Sandi/**
10144f669e9Sandi * Encodes an email address header
10244f669e9Sandi *
103e1906e6eSandi * Unicode characters will be deaccented and encoded
104e1906e6eSandi * quoted_printable for headers.
10544f669e9Sandi * Addresses may not contain Non-ASCII data!
10644f669e9Sandi *
10744f669e9Sandi * Example:
108a2021ad8Sandi *   mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc");
10944f669e9Sandi *
110f95e691eSAndreas Gohr * @param string  $string Multiple adresses separated by commas
111f95e691eSAndreas Gohr * @param string  $header Name of the header (To,Bcc,Cc,...)
112f95e691eSAndreas Gohr * @param boolean $names  Allow named Recipients?
11344f669e9Sandi */
114f95e691eSAndreas Gohrfunction mail_encode_address($string,$header='',$names=true){
11544f669e9Sandi  $headers = '';
11644f669e9Sandi  $parts = split(',',$string);
11744f669e9Sandi  foreach ($parts as $part){
11844f669e9Sandi    $part = trim($part);
11944f669e9Sandi
12044f669e9Sandi    // parse address
12144f669e9Sandi    if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
12244f669e9Sandi      $text = trim($matches[1]);
12344f669e9Sandi      $addr = $matches[2];
12444f669e9Sandi    }else{
12544f669e9Sandi      $addr = $part;
12644f669e9Sandi    }
12744f669e9Sandi
12844f669e9Sandi    // skip empty ones
12944f669e9Sandi    if(empty($addr)){
13044f669e9Sandi      continue;
13144f669e9Sandi    }
13244f669e9Sandi
13344f669e9Sandi    // FIXME: is there a way to encode the localpart of a emailaddress?
13444f669e9Sandi    if(!utf8_isASCII($addr)){
13544f669e9Sandi      msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
13644f669e9Sandi      continue;
13744f669e9Sandi    }
13844f669e9Sandi
13944f669e9Sandi    if(!mail_isvalid($addr)){
14044f669e9Sandi      msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
14144f669e9Sandi      continue;
14244f669e9Sandi    }
14344f669e9Sandi
144a2021ad8Sandi    // text was given
145f95e691eSAndreas Gohr    if(!empty($text) && $names){
146a2021ad8Sandi      // add address quotes
147a2021ad8Sandi      $addr = "<$addr>";
14844f669e9Sandi
149e1906e6eSandi      if(defined('MAILHEADER_ASCIIONLY')){
150e1906e6eSandi        $text = utf8_deaccent($text);
151e1906e6eSandi        $text = utf8_strip($text);
152e1906e6eSandi      }
153e1906e6eSandi
15444f669e9Sandi      if(!utf8_isASCII($text)){
15591275a65SAndreas Gohr        $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text,0).'?=';
15644f669e9Sandi      }
157628e6ba7SAndreas Gohr    }else{
158628e6ba7SAndreas Gohr      $text = '';
15944f669e9Sandi    }
16044f669e9Sandi
1612300b3e6SAndreas Gohr    // add to header comma seperated
1622300b3e6SAndreas Gohr    if($headers != ''){
1632300b3e6SAndreas Gohr        $headers .= ',';
1642300b3e6SAndreas Gohr        if($header) $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers
1652300b3e6SAndreas Gohr    }
166ba36e50eSAndreas Gohr    $headers .= $text.' '.$addr;
167a2021ad8Sandi  }
168a2021ad8Sandi
169a2021ad8Sandi  if(empty($headers)) return null;
170a2021ad8Sandi
171a2021ad8Sandi  //if headername was given add it and close correctly
172a2021ad8Sandi  if($header) $headers = $header.': '.$headers.MAILHEADER_EOL;
173a2021ad8Sandi
17444f669e9Sandi  return $headers;
17544f669e9Sandi}
17644f669e9Sandi
17744f669e9Sandi/**
178*e8f8d645SAndreas Gohr * Check if a given mail address is valid
17944f669e9Sandi *
18044f669e9Sandi * @param   string $email the address to check
18144f669e9Sandi * @return  bool          true if address is valid
18244f669e9Sandi */
18344f669e9Sandifunction mail_isvalid($email){
184*e8f8d645SAndreas Gohr    $validator = new EmailAddressValidator;
185*e8f8d645SAndreas Gohr    return $validator->check_email_address($email);
18644f669e9Sandi}
18744f669e9Sandi
18844f669e9Sandi/**
18944f669e9Sandi * Quoted printable encoding
19044f669e9Sandi *
19191275a65SAndreas Gohr * @author umu <umuAThrz.tu-chemnitz.de>
19291275a65SAndreas Gohr * @link   http://www.php.net/manual/en/function.imap-8bit.php#61216
19344f669e9Sandi */
19491275a65SAndreas Gohrfunction mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) {
19591275a65SAndreas Gohr  // split text into lines
19691275a65SAndreas Gohr  $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText);
19791275a65SAndreas Gohr
19891275a65SAndreas Gohr  for ($i=0;$i<count($aLines);$i++) {
19991275a65SAndreas Gohr    $sLine =& $aLines[$i];
20091275a65SAndreas Gohr    if (strlen($sLine)===0) continue; // do nothing, if empty
20191275a65SAndreas Gohr
20291275a65SAndreas Gohr    $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
20391275a65SAndreas Gohr
20491275a65SAndreas Gohr    // imap_8bit encodes x09 everywhere, not only at lineends,
20591275a65SAndreas Gohr    // for EBCDIC safeness encode !"#$@[\]^`{|}~,
20691275a65SAndreas Gohr    // for complete safeness encode every character :)
20791275a65SAndreas Gohr    if ($bEmulate_imap_8bit)
20891275a65SAndreas Gohr      $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
20991275a65SAndreas Gohr
21091275a65SAndreas Gohr    $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
21191275a65SAndreas Gohr    $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
21291275a65SAndreas Gohr
21391275a65SAndreas Gohr    // encode x09,x20 at lineends
21491275a65SAndreas Gohr    {
21591275a65SAndreas Gohr      $iLength = strlen($sLine);
21691275a65SAndreas Gohr      $iLastChar = ord($sLine{$iLength-1});
21791275a65SAndreas Gohr
21891275a65SAndreas Gohr      //              !!!!!!!!
21991275a65SAndreas Gohr      // imap_8_bit does not encode x20 at the very end of a text,
22091275a65SAndreas Gohr      // here is, where I don't agree with imap_8_bit,
22191275a65SAndreas Gohr      // please correct me, if I'm wrong,
22291275a65SAndreas Gohr      // or comment next line for RFC2045 conformance, if you like
22391275a65SAndreas Gohr      if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
22491275a65SAndreas Gohr
22591275a65SAndreas Gohr      if (($iLastChar==0x09)||($iLastChar==0x20)) {
22691275a65SAndreas Gohr        $sLine{$iLength-1}='=';
22791275a65SAndreas Gohr        $sLine .= ($iLastChar==0x09)?'09':'20';
22844f669e9Sandi      }
22991275a65SAndreas Gohr    }    // imap_8bit encodes x20 before chr(13), too
23091275a65SAndreas Gohr    // although IMHO not requested by RFC2045, why not do it safer :)
23191275a65SAndreas Gohr    // and why not encode any x20 around chr(10) or chr(13)
23291275a65SAndreas Gohr    if ($bEmulate_imap_8bit) {
23391275a65SAndreas Gohr      $sLine=str_replace(' =0D','=20=0D',$sLine);
23491275a65SAndreas Gohr      //$sLine=str_replace(' =0A','=20=0A',$sLine);
23591275a65SAndreas Gohr      //$sLine=str_replace('=0D ','=0D=20',$sLine);
23691275a65SAndreas Gohr      //$sLine=str_replace('=0A ','=0A=20',$sLine);
23744f669e9Sandi    }
23844f669e9Sandi
23991275a65SAndreas Gohr    // finally split into softlines no longer than $maxlen chars,
24091275a65SAndreas Gohr    // for even more safeness one could encode x09,x20
24191275a65SAndreas Gohr    // at the very first character of the line
24291275a65SAndreas Gohr    // and after soft linebreaks, as well,
24391275a65SAndreas Gohr    // but this wouldn't be caught by such an easy RegExp
24491275a65SAndreas Gohr    if($maxlen){
24591275a65SAndreas Gohr      preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch );
24629f3a5faSAndreas Gohr      $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's
24791275a65SAndreas Gohr    }
24891275a65SAndreas Gohr  }
24991275a65SAndreas Gohr
25091275a65SAndreas Gohr  // join lines into text
25129f3a5faSAndreas Gohr  return implode(MAILHEADER_EOL,$aLines);
25291275a65SAndreas Gohr}
25344f669e9Sandi
254340756e4Sandi
255340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
256