summaryrefslogtreecommitdiff
path: root/inc/mail.php
blob: eeef4f66b518a024a1e0b477d0a7d6ecf87038ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
 * Mail functions
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 */

  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
  require_once(DOKU_INC.'inc/utf8.php');

  define('MAILHEADER_EOL',"\n"); //end of line for mail headers
  #define('MAILHEADER_ASCIIONLY',1);

/**
 * UTF-8 autoencoding replacement for PHPs mail function
 *
 * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
 * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
 * automatically. You can seperate receivers by commas.
 *
 * @param string $to      Receiver of the mail (multiple seperated by commas)
 * @param string $subject Mailsubject
 * @param string $body    Messagebody
 * @param string $from    Sender address 
 * @param string $cc      CarbonCopy receiver (multiple seperated by commas)
 * @param string $bcc     BlindCarbonCopy receiver (multiple seperated by commas)
 * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
 * @param string $params  Additonal Sendmail params (passed to mail())
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 * @see    mail()
 */
function mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
  if(defined('MAILHEADER_ASCIIONLY')){
    $subject = utf8_deaccent($subject);
    $subject = utf8_strip($subject);
  }

  if(!utf8_isASCII($subject))
    $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject).'?=';

  $header  = '';

  // No named recipients for To: in Windows (see FS#652)
  $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;

  $to = mail_encode_address($to,'',$usenames);
  $header .= mail_encode_address($from,'From');
  $header .= mail_encode_address($cc,'Cc');
  $header .= mail_encode_address($bcc,'Bcc');
  $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
  $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
  $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
  $header .= $headers;
  $header  = trim($header);

  $body = mail_quotedprintable_encode($body);

  if($params == null){
    return @mail($to,$subject,$body,$header);
  }else{
    return @mail($to,$subject,$body,$header,$params);
  }
}

/**
 * Encodes an email address header
 *
 * Unicode characters will be deaccented and encoded
 * quoted_printable for headers.
 * Addresses may not contain Non-ASCII data!
 *
 * Example:
 *   mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc");
 *
 * @param string  $string Multiple adresses separated by commas
 * @param string  $header Name of the header (To,Bcc,Cc,...)
 * @param boolean $names  Allow named Recipients?
 */
function mail_encode_address($string,$header='',$names=true){
  $headers = '';
  $parts = split(',',$string);
  foreach ($parts as $part){
    $part = trim($part);

    // parse address
    if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
      $text = trim($matches[1]);
      $addr = $matches[2];
    }else{
      $addr = $part;
    }

    // skip empty ones
    if(empty($addr)){
      continue;
    }

    // FIXME: is there a way to encode the localpart of a emailaddress?
    if(!utf8_isASCII($addr)){
      msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
      continue;
    }

    if(!mail_isvalid($addr)){
      msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
      continue;
    }

    // text was given
    if(!empty($text) && $names){
      // add address quotes
      $addr = "<$addr>";

      if(defined('MAILHEADER_ASCIIONLY')){
        $text = utf8_deaccent($text);
        $text = utf8_strip($text);
      }

      if(!utf8_isASCII($text)){
        $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text).'?=';
      }
    }
    
    // add to header comma seperated and in new line to avoid too long headers
    if($headers != '') $headers .= ','.MAILHEADER_EOL.' ';
    $headers .= $text.$addr;
  }

  if(empty($headers)) return null;

  //if headername was given add it and close correctly
  if($header) $headers = $header.': '.$headers.MAILHEADER_EOL;

  return $headers;
}

/**
 * Uses a regular expresion to check if a given mail address is valid
 *
 * May not be completly RFC conform!
 * 
 * @link    http://www.webmasterworld.com/forum88/135.htm
 *
 * @param   string $email the address to check
 * @return  bool          true if address is valid
 */
function mail_isvalid($email){
  return eregi("^[0-9a-z]([+-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
}

/**
 * Quoted printable encoding
 *
 * @author <pob@medienrecht.org>
 * @author <tamas.tompa@kirowski.com>
 * @link   http://www.php.net/manual/en/function.quoted-printable-decode.php
 */
function mail_quotedprintable_encode($input='',$line_max=74,$space_conv=false){
  $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
  $eol = "\n";
  $escape = "=";
  $output = "";
  while( list(, $line) = each($lines) ) {
    //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
    $linlen = strlen($line);
    $newline = "";
    for($i = 0; $i < $linlen; $i++) {
      $c = substr( $line, $i, 1 );
      $dec = ord( $c );
      if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
        $c = "=2E";
      }
      if ( $dec == 32 ) {
        if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
          $c = "=20";
        } else if ( $space_conv ) {
          $c = "=20";
        }
      } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
        $h2 = floor($dec/16);
        $h1 = floor($dec%16);
        $c = $escape.$hex["$h2"].$hex["$h1"];
      }
      if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
         $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
         $newline = "";
         // check if newline first character will be point or not
         if ( $dec == 46 ) {
            $c = "=2E";
         }
      }
      $newline .= $c;
    } // end of for
    $output .= $newline.$eol;
  } // end of while
  return trim($output);
}



//Setup VIM: ex: et ts=2 enc=utf-8 :