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