Merge branch 'master' of inode:/srv/git/git-kairo
[php-utility-classes.git] / include / classes / email.php-class
... / ...
CommitLineData
1<?php
2/* ***** BEGIN LICENSE BLOCK *****
3 *
4 * The contents of this file are subject to Austrian copyright reegulations
5 * ("Urheberrecht"); you may not use this file except in compliance with
6 * those laws.
7 * This contents and any derived work, if it gets distributed in any way,
8 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
9 * either express or implied.
10 *
11 * The Original Code is KaiRo's E-Mail module.
12 *
13 * The Initial Developer of the Original Code is
14 * KaiRo - Robert Kaiser.
15 * Portions created by the Initial Developer are Copyright (C) 2003-2006
16 * the Initial Developer. All Rights Reserved.
17 *
18 * Contributor(s): Robert Kaiser <kairo@kairo.at>
19 *
20 * ***** END LICENSE BLOCK ***** */
21
22class email {
23 // email PHP class
24 // class/object for creating a new mail and send it
25 //
26 // function __construct()
27 // CONSTRUCTOR
28 //
29 // private $debug_toSingleAddress
30 // address to send mail to in debug mode
31 //
32 // private $subject
33 // the mail's subject line
34 //
35 // private $sender
36 // the mail's sender (array; fields see recipients)
37 //
38 // private $replyto
39 // Reply-to address (array; fields see recipients)
40 //
41 // private $recipients
42 // array of recipients (To: line)
43 // fields: name - real name
44 // mail - email address
45 //
46 // private $cc
47 // array of CC recipients (fields like recipients)
48 //
49 // private $bcc
50 // array of BCC recipients (fields like recipients)
51 //
52 // private $headers
53 // array containing all additional headers
54 // fields: name - headers name
55 // content - header content
56 //
57 // private $content_type
58 // the mail's content type (MIME-type) [default: text/plain]
59 //
60 // private $charset
61 // the mail's charset [default: iso-8859-15]
62 //
63 // private $mailtext
64 // the main mail body
65 //
66 // private $attachments
67 // array containing all attachments
68 // fields: name - attachment name
69 // content - attachment content
70 // type - MIME type of that attachment
71 //
72 // public function setDebugAddress($debug_email)
73 // debug mode: send only to this address
74 //
75 // public function setSubject($newsubject)
76 // set subject of mail
77 //
78 // public function setSender($email, [$name])
79 // set sender of mail
80 //
81 // public function setReplyTo($email, [$name])
82 // set reply-to address
83 //
84 // public function addRecipient($email, [$name])
85 // add a recipient to the mail
86 //
87 // public function addCC($email, [$name])
88 // add a CC recipient to the mail
89 //
90 // public function addBCC($email, [$name])
91 // add a BCC recipient to the mail
92 //
93 // public function addHeader($hname, [$hcontent])
94 // add a header to the mail
95 //
96 // public function addHeaderAddress($hname, $email, [$name])
97 // add an address header to the mail, possibly with both name and mail parts
98 //
99 // public function addMailText($textpart)
100 // add some text to the mail
101 //
102 // public function addAttachment($aname, $acontent, [$atype])
103 // add an attachment to the mail, use given file name, content and MIME type (defaults to application/octet-stream)
104 //
105 // public function send()
106 // really send the mail
107 //
108 // private function mimeencode($fieldtext, [$stringescape])
109 // helper function:
110 // encode given field text, ready to be placed into an e-mail MIME header
111 // if the boolean $stringescape is true, make sure this is sent as a single word in RFC2822 context (e.g. for names)
112 // see http://www.ietf.org/rfc/rfc2822.txt for the RFC in question
113
114 private $debug_toSingleAddress = '';
115 private $subject;
116 private $sender = array();
117 private $replyto = array();
118 private $recipients = array();
119 private $cc = array();
120 private $bcc = array();
121 private $headers = array();
122 private $content_type = 'text/plain';
123 private $charset = 'iso-8859-15';
124 private $mailtext = '';
125 private $attachments = array();
126
127 function __construct() {
128 // *** constructor ***
129 }
130
131 public function setDebugAddress($debug_email) { $this->debug_toSingleAddress = $debug_email; }
132
133 public function setSubject($newsubject) { $this->subject = $newsubject; }
134
135 public function setSender($email, $name = '') { $this->sender = array('mail' => $email, 'name' => $name); }
136
137 public function setReplyTo($email, $name = '') { $this->replyto = array('mail' => $email, 'name' => $name); }
138
139 public function addRecipient($email, $name = '') {
140 $this->recipients[] = array('mail' => $email, 'name' => $name);
141 }
142
143 public function addCC($email, $name = '') {
144 $this->cc[] = array('mail' => $email, 'name' => $name);
145 }
146
147 public function addBCC($email, $name = '') {
148 $this->bcc[] = array('mail' => $email, 'name' => $name);
149 }
150
151 public function addHeader($hname, $hcontent = '') {
152 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
153 }
154
155 public function addHeaderAddress($hname, $email, $name = '') {
156 if (strlen($name)) { $hcontent = $this->mimeencode($name, true).' <'.$email.'>'; }
157 else { $hcontent = $email; }
158 $this->headers[] = array('name' => $hname, 'content' => $hcontent);
159 }
160
161 public function addMailText($textpart) { $this->mailtext .= $textpart; }
162
163 public function addAttachment($aname, $acontent, $atype = 'application/octet-stream') {
164 $this->attachments[] = array('name' => $aname, 'content' => $acontent, 'type' => $atype);
165 }
166
167 public function send() {
168 global $util;
169 $mtxt = '';
170 $hdrs = 'MIME-Version: 1.0'."\n";
171 $subj = $this->mimeencode($this->subject);
172 if (strlen($this->sender['name'])) {
173 $hdrs .= 'From: '.$this->mimeencode($this->sender['name'], true).' <'.$this->sender['mail'].'>'."\n";
174 }
175 else { $hdrs .= 'From: '.$this->sender['mail']."\n"; }
176 if (count($this->replyto)) {
177 if (strlen($this->replyto['name'])) {
178 $hdrs .= 'Reply-to: '.$this->mimeencode($this->replyto['name'], true).' <'.$this->replyto['mail'].'>'."\n";
179 }
180 else { $hdrs .= 'Reply-to: '.$this->replyto['mail']."\n"; }
181 }
182 if (count($this->recipients)) {
183 $recpt = '';
184 foreach ($this->recipients as $address) {
185 if (strlen($address['name'])) { $recpt .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
186 else { $recpt .= $address['mail'].','; }
187 }
188 $recpt = preg_replace('/,$/', '', $recpt);
189 }
190 if (count($this->cc)) {
191 $adrs = '';
192 foreach ($this->cc as $address) {
193 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
194 else { $adrs .= $address['mail'].','; }
195 }
196 $adrs = preg_replace('/,$/', '', $adrs);
197 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Cc: '.$adrs."\n";
198 }
199 if (count($this->bcc)) {
200 $adrs = '';
201 foreach ($this->bcc as $address) {
202 if (strlen($address['name'])) { $adrs .= $this->mimeencode($address['name'], true).' <'.$address['mail'].'>,'; }
203 else { $adrs .= $address['mail'].','; }
204 }
205 $adrs = preg_replace('/,$/', '', $adrs);
206 $hdrs .= (strlen($this->debug_toSingleAddress)?'X-Real-':'').'Bcc: '.$adrs."\n";
207 }
208 if (count($this->headers)) {
209 foreach ($this->headers as $header) {
210 $hdrs .= $header['name'].': '.$header['content']."\n";
211 }
212 }
213 if (count($this->attachments)) {
214 // create random boundary, 20 chars, always beginning with KaiRo ;-)
215 $boundary = 'KaiRo';
216 for ($i = 1; $i <= 15; $i++) {
217 $r = rand(0, 61);
218 if ($r < 10) { $boundary .= chr($r + 48); }
219 elseif ($r < 36) { $boundary .= chr($r + 55); }
220 elseif ($r < 62) { $boundary .= chr($r + 61); }
221 }
222 $hdrs .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'";'."\n";
223 $hdrs .= 'Content-Transfer-Encoding: 7bit'."\n";
224 $mtxt .= 'This part of the E-mail should never be seen. If'."\n";
225 $mtxt .= 'you are reading this, consider upgrading your e-mail'."\n";
226 $mtxt .= 'client to a MIME-compatible client.'."\n";
227 $mtxt .= "\n".'--'.$boundary."\n";
228 if (preg_match('|^text/|', $this->content_type)) {
229 $mtxt .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
230 }
231 else {
232 $mtxt .= 'Content-Type: '.$this->content_type."\n";
233 }
234 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n\n";
235 }
236 else {
237 if (preg_match('|^text/|', $this->content_type)) {
238 $hdrs .= 'Content-Type: '.$this->content_type.'; charset="'.$this->charset.'"'."\n";
239 }
240 else {
241 $hdrs .= 'Content-Type: '.$this->content_type."\n";
242 }
243 $hdrs .= 'Content-Transfer-Encoding: 8bit'."\n";
244 }
245 $mtxt .= stripslashes($this->mailtext);
246 if (count($this->attachments)) {
247 foreach ($this->attachments as $attach) {
248 $mtxt .= "\n".'--'.$boundary."\n";
249 $mtxt .= 'Content-Type: '.$attach['type'].'; name="'.$attach['name'].'";'."\n";
250 if (preg_match('/^(text|message)\//', $attach['type'])) {
251 $mtxt .= 'Content-Transfer-Encoding: 8bit'."\n";
252 $mtxt .= 'Content-Disposition: attachment'."\n\n";
253 $mtxt .= $attach['content'];
254 $mtxt .= "\n";
255 }
256 else {
257 $mtxt .= 'Content-Transfer-Encoding: base64'."\n";
258 $mtxt .= 'Content-Disposition: attachment'."\n\n";
259 $mtxt .= rtrim(chunk_split(base64_encode($attach['content']), 76)); ;
260 $mtxt .= "\n";
261 }
262 }
263 $mtxt .= '--'.$boundary.'--'."\n";
264 }
265
266 if (strlen($this->debug_toSingleAddress)) {
267 $hdrs .= 'X-Real-To: '.$recpt."\n";
268 $recpt = $this->debug_toSingleAddress;
269 }
270
271 //print('Subject: '.$util->htmlify($subj).'<br>'."\n");
272 //print('To: '.$util->htmlify($recpt).'<br>'."\n");
273 //print(nl2br($util->htmlify($hdrs)));
274 //print(nl2br($util->htmlify($mtxt)));
275 return mail($recpt, $subj, $mtxt, $hdrs);
276 }
277
278 private function mimeencode($fieldtext, $stringescape = false) {
279 $mText = imap_8bit($fieldtext);
280 $is_qpformat = ($mText != $fieldtext);
281 if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
282 // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
283 $mText = '"'.strtr($mText, array('"' => '\"', '\\' => '\\\\')).'"';
284 }
285 if ($is_qpformat) {
286 $mText = strtr($mText, array('_' => '=5F', ' ' => '_', '?' => '=3F'));
287 $mText = '=?'.strtoupper($this->charset).'?Q?'.$mText.'?=';
288 }
289 return $mText;
290 }
291
292
293}
294?>