9 $this->setHeaderCharset('UTF-8');
10 $this->setBodyCharset('UTF-8');
11 $this->setMimeType('text/plain');
12 $this->setTo('', true);
13 $this->setBcc('', true);
14 $this->setCc('', true);
15 $this->setBody('', true);
16 $this->clearAdditionalHeaders();
20 function setHeaderCharset($charset) {
21 $this->_headerCharset = $charset;
23 function getHeaderCharset() {
24 return $this->_headerCharset;
28 function setBodyCharset($charset) {
29 $this->_bodyCharset = $charset;
31 function getBodyCharset() {
32 return $this->_bodyCharset;
36 function setSubject($subject) {
37 $this->_subject = $subject;
39 function getSubject() {
40 return $this->_subject;
42 function getEncodedSubject() {
43 return $this->_encodeHeader($this->_subject, $this->getHeaderCharset());
47 * Function to encode a header if necessary
48 * according to RFC2047
49 * Filename.......: class.html.mime.mail.inc
50 * Project........: HTML Mime mail class
51 * Last Modified..: Date: 2002/07/24 13:14:10
52 * CVS Revision...: Revision: 1.4
53 * Copyright......: 2001, 2002 Richard Heyes
55 function _encodeHeader($input, $charset) {
56 preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
57 foreach ($matches[1] as $value) {
58 $replacement = preg_replace('/([\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
59 $input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
66 * Given a header, this function will decode it
67 * according to RFC2047. Probably not *exactly*
68 * conformant, but it does pass all the given
69 * examples (in RFC2047).
71 * @param string Input header value to decode
72 * @return string Decoded header value
75 function _decodeHeader($input)
77 // Remove white space between encoded-words
78 $input = preg_replace('/(=\?[^?]+\?(q|b)\?[^?]*\?=)(\s)+=\?/i', '\1=?', $input);
80 // For each encoded-word...
81 while (preg_match('/(=\?([^?]+)\?(q|b)\?([^?]*)\?=)/i', $input, $matches)) {
83 $encoded = $matches[1];
84 $charset = $matches[2];
85 $encoding = $matches[3];
88 switch (strtolower($encoding)) {
90 $text = base64_decode($text);
94 $text = str_replace('_', ' ', $text);
95 preg_match_all('/=([a-f0-9]{2})/i', $text, $matches);
96 foreach($matches[1] as $value)
97 $text = str_replace('='.$value, chr(hexdec($value)), $text);
101 $input = str_replace($encoded, $text, $input);
108 function setBody($body) {
109 $this->_body = $body;
116 function setFrom($from) {
117 $this->_from = $this->_validateRecipient($from);
124 * Check if given mail is a valid (Ie. Active or Restricted) user.
126 * The given mail can by both user_name or email. Return form is always the
129 * @param $list (IN) list of email addresses separated by , or ;
130 * @return list of email separated by ,
132 function _validateRecipient($list) {
133 $recipArray = split('[;,]', $list);
135 foreach($recipArray as $email) {
136 $email = trim($email);
138 $user = UserManager::instance()->getUserByEmail($email);
140 $allowed_status = array('A', 'R', 'P', 'V', 'W');
141 $one_with_status_allowed_found = false;
142 while ( !$one_with_status_allowed_found) {
143 if (in_array($user->getStatus(), $allowed_status)) {
144 $retArray[] = '"'.$this->_encodeHeader($user->getRealName(), $this->getHeaderCharset()).'" <'.$user->getEmail().'>';
145 $one_with_status_allowed_found = true;
149 $retArray[] = $email;
153 return implode(', ', $retArray);
157 function setTo($to, $raw=false) {
161 $this->_to = $this->_validateRecipient($to);
168 function setBcc($bcc, $raw=false) {
172 $this->_bcc = $this->_validateRecipient($bcc);
179 function setCc($cc, $raw=false) {
183 $this->_cc = $this->_validateRecipient($cc);
191 function setMimeType($mimeType) {
192 $this->_mimeType = $mimeType;
194 function getMimeType() {
195 return $this->_mimeType;
198 var $_additionalHeaders;
199 function clearAdditionalHeaders() {
200 $this->_additionalHeaders = array();
202 function addAdditionalHeader($name, $value) {
203 $this->_additionalHeaders[$name] = $value;
205 function removeAdditionalHeader($name) {
206 if (isset($this->_additionalHeaders[$name])) {
207 unset($this->_additionalHeaders[$name]);
212 * @returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
213 * It is important to note that just because the mail was accepted for delivery,
214 * it does NOT mean the mail will actually reach the intended destination.
218 if($this->getTo() === ''
219 && $this->getCc() === ''
220 && $this->getBcc() === '') {
224 $header = "From: ".$this->getFrom().$sys_lf;
225 $header .= "Content-type: ".$this->getMimeType()."; charset=".$this->getBodyCharset().$sys_lf;
226 $cc = $this->getCc();
227 if (strlen($cc) > 0) {
228 $header .= "Cc: ".$cc.$sys_lf;
230 $bcc = $this->getBcc();
231 if (strlen($bcc) > 0) {
232 $header .= "Bcc: ".$bcc.$sys_lf;
234 foreach($this->_additionalHeaders as $name => $value) {
235 $header .= $name.": ".$value.$sys_lf;
237 return $this->_sendmail($header);
241 * Perform effective email send.
244 function _sendmail($header) {
245 $params = array('mail' => $this,
246 'header' => $header);
247 //$em =& EventManager::instance();
248 //$em->processEvent('mail_sendmail', $params);
250 return mail($this->getTo(),
251 $this->getEncodedSubject(),