KaiRo bug 401 - Convert UA tests to using ExtendedDocument, also updating the used...
[php-utility-classes.git] / classes / email.php-class
CommitLineData
0c81b5b3 1<?php
880bcb60
RK
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/. */
4f96c398 5
0c81b5b3 6class email {
7 // email PHP class
8 // class/object for creating a new mail and send it
9 //
3077a4f6 10 // function __construct()
0c81b5b3 11 // CONSTRUCTOR
12 //
3077a4f6 13 // private $debug_toSingleAddress
0c81b5b3 14 // address to send mail to in debug mode
15 //
3077a4f6 16 // private $subject
0c81b5b3 17 // the mail's subject line
18 //
3077a4f6 19 // private $sender
0c81b5b3 20 // the mail's sender (array; fields see recipients)
21 //
3077a4f6 22 // private $replyto
0c81b5b3 23 // Reply-to address (array; fields see recipients)
24 //
3077a4f6 25 // private $recipients
0c81b5b3 26 // array of recipients (To: line)
27 // fields: name - real name
28 // mail - email address
29 //
3077a4f6 30 // private $cc
0c81b5b3 31 // array of CC recipients (fields like recipients)
32 //
3077a4f6 33 // private $bcc
0c81b5b3 34 // array of BCC recipients (fields like recipients)
35 //
3077a4f6 36 // private $headers
0c81b5b3 37 // array containing all additional headers
38 // fields: name - headers name
39 // content - header content
40 //
3077a4f6 41 // private $content_type
0c81b5b3 42 // the mail's content type (MIME-type) [default: text/plain]
43 //
3077a4f6 44 // private $charset
0c81b5b3 45 // the mail's charset [default: iso-8859-15]
46 //
3077a4f6 47 // private $mailtext
0c81b5b3 48 // the main mail body
49 //
3077a4f6 50 // private $attachments
0c81b5b3 51 // array containing all attachments
52 // fields: name - attachment name
53 // content - attachment content
54 // type - MIME type of that attachment
55 //
3077a4f6 56 // public function setDebugAddress($debug_email)
0c81b5b3 57 // debug mode: send only to this address
58 //
3077a4f6 59 // public function setSubject($newsubject)
0c81b5b3 60 // set subject of mail
61 //
3077a4f6 62 // public function setSender($email, [$name])
0c81b5b3 63 // set sender of mail
64 //
3077a4f6 65 // public function setReplyTo($email, [$name])
0c81b5b3 66 // set reply-to address
67 //
3077a4f6 68 // public function addRecipient($email, [$name])
0c81b5b3 69 // add a recipient to the mail
70 //
3077a4f6 71 // public function addCC($email, [$name])
0c81b5b3 72 // add a CC recipient to the mail
73 //
3077a4f6 74 // public function addBCC($email, [$name])
0c81b5b3 75 // add a BCC recipient to the mail
76 //
3077a4f6 77 // public function addHeader($hname, [$hcontent])
0c81b5b3 78 // add a header to the mail
79 //
025016ca 80 // public function addHeaderAddress($hname, $email, [$name])
81 // add an address header to the mail, possibly with both name and mail parts
82 //
3aa9a02f
RK
83 // public function setCharset($newcharset)
84 // set charset for this mail
85 //
3077a4f6 86 // public function addMailText($textpart)
0c81b5b3 87 // add some text to the mail
88 //
3077a4f6 89 // public function addAttachment($aname, $acontent, [$atype])
d3f5b37c 90 // add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream)
91 //
a04c09ff
RK
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
97 //
3077a4f6 98 // public function send()
0c81b5b3 99 // really send the mail
100 //
4edf094e 101 // private function mimeencode($fieldtext, [$stringescape])
0c81b5b3 102 // helper function:
4edf094e 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
0c81b5b3 106
3077a4f6 107 private $debug_toSingleAddress = '';
108 private $subject;
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();
0c81b5b3 119
3077a4f6 120 function __construct() {
0c81b5b3 121 // *** constructor ***
122 }
123
3077a4f6 124 public function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
0c81b5b3 125
3077a4f6 126 public function setSubject($newsubject) { $this->subject = $newsubject; }
0c81b5b3 127
3077a4f6 128 public function setSender($email, $name = '') { $this->sender = array('mail' => $email, 'name' => $name); }
0c81b5b3 129
3077a4f6 130 public function setReplyTo($email, $name = '') { $this->replyto = array('mail' => $email, 'name' => $name); }
0c81b5b3 131
3077a4f6 132 public function addRecipient($email, $name = '') {
1d38ad3e 133 $this->recipients[] = array('mail' => $email, 'name' => $name);
0c81b5b3 134 }
135
3077a4f6 136 public function addCC($email, $name = '') {
1d38ad3e 137 $this->cc[] = array('mail' => $email, 'name' => $name);
0c81b5b3 138 }
139
3077a4f6 140 public function addBCC($email, $name = '') {
1d38ad3e 141 $this->bcc[] = array('mail' => $email, 'name' => $name);
0c81b5b3 142 }
143
3077a4f6 144 public function addHeader($hname, $hcontent = '') {
1d38ad3e 145 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
025016ca 146 }
147
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);
0c81b5b3 152 }
153
3aa9a02f
RK
154 public function setCharset($newcharset) { $this->charset = $newcharset; }
155
3077a4f6 156 public function addMailText($textpart) { $this->mailtext .= $textpart; }
0c81b5b3 157
3077a4f6 158 public function addAttachment($aname, $acontent, $atype = 'application/octet-stream') {
1d38ad3e 159 $this->attachments[] = array('name' => $aname, 'content' => $acontent, 'type' => $atype);
0c81b5b3 160 }
161
a04c09ff
RK
162 public function getAddresses($addrtype = null) {
163 // returns all addresses this mail gets sent to
43bc07b3
RK
164 if (!is_array($addrtype)) {
165 if (strlen($addrtype)) { $addrtype = explode(',', strtolower($addrtype)); }
166 else { $addrtype = array('to','cc','bcc'); }
167 }
a04c09ff
RK
168 $mailaddresses = array();
169
43bc07b3 170 if (in_array('to', $addrtype)) {
a04c09ff
RK
171 foreach ($this->recipients as $address) {
172 if (strlen(@$address['mail'])) {
173 $mailaddresses[] = array('mail'=>$address['mail'],
174 'name'=>strlen($address['name'])?$address['name']:'',
175 'addrtype'=>'to');
176 }
177 }
178 }
43bc07b3 179 if (in_array('cc', $addrtype)) {
a04c09ff
RK
180 foreach ($this->cc as $address) {
181 if (strlen(@$address['mail'])) {
182 $mailaddresses[] = array('mail'=>$address['mail'],
183 'name'=>strlen($address['name'])?$address['name']:'',
184 'addrtype'=>'cc');
185 }
186 }
187 }
43bc07b3 188 if (in_array('bcc', $addrtype)) {
a04c09ff
RK
189 foreach ($this->bcc as $address) {
190 if (strlen(@$address['mail'])) {
191 $mailaddresses[] = array('mail'=>$address['mail'],
192 'name'=>strlen($address['name'])?$address['name']:'',
193 'addrtype'=>'bcc');
194 }
195 }
196 }
43bc07b3
RK
197
198 return $mailaddresses;
a04c09ff
RK
199 }
200
3077a4f6 201 public function send() {
0c81b5b3 202 global $util;
1d38ad3e 203 $mtxt = '';
204 $hdrs = 'MIME-Version: 1.0'."\n";
0c81b5b3 205 $subj = $this->mimeencode($this->subject);
1d38ad3e 206 if (strlen($this->sender['name'])) {
4edf094e 207 $hdrs .= 'From: '.$this->mimeencode($this->sender['name'], true).' <'.$this->sender['mail'].'>'."\n";
0c81b5b3 208 }
1d38ad3e 209 else { $hdrs .= 'From: '.$this->sender['mail']."\n"; }
0c81b5b3 210 if (count($this->replyto)) {
1d38ad3e 211 if (strlen($this->replyto['name'])) {
4edf094e 212 $hdrs .= 'Reply-to: '.$this->mimeencode($this->replyto['name'], true).' <'.$this->replyto['mail'].'>'."\n";
0c81b5b3 213 }
1d38ad3e 214 else { $hdrs .= 'Reply-to: '.$this->replyto['mail']."\n"; }
0c81b5b3 215 }
216 if (count($this->recipients)) {
1d38ad3e 217 $recpt = '';
0c81b5b3 218 foreach ($this->recipients as $address) {
ba79b48c
RK
219 if (strlen(@$address['mail'])) {
220 if (strlen($address['name'])) { $recpt .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
221 else { $recpt .= $address['mail'].','; }
222 }
0c81b5b3 223 }
1d38ad3e 224 $recpt = preg_replace('/,$/', '', $recpt);
0c81b5b3 225 }
ba79b48c
RK
226 if (!strlen($recpt)) {
227 return null;
228 }
0c81b5b3 229 if (count($this->cc)) {
1d38ad3e 230 $adrs = '';
0c81b5b3 231 foreach ($this->cc as $address) {
4edf094e 232 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
1d38ad3e 233 else { $adrs .= $address['mail'].','; }
0c81b5b3 234 }
1d38ad3e 235 $adrs = preg_replace('/,$/', '', $adrs);
236 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Cc: '.$adrs."\n";
0c81b5b3 237 }
238 if (count($this->bcc)) {
1d38ad3e 239 $adrs = '';
0c81b5b3 240 foreach ($this->bcc as $address) {
4edf094e 241 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
1d38ad3e 242 else { $adrs .= $address['mail'].','; }
0c81b5b3 243 }
1d38ad3e 244 $adrs = preg_replace('/,$/', '', $adrs);
245 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Bcc: '.$adrs."\n";
0c81b5b3 246 }
247 if (count($this->headers)) {
248 foreach ($this->headers as $header) {
1d38ad3e 249 $hdrs .= $header['name'].': '.$header['content']."\n";
0c81b5b3 250 }
251 }
252 if (count($this->attachments)) {
269bd147 253 // create random boundary, 20 chars, always beginning with KaiRo ;-)
254 $boundary = 'KaiRo';
1d38ad3e 255 for ($i = 1; $i <= 15; $i++) {
256 $r = rand(0, 61);
257 if ($r < 10) { $boundary .= chr($r + 48); }
258 elseif ($r < 36) { $boundary .= chr($r + 55); }
259 elseif ($r < 62) { $boundary .= chr($r + 61); }
269bd147 260 }
1d38ad3e 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";
0c81b5b3 269 }
270 else {
1d38ad3e 271 $mtxt .= 'Content-Type: '.$this->content_type."\n";
0c81b5b3 272 }
1d38ad3e 273 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n\n";
0c81b5b3 274 }
275 else {
1d38ad3e 276 if (preg_match('|^text/|', $this->content_type)) {
277 $hdrs .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
0c81b5b3 278 }
279 else {
1d38ad3e 280 $hdrs .= 'Content-Type: '.$this->content_type."\n";
0c81b5b3 281 }
1d38ad3e 282 $hdrs .= 'Content-Transfer-Encoding: 8bit'."\n";
0c81b5b3 283 }
284 $mtxt .= stripslashes($this->mailtext);
285 if (count($this->attachments)) {
286 foreach ($this->attachments as $attach) {
1d38ad3e 287 $mtxt .= "\n".'--'.$boundary."\n";
288 $mtxt .= 'Content-Type: '.$attach['type'].'; name="'.$attach['name'].'";'."\n";
1b1735d1 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'];
293 $mtxt .= "\n";
294 }
295 else {
296 $mtxt .= 'Content-Transfer-Encoding: base64'."\n";
297 $mtxt .= 'Content-Disposition: attachment'."\n\n";
298 $mtxt .= rtrim(chunk_split(base64_encode($attach['content']), 76)); ;
299 $mtxt .= "\n";
300 }
0c81b5b3 301 }
1b1735d1 302 $mtxt .= '--'.$boundary.'--'."\n";
0c81b5b3 303 }
304
305 if (strlen($this->debug_toSingleAddress)) {
1d38ad3e 306 $hdrs .= 'X-Real-To: '.$recpt."\n";
0c81b5b3 307 $recpt = $this->debug_toSingleAddress;
308 }
309
1d38ad3e 310 //print('Subject: '.$util->htmlify($subj).'<br>'."\n");
311 //print('To: '.$util->htmlify($recpt).'<br>'."\n");
0c81b5b3 312 //print(nl2br($util->htmlify($hdrs)));
313 //print(nl2br($util->htmlify($mtxt)));
61432682 314 return mail($recpt, $subj, $mtxt, $hdrs);
0c81b5b3 315 }
316
4edf094e 317 private function mimeencode($fieldtext, $stringescape = false) {
0c81b5b3 318 $mText = imap_8bit($fieldtext);
4edf094e 319 $is_qpformat = ($mText != $fieldtext);
320 if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
321 // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
322 $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"';
323 }
324 if ($is_qpformat) {
325 $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));
1d38ad3e 326 $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';
0c81b5b3 327 }
328 return $mText;
329 }
0c81b5b3 330}
331?>