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