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