3 * Copyright 2005-2012, Codendi Team
4 * Copyright 2013, Franck Villaume - TrivialDev
6 * This file is part of FusionForge. FusionForge is free software;
7 * you can redistribute it and/or modify it under the terms of the
8 * GNU General Public License as published by the Free Software
9 * Foundation; either version 2 of the Licence, or (at your option)
12 * FusionForge is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 require_once $gfcommon.'include/utils.php';
35 var $_additionalHeaders;
37 function __construct() {
38 $charset = _('UTF-8');
42 $this->setHeaderCharset($charset);
43 $this->setBodyCharset($charset);
44 $this->setMimeType('text/plain');
45 $this->setTo('', true);
46 $this->setBcc('', true);
47 $this->setCc('', true);
48 $this->setBody('', true);
49 $this->clearAdditionalHeaders();
52 function setHeaderCharset($charset) {
53 $this->_headerCharset = $charset;
56 function getHeaderCharset() {
57 return $this->_headerCharset;
60 function setBodyCharset($charset) {
61 $this->_bodyCharset = $charset;
64 function getBodyCharset() {
65 return $this->_bodyCharset;
68 function setSubject($subject) {
69 $this->_subject = $subject;
72 function getSubject() {
73 return $this->_subject;
76 function getEncodedSubject() {
77 return $this->_encodeHeader($this->_subject, $this->getHeaderCharset());
81 * Function to encode a header if necessary
82 * according to RFC2047
83 * Filename.......: class.html.mime.mail.inc
84 * Project........: HTML Mime mail class
85 * Last Modified..: Date: 2002/07/24 13:14:10
86 * CVS Revision...: Revision: 1.4
87 * Copyright......: 2001, 2002 Richard Heyes
89 * @param string $input
90 * @param string $charset
93 function _encodeHeader($input, $charset) {
94 preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
95 foreach ($matches[1] as $value) {
96 $replacement = preg_replace('/([\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
97 $input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
102 function setBody($body) {
103 $this->_body = $body;
110 function getEncodedBody() {
111 return util_convert_body($this->getBody(), $this->getBodyCharset());
114 function setFrom($from) {
115 $this->_from = $this->_validateRecipient($from);
120 return 'noreply@'.forge_get_config('web_host');
126 * Check if given mail is a valid (Ie. Active or Restricted) user.
128 * The given mail can by both user_name or email. Return form is always the
131 * @param $list (IN) list of email addresses separated by , or ;
132 * @return string list of email separated by ,
134 function _validateRecipient($list) {
135 $recipArray = preg_split('/[;,]/', $list);
137 foreach($recipArray as $email) {
138 $email = trim($email);
140 $user = user_get_object_by_email($email);
142 $allowed_status = array('A', 'R', 'P', 'V', 'W');
143 $one_with_status_allowed_found = false;
144 while ( !$one_with_status_allowed_found) {
145 if (in_array($user->getStatus(), $allowed_status)) {
146 $retArray[] = '"'.$this->_encodeHeader($user->getRealName(), $this->getHeaderCharset()).'" <'.$user->getEmail().'>';
147 $one_with_status_allowed_found = true;
151 if (validate_email($email)) {
152 $retArray[] = $email;
157 return implode(', ', $retArray);
161 function setTo($to, $raw = false) {
165 $this->_to = $this->_validateRecipient($to);
171 return 'noreply@'.forge_get_config('web_host');
176 function setBcc($bcc, $raw = false) {
180 $this->_bcc = $this->_validateRecipient($bcc);
183 if (forge_get_config('bcc_all_emails') != '') {
184 $this->addBcc(forge_get_config('bcc_all_emails'));
188 function addBcc($addbcc, $raw = false) {
190 $this->_bcc .= ', '.$addbcc;
192 $_addbcc = $this->_validateRecipient($addbcc);
193 if (strlen($_addbcc)) {
194 $this->_bcc .= ', '.$_addbcc;
203 function setCc($cc, $raw = false) {
207 $this->_cc = $this->_validateRecipient($cc);
215 function setMimeType($mimeType) {
216 $this->_mimeType = $mimeType;
219 function getMimeType() {
220 return $this->_mimeType;
223 function clearAdditionalHeaders() {
224 $this->_additionalHeaders = array();
227 function addAdditionalHeader($name, $value) {
228 $this->_additionalHeaders[$name] = $value;
231 function removeAdditionalHeader($name) {
232 if (isset($this->_additionalHeaders[$name])) {
233 unset($this->_additionalHeaders[$name]);
238 * @returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
239 * It is important to note that just because the mail was accepted for delivery,
240 * it does NOT mean the mail will actually reach the intended destination.
245 $mail = "To: ".$this->getTo().$sys_lf;
246 $mail .= "From: ".$this->getFrom().$sys_lf;
247 $mail .= "Content-type: ".$this->getMimeType()."; charset=".$this->getBodyCharset().$sys_lf;
248 $mail .= "Content-Transfer-Encoding: 8bit".$sys_lf;
249 $cc = $this->getCc();
250 if (strlen($cc) > 0) {
251 $mail .= "Cc: ".$cc.$sys_lf;
253 $bcc = $this->getBcc();
254 if (strlen($bcc) > 0) {
255 $mail .= "Bcc: ".$bcc.$sys_lf;
257 foreach($this->_additionalHeaders as $name => $value) {
258 $mail .= $name.": ".$value.$sys_lf;
260 $mail .= $this->getEncodedSubject().$sys_lf;
261 $mail .= $this->getEncodedBody().$sys_lf;
262 return $this->_sendmail($mail);
266 * Perform effective email send.
269 * @param string $mail
272 function _sendmail($mail) {
273 $from = $this->getFrom();
274 $handle = popen(forge_get_config('sendmail_path')." -f'$from' -t -i", 'w');
275 fwrite($handle, $mail);