xref: /dokuwiki/inc/mail.php (revision ed7b5f0908941f1bacef7e7c3a02c106a42cd5cc)
1*ed7b5f09Sandi<?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
9*ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10*ed7b5f09Sandi  require_once(DOKU_INC.'inc/utf8.php');
1144f669e9Sandi
1244f669e9Sandi  define('MAILHEADER_EOL',"\n"); //end of line for mail headers
1344f669e9Sandi
1444f669e9Sandi/**
1544f669e9Sandi * UTF-8 autoencoding replacement for PHPs mail function
1644f669e9Sandi *
1744f669e9Sandi * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
1844f669e9Sandi * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
1944f669e9Sandi * automatically. You can seperate receivers by commas.
2044f669e9Sandi *
2144f669e9Sandi * @todo  currently an empty To: header is added
2244f669e9Sandi *
2344f669e9Sandi * @param string $to      Receiver of the mail (multiple seperated by commas)
2444f669e9Sandi * @param string $subject Mailsubject
2544f669e9Sandi * @param string $body    Messagebody
2644f669e9Sandi * @param string $from    Sender address
2744f669e9Sandi * @param string $cc      CarbonCopy receiver (multiple seperated by commas)
2844f669e9Sandi * @param string $bcc     BlindCarbonCopy receiver (multiple seperated by commas)
2944f669e9Sandi * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
3044f669e9Sandi * @param string $params  Additonal Sendmail params (passed to mail())
3144f669e9Sandi *
3244f669e9Sandi * @author Andreas Gohr <andi@splitbrain.org>
3344f669e9Sandi * @see    mail()
3444f669e9Sandi */
3544f669e9Sandifunction mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
3644f669e9Sandi  if(!utf8_isASCII($subject)) $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject).'?=';
3744f669e9Sandi
3844f669e9Sandi  $header  = '';
3944f669e9Sandi  $header .= mail_encode_address($from,'From');
4044f669e9Sandi  $header .= mail_encode_address($to,'To');
4144f669e9Sandi  $header .= mail_encode_address($cc,'Cc');
4244f669e9Sandi  $header .= mail_encode_address($bcc,'Bcc');
4344f669e9Sandi  $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
4444f669e9Sandi  $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
4544f669e9Sandi  $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
4644f669e9Sandi  $header .= $headers;
4744f669e9Sandi  $heade   = trim($header);
4844f669e9Sandi
4944f669e9Sandi  $body = mail_quotedprintable_encode($body);
5044f669e9Sandi
5144f669e9Sandi  return @mail(null,$subject,$body,$header,$params);
5244f669e9Sandi}
5344f669e9Sandi
5444f669e9Sandi/**
5544f669e9Sandi * Encodes an email address header
5644f669e9Sandi *
5744f669e9Sandi * Unicode chracters will be encoded quoted_printable for headers like
5844f669e9Sandi * Addresses may not contain Non-ASCII data!
5944f669e9Sandi *
6044f669e9Sandi * Example:
6144f669e9Sandi *   mail_encode_address("föö <foo@bar.com>, me@somewhere.com","To");
6244f669e9Sandi *
6344f669e9Sandi * @param string $string Multiple headers seperated by commas
6444f669e9Sandi */
6544f669e9Sandifunction mail_encode_address($string,$header='To'){
6644f669e9Sandi  $headers = '';
6744f669e9Sandi  $parts = split(',',$string);
6844f669e9Sandi  foreach ($parts as $part){
6944f669e9Sandi    $part = trim($part);
7044f669e9Sandi
7144f669e9Sandi    // parse address
7244f669e9Sandi    if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
7344f669e9Sandi      $text = trim($matches[1]);
7444f669e9Sandi      $addr = $matches[2];
7544f669e9Sandi    }else{
7644f669e9Sandi      $addr = $part;
7744f669e9Sandi    }
7844f669e9Sandi
7944f669e9Sandi    // skip empty ones
8044f669e9Sandi    if(empty($addr)){
8144f669e9Sandi      continue;
8244f669e9Sandi    }
8344f669e9Sandi
8444f669e9Sandi    // FIXME: is there a way to encode the localpart of a emailaddress?
8544f669e9Sandi    if(!utf8_isASCII($addr)){
8644f669e9Sandi      msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
8744f669e9Sandi      continue;
8844f669e9Sandi    }
8944f669e9Sandi
9044f669e9Sandi    if(!mail_isvalid($addr)){
9144f669e9Sandi      msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
9244f669e9Sandi      continue;
9344f669e9Sandi    }
9444f669e9Sandi
9544f669e9Sandi    // no text was given
9644f669e9Sandi    if(empty($text)){
9744f669e9Sandi      $headers .= $header.': <'.$addr.'>'.MAILHEADER_EOL;
9844f669e9Sandi      continue;
9944f669e9Sandi    }
10044f669e9Sandi
10144f669e9Sandi    // FIME: can make problems with long headers?
10244f669e9Sandi    if(!utf8_isASCII($text)){
10344f669e9Sandi      $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text).'?=';
10444f669e9Sandi    }
10544f669e9Sandi
10644f669e9Sandi    //construct header
10744f669e9Sandi    $headers .= $header.': '.$text.' <'.$addr.'>'.MAILHEADER_EOL;
10844f669e9Sandi  }
10944f669e9Sandi
11044f669e9Sandi  return $headers;
11144f669e9Sandi}
11244f669e9Sandi
11344f669e9Sandi/**
11444f669e9Sandi * Uses a regular expresion to check if a given mail address is valid
11544f669e9Sandi *
11644f669e9Sandi * May not be completly RFC conform!
11744f669e9Sandi *
11844f669e9Sandi * @link    http://www.webmasterworld.com/forum88/135.htm
11944f669e9Sandi *
12044f669e9Sandi * @param   string $email the address to check
12144f669e9Sandi * @return  bool          true if address is valid
12244f669e9Sandi */
12344f669e9Sandifunction mail_isvalid($email){
12444f669e9Sandi  return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
12544f669e9Sandi}
12644f669e9Sandi
12744f669e9Sandi/**
12844f669e9Sandi * Quoted printable encoding
12944f669e9Sandi *
13044f669e9Sandi * @author <pob@medienrecht.org>
13144f669e9Sandi * @author <tamas.tompa@kirowski.com>
13244f669e9Sandi * @link   http://www.php.net/manual/en/function.quoted-printable-decode.php
13344f669e9Sandi */
13444f669e9Sandifunction mail_quotedprintable_encode($input='',$line_max=74,$space_conv=false){
13544f669e9Sandi  $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
13644f669e9Sandi  $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
13744f669e9Sandi  $eol = "\n";
13844f669e9Sandi  $escape = "=";
13944f669e9Sandi  $output = "";
14044f669e9Sandi  while( list(, $line) = each($lines) ) {
14144f669e9Sandi    //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
14244f669e9Sandi    $linlen = strlen($line);
14344f669e9Sandi    $newline = "";
14444f669e9Sandi    for($i = 0; $i < $linlen; $i++) {
14544f669e9Sandi      $c = substr( $line, $i, 1 );
14644f669e9Sandi      $dec = ord( $c );
14744f669e9Sandi      if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
14844f669e9Sandi        $c = "=2E";
14944f669e9Sandi      }
15044f669e9Sandi      if ( $dec == 32 ) {
15144f669e9Sandi        if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
15244f669e9Sandi          $c = "=20";
15344f669e9Sandi        } else if ( $space_conv ) {
15444f669e9Sandi          $c = "=20";
15544f669e9Sandi        }
15644f669e9Sandi      } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
15744f669e9Sandi        $h2 = floor($dec/16);
15844f669e9Sandi        $h1 = floor($dec%16);
15944f669e9Sandi        $c = $escape.$hex["$h2"].$hex["$h1"];
16044f669e9Sandi      }
16144f669e9Sandi      if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
16244f669e9Sandi         $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
16344f669e9Sandi         $newline = "";
16444f669e9Sandi         // check if newline first character will be point or not
16544f669e9Sandi         if ( $dec == 46 ) {
16644f669e9Sandi            $c = "=2E";
16744f669e9Sandi         }
16844f669e9Sandi      }
16944f669e9Sandi      $newline .= $c;
17044f669e9Sandi    } // end of for
17144f669e9Sandi    $output .= $newline.$eol;
17244f669e9Sandi  } // end of while
17344f669e9Sandi  return trim($output);
17444f669e9Sandi}
17544f669e9Sandi
17644f669e9Sandi
17744f669e9Sandi?>
178