2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
8 // class/object for creating a new mail and send it
10 // function __construct()
13 // private $debug_toSingleAddress
14 // address to send mail to in debug mode
17 // the mail's subject line
20 // the mail's sender (array; fields see recipients)
23 // Reply-to address (array; fields see recipients)
25 // private $recipients
26 // array of recipients (To: line)
27 // fields: name - real name
28 // mail - email address
31 // array of CC recipients (fields like recipients)
34 // array of BCC recipients (fields like recipients)
37 // array containing all additional headers
38 // fields: name - headers name
39 // content - header content
41 // private $content_type
42 // the mail's content type (MIME-type) [default: text/plain]
45 // the mail's charset [default: iso-8859-15]
50 // private $attachments
51 // array containing all attachments
52 // fields: name - attachment name
53 // content - attachment content
54 // type - MIME type of that attachment
56 // public function setDebugAddress($debug_email)
57 // debug mode: send only to this address
59 // public function setSubject($newsubject)
60 // set subject of mail
62 // public function setSender($email, [$name])
65 // public function setReplyTo($email, [$name])
66 // set reply-to address
68 // public function addRecipient($email, [$name])
69 // add a recipient to the mail
71 // public function addCC($email, [$name])
72 // add a CC recipient to the mail
74 // public function addBCC($email, [$name])
75 // add a BCC recipient to the mail
77 // public function addHeader($hname, [$hcontent])
78 // add a header to the mail
80 // public function addHeaderAddress($hname, $email, [$name])
81 // add an address header to the mail, possibly with both name and mail parts
83 // public function setCharset($newcharset)
84 // set charset for this mail
86 // public function addMailText($textpart)
87 // add some text to the mail
89 // public function addAttachment($aname, $acontent, [$atype])
90 // add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream)
92 // public function getAddresses([$addrtype])
93 // returns an array of all addresses this mail gets sent to
94 // fields: email, name, addrtype
95 // addrtype is one of to/cc/bcc
96 // the $addrtype parameter is a comma-separated list of such types, default: all of them
98 // public function send()
99 // really send the mail
101 // private function mimeencode($fieldtext, [$stringescape])
103 // encode given field text, ready to be placed into an e-mail MIME header
104 // if the boolean $stringescape is true, make sure this is sent as a single word in RFC2822 context (e.g. for names)
105 // see http://www.ietf.org/rfc/rfc2822.txt for the RFC in question
107 private $debug_toSingleAddress = '';
109 private $sender = array();
110 private $replyto = array();
111 private $recipients = array();
112 private $cc = array();
113 private $bcc = array();
114 private $headers = array();
115 private $content_type = 'text/plain';
116 private $charset = 'iso-8859-15';
117 private $mailtext = '';
118 private $attachments = array();
120 function __construct() {
121 // *** constructor ***
124 public function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
126 public function setSubject($newsubject) { $this->subject = $newsubject; }
128 public function setSender($email, $name = '') { $this->sender = array('mail' => $email, 'name' => $name); }
130 public function setReplyTo($email, $name = '') { $this->replyto = array('mail' => $email, 'name' => $name); }
132 public function addRecipient($email, $name = '') {
133 $this->recipients[] = array('mail' => $email, 'name' => $name);
136 public function addCC($email, $name = '') {
137 $this->cc[] = array('mail' => $email, 'name' => $name);
140 public function addBCC($email, $name = '') {
141 $this->bcc[] = array('mail' => $email, 'name' => $name);
144 public function addHeader($hname, $hcontent = '') {
145 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
148 public function addHeaderAddress($hname, $email, $name = '') {
149 if (strlen($name)) { $hcontent = $this->mimeencode($name, true).' <'.$email.'>'; }
150 else { $hcontent = $email; }
151 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
154 public function setCharset($newcharset) { $this->charset = $newcharset; }
156 public function addMailText($textpart) { $this->mailtext .= $textpart; }
158 public function addAttachment($aname, $acontent, $atype = 'application/octet-stream') {
159 $this->attachments[] = array('name' => $aname, 'content' => $acontent, 'type' => $atype);
162 public function getAddresses($addrtype = null) {
163 // returns all addresses this mail gets sent to
164 if (!is_array($addrtype)) {
165 if (strlen($addrtype)) { $addrtype = explode(',', strtolower($addrtype)); }
166 else { $addrtype = array('to','cc','bcc'); }
168 $mailaddresses = array();
170 if (in_array('to', $addrtype)) {
171 foreach ($this->recipients as $address) {
172 if (strlen($address['mail'] ?? '')) {
173 $mailaddresses[] = array('mail'=>$address['mail'],
174 'name'=>strlen($address['name'])?$address['name']:'',
179 if (in_array('cc', $addrtype)) {
180 foreach ($this->cc as $address) {
181 if (strlen($address['mail'] ?? '')) {
182 $mailaddresses[] = array('mail'=>$address['mail'],
183 'name'=>strlen($address['name'])?$address['name']:'',
188 if (in_array('bcc', $addrtype)) {
189 foreach ($this->bcc as $address) {
190 if (strlen($address['mail'] ?? '')) {
191 $mailaddresses[] = array('mail'=>$address['mail'],
192 'name'=>strlen($address['name'])?$address['name']:'',
198 return $mailaddresses;
201 public function send() {
204 $hdrs = 'MIME-Version: 1.0'."\n";
205 $subj = $this->mimeencode($this->subject);
206 if (strlen($this->sender['name'])) {
207 $hdrs .= 'From: '.$this->mimeencode($this->sender['name'], true).' <'.$this->sender['mail'].'>'."\n";
209 else { $hdrs .= 'From: '.$this->sender['mail']."\n"; }
210 if (count($this->replyto)) {
211 if (strlen($this->replyto['name'])) {
212 $hdrs .= 'Reply-to: '.$this->mimeencode($this->replyto['name'], true).' <'.$this->replyto['mail'].'>'."\n";
214 else { $hdrs .= 'Reply-to: '.$this->replyto['mail']."\n"; }
216 if (count($this->recipients)) {
218 foreach ($this->recipients as $address) {
219 if (strlen($address['mail'] ?? '')) {
220 if (strlen($address['name'])) { $recpt .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
221 else { $recpt .= $address['mail'].','; }
224 $recpt = preg_replace('/,$/', '', $recpt);
226 if (!strlen($recpt)) {
229 if (count($this->cc)) {
231 foreach ($this->cc as $address) {
232 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
233 else { $adrs .= $address['mail'].','; }
235 $adrs = preg_replace('/,$/', '', $adrs);
236 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Cc: '.$adrs."\n";
238 if (count($this->bcc)) {
240 foreach ($this->bcc as $address) {
241 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
242 else { $adrs .= $address['mail'].','; }
244 $adrs = preg_replace('/,$/', '', $adrs);
245 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Bcc: '.$adrs."\n";
247 if (count($this->headers)) {
248 foreach ($this->headers as $header) {
249 $hdrs .= $header['name'].': '.$header['content']."\n";
252 if (count($this->attachments)) {
253 // create random boundary, 20 chars, always beginning with KaiRo ;-)
255 for ($i = 1; $i <= 15; $i++) {
257 if ($r < 10) { $boundary .= chr($r + 48); }
258 elseif ($r < 36) { $boundary .= chr($r + 55); }
259 elseif ($r < 62) { $boundary .= chr($r + 61); }
261 $hdrs .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'";'."\n";
262 $hdrs .= 'Content-Transfer-Encoding: 7bit'."\n";
263 $mtxt .= 'This part of the E-mail should never be seen. If'."\n";
264 $mtxt .= 'you are reading this, consider upgrading your e-mail'."\n";
265 $mtxt .= 'client to a MIME-compatible client.'."\n";
266 $mtxt .= "\n".'--'.$boundary."\n";
267 if (preg_match('|^text/|', $this->content_type)) {
268 $mtxt .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
271 $mtxt .= 'Content-Type: '.$this->content_type."\n";
273 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n\n";
276 if (preg_match('|^text/|', $this->content_type)) {
277 $hdrs .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
280 $hdrs .= 'Content-Type: '.$this->content_type."\n";
282 $hdrs .= 'Content-Transfer-Encoding: 8bit'."\n";
284 $mtxt .= stripslashes($this->mailtext);
285 if (count($this->attachments)) {
286 foreach ($this->attachments as $attach) {
287 $mtxt .= "\n".'--'.$boundary."\n";
288 $mtxt .= 'Content-Type: '.$attach['type'].'; name="'.$attach['name'].'";'."\n";
289 if (preg_match('/^(text|message)\//', $attach['type'])) {
290 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n";
291 $mtxt .= 'Content-Disposition: attachment'."\n\n";
292 $mtxt .= $attach['content'];
296 $mtxt .= 'Content-Transfer-Encoding: base64'."\n";
297 $mtxt .= 'Content-Disposition: attachment'."\n\n";
298 $mtxt .= rtrim(chunk_split(base64_encode($attach['content']), 76)); ;
302 $mtxt .= '--'.$boundary.'--'."\n";
305 if (strlen($this->debug_toSingleAddress)) {
306 $hdrs .= 'X-Real-To: '.$recpt."\n";
307 $recpt = $this->debug_toSingleAddress;
310 //print('Subject: '.$util->htmlify($subj).'<br>'."\n");
311 //print('To: '.$util->htmlify($recpt).'<br>'."\n");
312 //print(nl2br($util->htmlify($hdrs)));
313 //print(nl2br($util->htmlify($mtxt)));
314 return mail($recpt, $subj, $mtxt, $hdrs);
317 private function mimeencode($fieldtext, $stringescape = false) {
318 if (function_exists('imap_8bit')) {
319 $mText = imap_8bit($fieldtext);
322 $mText = quoted_printable_encode($fieldtext);
324 $is_qpformat = ($mText != $fieldtext);
325 if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
326 // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
327 $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"';
330 $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));
331 $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';
336 private function quoted_printable_encode($sText, $bEmulate_imap_8bit=true) {
337 /* by ...deed.ztinmehc-ut.zrh@umuumu@hrz.tu-chemnitz.deed...
338 from https://secure.php.net/manual/en/function.imap-8bit.php#61216
340 I use the following function instead of imap_8bit
341 when using PHP without the IMAP module,
342 which is based on code found in
343 http://www.php.net/quoted_printable_decode,
344 and giving (supposedly) exactly the same results as imap_8bit,
345 (tested on thousands of random strings containing lots
346 of spaces, tabs, crlf, lfcr, lf, cr and so on,
347 no counterexample found SO FAR:)
349 AND you can force a trailing space to be encoded,
350 as opposed to what imap_8bit does,
351 which I consider is a violation of RFC2045,
352 (see http://bugs.php.net/bug.php?id=35290)
353 by commenting that one central line.
355 // split text into lines
356 $aLines=explode(chr(13).chr(10),$sText);
358 for ($i=0;$i<count($aLines);$i++) {
359 $sLine =& $aLines[$i];
360 if (strlen($sLine)===0) continue; // do nothing, if empty
362 $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
364 // imap_8bit encodes x09 everywhere, not only at lineends,
365 // for EBCDIC safeness encode !"#$@[\]^`{|}~,
366 // for complete safeness encode every character :)
367 if ($bEmulate_imap_8bit)
368 $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
370 $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
371 $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
373 // encode x09,x20 at lineends
375 $iLength = strlen($sLine);
376 $iLastChar = ord($sLine[$iLength-1]);
379 // imap_8_bit does not encode x20 at the very end of a text,
380 // here is, where I don't agree with imap_8_bit,
381 // please correct me, if I'm wrong,
382 // or comment next line for RFC2045 conformance, if you like
383 if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
385 if (($iLastChar==0x09)||($iLastChar==0x20)) {
386 $sLine[$iLength-1]='=';
387 $sLine .= ($iLastChar==0x09)?'09':'20';
389 } // imap_8bit encodes x20 before chr(13), too
390 // although IMHO not requested by RFC2045, why not do it safer :)
391 // and why not encode any x20 around chr(10) or chr(13)
392 if ($bEmulate_imap_8bit) {
393 $sLine=str_replace(' =0D','=20=0D',$sLine);
394 //$sLine=str_replace(' =0A','=20=0A',$sLine);
395 //$sLine=str_replace('=0D ','=0D=20',$sLine);
396 //$sLine=str_replace('=0A ','=0A=20',$sLine);
399 // finally split into softlines no longer than 76 chars,
400 // for even more safeness one could encode x09,x20
401 // at the very first character of the line
402 // and after soft linebreaks, as well,
403 // but this wouldn't be caught by such an easy RegExp
404 preg_match_all( '/.{1,73}([^=]{0,2})?/', $sLine, $aMatch );
405 $sLine = implode( '=' . chr(13).chr(10), $aMatch[0] ); // add soft crlf's
408 // join lines into text
409 return implode(chr(13).chr(10),$aLines);