commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / Mail / mail.php
1 <?php
2 /**
3 * internal PHP-mail() implementation of the PEAR Mail:: interface.
4 *
5 * PHP versions 4 and 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2010 Chuck Hagenbuch
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * o Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * o Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * o The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
23 * permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * @category Mail
38 * @package Mail
39 * @author Chuck Hagenbuch <chuck@horde.org>
40 * @copyright 2010 Chuck Hagenbuch
41 * @license http://opensource.org/licenses/bsd-license.php New BSD License
42 * @version CVS: $Id: mail.php 294747 2010-02-08 08:18:33Z clockwerx $
43 * @link http://pear.php.net/package/Mail/
44 */
45
46 /**
47 * internal PHP-mail() implementation of the PEAR Mail:: interface.
48 * @package Mail
49 * @version $Revision: 294747 $
50 */
51 class Mail_mail extends Mail {
52
53 /**
54 * Any arguments to pass to the mail() function.
55 * @var string
56 */
57 var $_params = '';
58
59 /**
60 * Constructor.
61 *
62 * Instantiates a new Mail_mail:: object based on the parameters
63 * passed in.
64 *
65 * @param array $params Extra arguments for the mail() function.
66 */
67 function Mail_mail($params = null)
68 {
69 // The other mail implementations accept parameters as arrays.
70 // In the interest of being consistent, explode an array into
71 // a string of parameter arguments.
72 if (is_array($params)) {
73 $this->_params = join(' ', $params);
74 } else {
75 $this->_params = $params;
76 }
77
78 /* Because the mail() function may pass headers as command
79 * line arguments, we can't guarantee the use of the standard
80 * "\r\n" separator. Instead, we use the system's native line
81 * separator. */
82 if (defined('PHP_EOL')) {
83 $this->sep = PHP_EOL;
84 } else {
85 $this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
86 }
87 }
88
89 /**
90 * Implements Mail_mail::send() function using php's built-in mail()
91 * command.
92 *
93 * @param mixed $recipients Either a comma-seperated list of recipients
94 * (RFC822 compliant), or an array of recipients,
95 * each RFC822 valid. This may contain recipients not
96 * specified in the headers, for Bcc:, resending
97 * messages, etc.
98 *
99 * @param array $headers The array of headers to send with the mail, in an
100 * associative array, where the array key is the
101 * header name (ie, 'Subject'), and the array value
102 * is the header value (ie, 'test'). The header
103 * produced from those values would be 'Subject:
104 * test'.
105 *
106 * @param string $body The full text of the message body, including any
107 * Mime parts, etc.
108 *
109 * @return mixed Returns true on success, or a PEAR_Error
110 * containing a descriptive error message on
111 * failure.
112 *
113 * @access public
114 */
115 function send($recipients, $headers, $body)
116 {
117 if (defined('CIVICRM_MAIL_LOG')) {
118 CRM_Utils_Mail::logger($recipients, $headers, $body);
119 if(!defined('CIVICRM_MAIL_LOG_AND SEND')) {
120 return true;
121 }
122 }
123
124 if (!is_array($headers)) {
125 return PEAR::raiseError('$headers must be an array');
126 }
127
128 $result = $this->_sanitizeHeaders($headers);
129 if (is_a($result, 'PEAR_Error')) {
130 return $result;
131 }
132
133 // If we're passed an array of recipients, implode it.
134 if (is_array($recipients)) {
135 $recipients = implode(', ', $recipients);
136 }
137
138 // Get the Subject out of the headers array so that we can
139 // pass it as a seperate argument to mail().
140 $subject = '';
141 if (isset($headers['Subject'])) {
142 $subject = $headers['Subject'];
143 unset($headers['Subject']);
144 }
145
146 // Also remove the To: header. The mail() function will add its own
147 // To: header based on the contents of $recipients.
148 unset($headers['To']);
149
150 // Flatten the headers out.
151 $headerElements = $this->prepareHeaders($headers);
152 if (is_a($headerElements, 'PEAR_Error')) {
153 return $headerElements;
154 }
155 list($from, $text_headers) = $headerElements;
156
157 // use Return-Path for SMTP envelope’s FROM address (if set), CRM-5946
158 if (!empty($headers['Return-Path'])) {
159 $from = $headers['Return-Path'];
160 }
161 $this->_params = "-f".$from;
162
163 // We only use mail()'s optional fifth parameter if the additional
164 // parameters have been provided and we're not running in safe mode.
165 if (empty($this->_params) || ini_get('safe_mode')) {
166 $result = mail($recipients, $subject, $body, $text_headers);
167 } else {
168 $result = mail($recipients, $subject, $body, $text_headers,
169 $this->_params);
170 }
171
172 // If the mail() function returned failure, we need to create a
173 // PEAR_Error object and return it instead of the boolean result.
174 if ($result === false) {
175 $result = PEAR::raiseError('mail() returned failure');
176 }
177
178 return $result;
179 }
180
181 }