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 $mText = imap_8bit($fieldtext);
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('"' => '\"', '\\' => '\\\\')).'"';
325 $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));
326 $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';