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