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