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;
61 function setBodyCharset($charset) {
62 $this->_bodyCharset = $charset;
65 function getBodyCharset() {
66 return $this->_bodyCharset;
69 function setSubject($subject) {
70 $this->_subject = $subject;
73 function getSubject() {
74 return $this->_subject;
77 function getEncodedSubject() {
78 return $this->_encodeHeader($this->_subject, $this->getHeaderCharset());
82 * Function to encode a header if necessary
83 * according to RFC2047
84 * Filename.......: class.html.mime.mail.inc
85 * Project........: HTML Mime mail class
86 * Last Modified..: Date: 2002/07/24 13:14:10
87 * CVS Revision...: Revision: 1.4
88 * Copyright......: 2001, 2002 Richard Heyes
90 * @param string $input
91 * @param string $charset
94 function _encodeHeader($input, $charset) {
95 preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
96 foreach ($matches[1] as $value) {
97 $replacement = preg_replace('/([\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
98 $input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
103 function setBody($body) {
104 $this->_body = $body;
111 function getEncodedBody() {
112 return util_convert_body($this->getBody(), $this->getBodyCharset());
115 function setFrom($from) {
116 $this->_from = $this->_validateRecipient($from);
121 return 'noreply@'.forge_get_config('web_host');
127 * Check if given mail is a valid (Ie. Active or Restricted) user.
129 * The given mail can by both user_name or email. Return form is always the
132 * @param $list (IN) list of email addresses separated by , or ;
133 * @return string list of email separated by ,
135 function _validateRecipient($list) {
136 $recipArray = preg_split('/[;,]/', $list);
138 foreach($recipArray as $email) {
139 $email = trim($email);
141 $user = UserManager::instance()->getUserByEmail($email);
143 $allowed_status = array('A', 'R', 'P', 'V', 'W');
144 $one_with_status_allowed_found = false;
145 while ( !$one_with_status_allowed_found) {
146 if (in_array($user->getStatus(), $allowed_status)) {
147 $retArray[] = '"'.$this->_encodeHeader($user->getRealName(), $this->getHeaderCharset()).'" <'.$user->getEmail().'>';
148 $one_with_status_allowed_found = true;
152 if (validate_email($email)) {
153 $retArray[] = $email;
158 return implode(', ', $retArray);
162 function setTo($to, $raw = false) {
166 $this->_to = $this->_validateRecipient($to);
172 return 'noreply@'.forge_get_config('web_host');
177 function setBcc($bcc, $raw = false) {
181 $this->_bcc = $this->_validateRecipient($bcc);
184 if (forge_get_config('bcc_all_emails') != '') {
185 $this->addBcc(forge_get_config('bcc_all_emails'));
189 function addBcc($addbcc, $raw = false) {
191 $this->_bcc .= ', '.$addbcc;
193 $_addbcc = $this->_validateRecipient($addbcc);
194 if (strlen($_addbcc)) {
195 $this->_bcc .= ', '.$_addbcc;
204 function setCc($cc, $raw = false) {
208 $this->_cc = $this->_validateRecipient($cc);
216 function setMimeType($mimeType) {
217 $this->_mimeType = $mimeType;
220 function getMimeType() {
221 return $this->_mimeType;
224 function clearAdditionalHeaders() {
225 $this->_additionalHeaders = array();
228 function addAdditionalHeader($name, $value) {
229 $this->_additionalHeaders[$name] = $value;
232 function removeAdditionalHeader($name) {
233 if (isset($this->_additionalHeaders[$name])) {
234 unset($this->_additionalHeaders[$name]);
239 * @returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
240 * It is important to note that just because the mail was accepted for delivery,
241 * it does NOT mean the mail will actually reach the intended destination.
246 $mail = "To: ".$this->getTo().$sys_lf;
247 $mail .= "From: ".$this->getFrom().$sys_lf;
248 $mail .= "Content-type: ".$this->getMimeType()."; charset=".$this->getBodyCharset().$sys_lf;
249 $mail .= "Content-Transfer-Encoding: 8bit".$sys_lf;
250 $cc = $this->getCc();
251 if (strlen($cc) > 0) {
252 $mail .= "Cc: ".$cc.$sys_lf;
254 $bcc = $this->getBcc();
255 if (strlen($bcc) > 0) {
256 $mail .= "Bcc: ".$bcc.$sys_lf;
258 foreach($this->_additionalHeaders as $name => $value) {
259 $mail .= $name.": ".$value.$sys_lf;
261 $mail .= $this->getEncodedSubject().$sys_lf;
262 $mail .= $this->getEncodedBody().$sys_lf;
263 return $this->_sendmail($mail);
267 * Perform effective email send.
270 * @param string $mail
273 function _sendmail($mail) {
274 $from = $this->getFrom();
275 $handle = popen(forge_get_config('sendmail_path')." -f'$from' -t -i", 'w');
276 fwrite($handle, $mail);