xref: /dokuwiki/inc/mail.php (revision 3a8a9050bc97ca54a26f0163fb9b8d50c82f8b3d)
1<?php
2/**
3 * Mail functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10  require_once(DOKU_INC.'inc/utf8.php');
11
12  define('MAILHEADER_EOL',"\n"); //end of line for mail headers
13
14/**
15 * UTF-8 autoencoding replacement for PHPs mail function
16 *
17 * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
18 * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
19 * automatically. You can seperate receivers by commas.
20 *
21 * @todo  currently an empty To: header is added
22 *
23 * @param string $to      Receiver of the mail (multiple seperated by commas)
24 * @param string $subject Mailsubject
25 * @param string $body    Messagebody
26 * @param string $from    Sender address
27 * @param string $cc      CarbonCopy receiver (multiple seperated by commas)
28 * @param string $bcc     BlindCarbonCopy receiver (multiple seperated by commas)
29 * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
30 * @param string $params  Additonal Sendmail params (passed to mail())
31 *
32 * @author Andreas Gohr <andi@splitbrain.org>
33 * @see    mail()
34 */
35function mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
36  if(!utf8_isASCII($subject)) $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject).'?=';
37
38  $header  = '';
39  $header .= mail_encode_address($from,'From');
40  $header .= mail_encode_address($to,'To');
41  $header .= mail_encode_address($cc,'Cc');
42  $header .= mail_encode_address($bcc,'Bcc');
43  $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
44  $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
45  $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
46  $header .= $headers;
47  $heade   = trim($header);
48
49  $body = mail_quotedprintable_encode($body);
50
51  return @mail(null,$subject,$body,$header,$params);
52}
53
54/**
55 * Encodes an email address header
56 *
57 * Unicode chracters will be encoded quoted_printable for headers like
58 * Addresses may not contain Non-ASCII data!
59 *
60 * Example:
61 *   mail_encode_address("föö <foo@bar.com>, me@somewhere.com","To");
62 *
63 * @param string $string Multiple headers seperated by commas
64 */
65function mail_encode_address($string,$header='To'){
66  $headers = '';
67  $parts = split(',',$string);
68  foreach ($parts as $part){
69    $part = trim($part);
70
71    // parse address
72    if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
73      $text = trim($matches[1]);
74      $addr = $matches[2];
75    }else{
76      $addr = $part;
77    }
78
79    // skip empty ones
80    if(empty($addr)){
81      continue;
82    }
83
84    // FIXME: is there a way to encode the localpart of a emailaddress?
85    if(!utf8_isASCII($addr)){
86      msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
87      continue;
88    }
89
90    if(!mail_isvalid($addr)){
91      msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
92      continue;
93    }
94
95    // no text was given
96    if(empty($text)){
97      $headers .= $header.': <'.$addr.'>'.MAILHEADER_EOL;
98      continue;
99    }
100
101    // FIME: can make problems with long headers?
102    if(!utf8_isASCII($text)){
103      $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text).'?=';
104    }
105
106    //construct header
107    $headers .= $header.': '.$text.' <'.$addr.'>'.MAILHEADER_EOL;
108  }
109
110  return $headers;
111}
112
113/**
114 * Uses a regular expresion to check if a given mail address is valid
115 *
116 * May not be completly RFC conform!
117 *
118 * @link    http://www.webmasterworld.com/forum88/135.htm
119 *
120 * @param   string $email the address to check
121 * @return  bool          true if address is valid
122 */
123function mail_isvalid($email){
124  return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
125}
126
127/**
128 * Quoted printable encoding
129 *
130 * @author <pob@medienrecht.org>
131 * @author <tamas.tompa@kirowski.com>
132 * @link   http://www.php.net/manual/en/function.quoted-printable-decode.php
133 */
134function mail_quotedprintable_encode($input='',$line_max=74,$space_conv=false){
135  $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
136  $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
137  $eol = "\n";
138  $escape = "=";
139  $output = "";
140  while( list(, $line) = each($lines) ) {
141    //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
142    $linlen = strlen($line);
143    $newline = "";
144    for($i = 0; $i < $linlen; $i++) {
145      $c = substr( $line, $i, 1 );
146      $dec = ord( $c );
147      if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
148        $c = "=2E";
149      }
150      if ( $dec == 32 ) {
151        if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
152          $c = "=20";
153        } else if ( $space_conv ) {
154          $c = "=20";
155        }
156      } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
157        $h2 = floor($dec/16);
158        $h1 = floor($dec%16);
159        $c = $escape.$hex["$h2"].$hex["$h1"];
160      }
161      if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
162         $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
163         $newline = "";
164         // check if newline first character will be point or not
165         if ( $dec == 46 ) {
166            $c = "=2E";
167         }
168      }
169      $newline .= $c;
170    } // end of for
171    $output .= $newline.$eol;
172  } // end of while
173  return trim($output);
174}
175
176
177?>
178