Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Mail { | |
36 | ||
37 | /** | |
38 | * Wrapper function to send mail in CiviCRM. Hooks are called from this function. The input parameter | |
39 | * is an associateive array which holds the values of field needed to send an email. These are: | |
40 | * | |
41 | * from : complete from envelope | |
42 | * toName : name of person to send email | |
43 | * toEmail : email address to send to | |
44 | * cc : email addresses to cc | |
45 | * bcc : email addresses to bcc | |
46 | * subject : subject of the email | |
47 | * text : text of the message | |
48 | * html : html version of the message | |
49 | * replyTo : reply-to header in the email | |
50 | * attachments: an associative array of | |
51 | * fullPath : complete pathname to the file | |
52 | * mime_type: mime type of the attachment | |
53 | * cleanName: the user friendly name of the attachmment | |
54 | * | |
55 | * @param array $params (by reference) | |
56 | * | |
6a488035 TO |
57 | * |
58 | * @return boolean true if a mail was sent, else false | |
59 | */ | |
00be9182 | 60 | public static function send(&$params) { |
6a488035 TO |
61 | $returnPath = CRM_Core_BAO_MailSettings::defaultReturnPath(); |
62 | $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId(); | |
63 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
64 | $from = CRM_Utils_Array::value('from', $params); | |
65 | if (!$returnPath) { | |
66 | $returnPath = self::pluckEmailFromHeader($from); | |
67 | } | |
68 | $params['returnPath'] = $returnPath; | |
69 | ||
70 | // first call the mail alter hook | |
71 | CRM_Utils_Hook::alterMailParams($params); | |
72 | ||
73 | // check if any module has aborted mail sending | |
8cc574cf | 74 | if (!empty($params['abortMailSend']) || empty($params['toEmail'])) { |
6a488035 TO |
75 | return FALSE; |
76 | } | |
77 | ||
78 | $textMessage = CRM_Utils_Array::value('text', $params); | |
79 | $htmlMessage = CRM_Utils_Array::value('html', $params); | |
80 | $attachments = CRM_Utils_Array::value('attachments', $params); | |
81 | ||
82 | // CRM-6224 | |
83 | if (trim(CRM_Utils_String::htmlToText($htmlMessage)) == '') { | |
84 | $htmlMessage = FALSE; | |
85 | } | |
86 | ||
87 | $headers = array(); | |
88 | // CRM-10699 support custom email headers | |
a7488080 | 89 | if (!empty($params['headers'])) { |
6a488035 TO |
90 | $headers = array_merge($headers, $params['headers']); |
91 | } | |
92 | $headers['From'] = $params['from']; | |
361fb6c0 DL |
93 | $headers['To'] = |
94 | self::formatRFC822Email( | |
95 | CRM_Utils_Array::value('toName', $params), | |
96 | CRM_Utils_Array::value('toEmail', $params), | |
97 | FALSE | |
98 | ); | |
6a488035 TO |
99 | $headers['Cc'] = CRM_Utils_Array::value('cc', $params); |
100 | $headers['Bcc'] = CRM_Utils_Array::value('bcc', $params); | |
101 | $headers['Subject'] = CRM_Utils_Array::value('subject', $params); | |
102 | $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8'; | |
103 | $headers['Content-Disposition'] = 'inline'; | |
104 | $headers['Content-Transfer-Encoding'] = '8bit'; | |
105 | $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params); | |
361fb6c0 | 106 | |
6a488035 TO |
107 | // CRM-11295: Omit reply-to headers if empty; this avoids issues with overzealous mailservers |
108 | $replyTo = CRM_Utils_Array::value('replyTo', $params, $from); | |
361fb6c0 | 109 | |
6a488035 TO |
110 | if (!empty($replyTo)) { |
111 | $headers['Reply-To'] = $replyTo; | |
112 | } | |
113 | $headers['Date'] = date('r'); | |
114 | if ($includeMessageId) { | |
115 | $headers['Message-ID'] = '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; | |
116 | } | |
a7488080 | 117 | if (!empty($params['autoSubmitted'])) { |
6a488035 TO |
118 | $headers['Auto-Submitted'] = "Auto-Generated"; |
119 | } | |
120 | ||
121 | //make sure we has to have space, CRM-6977 | |
122 | foreach (array('From', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path') as $fld) { | |
123 | if (isset($headers[$fld])) { | |
124 | $headers[$fld] = str_replace('"<', '" <', $headers[$fld]); | |
125 | } | |
126 | } | |
127 | ||
128 | // quote FROM, if comma is detected AND is not already quoted. CRM-7053 | |
129 | if (strpos($headers['From'], ',') !== FALSE) { | |
130 | $from = explode(' <', $headers['From']); | |
131 | $headers['From'] = self::formatRFC822Email( | |
132 | $from[0], | |
133 | substr(trim($from[1]), 0, -1), | |
134 | TRUE | |
135 | ); | |
136 | } | |
137 | ||
138 | require_once 'Mail/mime.php'; | |
139 | $msg = new Mail_mime("\n"); | |
140 | if ($textMessage) { | |
141 | $msg->setTxtBody($textMessage); | |
142 | } | |
143 | ||
144 | if ($htmlMessage) { | |
145 | $msg->setHTMLBody($htmlMessage); | |
146 | } | |
147 | ||
148 | if (!empty($attachments)) { | |
149 | foreach ($attachments as $fileID => $attach) { | |
150 | $msg->addAttachment( | |
151 | $attach['fullPath'], | |
152 | $attach['mime_type'], | |
153 | $attach['cleanName'] | |
154 | ); | |
155 | } | |
156 | } | |
157 | ||
158 | $message = self::setMimeParams($msg); | |
159 | $headers = &$msg->headers($headers); | |
160 | ||
161 | $to = array($params['toEmail']); | |
cbcec379 DL |
162 | $result = null; |
163 | $mailer =& CRM_Core_Config::getMailer( ); | |
164 | ||
165 | // Mail_smtp and Mail_sendmail mailers require Bcc anc Cc emails | |
166 | // be included in both $to and $headers['Cc', 'Bcc'] | |
167 | if (get_class($mailer) != "Mail_mail") { | |
383c047b | 168 | //get emails from headers, since these are |
cbcec379 | 169 | //combination of name and email addresses. |
a7488080 | 170 | if (!empty($headers['Cc'])) { |
cbcec379 DL |
171 | $to[] = CRM_Utils_Array::value( 'Cc', $headers ); |
172 | } | |
a7488080 | 173 | if (!empty($headers['Bcc'])) { |
cbcec379 DL |
174 | $to[] = CRM_Utils_Array::value( 'Bcc', $headers ); |
175 | } | |
6a488035 | 176 | } |
6a488035 | 177 | if (is_object($mailer)) { |
6a4257d4 | 178 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 179 | $result = $mailer->send($to, $headers, $message); |
6a488035 TO |
180 | if (is_a($result, 'PEAR_Error')) { |
181 | $message = self::errorMessage($mailer, $result); | |
182 | // append error message in case multiple calls are being made to | |
183 | // this method in the course of sending a batch of messages. | |
361fb6c0 | 184 | CRM_Core_Session::setStatus($message, ts('Mailing Error'), 'error'); |
6a488035 TO |
185 | return FALSE; |
186 | } | |
187 | // CRM-10699 | |
188 | CRM_Utils_Hook::postEmailSend($params); | |
189 | return TRUE; | |
190 | } | |
191 | return FALSE; | |
192 | } | |
193 | ||
5bc392e6 EM |
194 | /** |
195 | * @param $mailer | |
196 | * @param $result | |
197 | * | |
198 | * @return string | |
199 | */ | |
00be9182 | 200 | public static function errorMessage($mailer, $result) { |
6a488035 TO |
201 | $message = '<p>' . ts('An error occurred when CiviCRM attempted to send an email (via %1). If you received this error after submitting on online contribution or event registration - the transaction was completed, but we were unable to send the email receipt.', array( |
202 | 1 => 'SMTP')) . '</p>' . '<p>' . ts('The mail library returned the following error message:') . '<br /><span class="font-red"><strong>' . $result->getMessage() . '</strong></span></p>' . '<p>' . ts('This is probably related to a problem in your Outbound Email Settings (Administer CiviCRM » System Settings » Outbound Email), OR the FROM email address specifically configured for your contribution page or event. Possible causes are:') . '</p>'; | |
203 | ||
204 | if (is_a($mailer, 'Mail_smtp')) { | |
205 | $message .= '<ul>' . '<li>' . ts('Your SMTP Username or Password are incorrect.') . '</li>' . '<li>' . ts('Your SMTP Server (machine) name is incorrect.') . '</li>' . '<li>' . ts('You need to use a Port other than the default port 25 in your environment.') . '</li>' . '<li>' . ts('Your SMTP server is just not responding right now (it is down for some reason).') . '</li>'; | |
206 | } | |
207 | else { | |
208 | $message .= '<ul>' . '<li>' . ts('Your Sendmail path is incorrect.') . '</li>' . '<li>' . ts('Your Sendmail argument is incorrect.') . '</li>'; | |
209 | } | |
210 | ||
211 | $message .= '<li>' . ts('The FROM Email Address configured for this feature may not be a valid sender based on your email service provider rules.') . '</li>' . '</ul>' . '<p>' . ts('Check <a href="%1">this page</a> for more information.', array( | |
baccef84 | 212 | 1 => CRM_Utils_System::docURL2('user/advanced-configuration/email-system-configuration', TRUE))) . '</p>'; |
6a488035 TO |
213 | |
214 | return $message; | |
215 | } | |
216 | ||
5bc392e6 EM |
217 | /** |
218 | * @param $to | |
219 | * @param $headers | |
220 | * @param $message | |
221 | */ | |
00be9182 | 222 | public static function logger(&$to, &$headers, &$message) { |
6a488035 TO |
223 | if (is_array($to)) { |
224 | $toString = implode(', ', $to); | |
225 | $fileName = $to[0]; | |
226 | } | |
227 | else { | |
228 | $toString = $fileName = $to; | |
229 | } | |
230 | $content = "To: " . $toString . "\n"; | |
231 | foreach ($headers as $key => $val) { | |
232 | $content .= "$key: $val\n"; | |
233 | } | |
234 | $content .= "\n" . $message . "\n"; | |
235 | ||
236 | if (is_numeric(CIVICRM_MAIL_LOG)) { | |
237 | $config = CRM_Core_Config::singleton(); | |
238 | // create the directory if not there | |
239 | $dirName = $config->configAndLogDir . 'mail' . DIRECTORY_SEPARATOR; | |
240 | CRM_Utils_File::createDir($dirName); | |
241 | $fileName = md5(uniqid(CRM_Utils_String::munge($fileName))) . '.txt'; | |
242 | file_put_contents($dirName . $fileName, | |
243 | $content | |
244 | ); | |
245 | } | |
246 | else { | |
247 | file_put_contents(CIVICRM_MAIL_LOG, $content, FILE_APPEND); | |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
252 | * Get the email address itself from a formatted full name + address string | |
253 | * | |
254 | * Ugly but working. | |
255 | * | |
256 | * @param string $header the full name + email address string | |
257 | * | |
258 | * @return string the plucked email address | |
259 | * @static | |
260 | */ | |
00be9182 | 261 | public static function pluckEmailFromHeader($header) { |
6a488035 TO |
262 | preg_match('/<([^<]*)>$/', $header, $matches); |
263 | ||
264 | if (isset($matches[1])) { | |
265 | return $matches[1]; | |
266 | } | |
267 | return NULL; | |
268 | } | |
269 | ||
270 | /** | |
271 | * Get the Active outBound email | |
272 | * | |
273 | * @return boolean true if valid outBound email configuration found, false otherwise | |
6a488035 TO |
274 | * @static |
275 | */ | |
00be9182 | 276 | public static function validOutBoundMail() { |
6a488035 TO |
277 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, |
278 | 'mailing_backend' | |
279 | ); | |
280 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { | |
281 | return TRUE; | |
282 | } | |
283 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
284 | if (!isset($mailingInfo['smtpServer']) || $mailingInfo['smtpServer'] == '' || | |
285 | $mailingInfo['smtpServer'] == 'YOUR SMTP SERVER' || | |
286 | ($mailingInfo['smtpAuth'] && ($mailingInfo['smtpUsername'] == '' || $mailingInfo['smtpPassword'] == '')) | |
287 | ) { | |
288 | return FALSE; | |
289 | } | |
290 | return TRUE; | |
291 | } | |
292 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
293 | if (!$mailingInfo['sendmail_path'] || !$mailingInfo['sendmail_args']) { | |
294 | return FALSE; | |
295 | } | |
296 | return TRUE; | |
297 | } | |
298 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) { | |
299 | return TRUE; | |
300 | } | |
301 | return FALSE; | |
302 | } | |
303 | ||
5bc392e6 EM |
304 | /** |
305 | * @param $message | |
100fef9d | 306 | * @param array $params |
5bc392e6 EM |
307 | * |
308 | * @return mixed | |
309 | */ | |
00be9182 | 310 | public static function &setMimeParams(&$message, $params = NULL) { |
6a488035 TO |
311 | static $mimeParams = NULL; |
312 | if (!$params) { | |
313 | if (!$mimeParams) { | |
314 | $mimeParams = array( | |
315 | 'text_encoding' => '8bit', | |
316 | 'html_encoding' => '8bit', | |
317 | 'head_charset' => 'utf-8', | |
318 | 'text_charset' => 'utf-8', | |
319 | 'html_charset' => 'utf-8', | |
320 | ); | |
321 | } | |
322 | $params = $mimeParams; | |
323 | } | |
324 | return $message->get($params); | |
325 | } | |
326 | ||
5bc392e6 | 327 | /** |
100fef9d | 328 | * @param string $name |
5bc392e6 EM |
329 | * @param $email |
330 | * @param bool $useQuote | |
331 | * | |
332 | * @return null|string | |
333 | */ | |
00be9182 | 334 | public static function formatRFC822Email($name, $email, $useQuote = FALSE) { |
6a488035 TO |
335 | $result = NULL; |
336 | ||
337 | $name = trim($name); | |
338 | ||
339 | // strip out double quotes if present at the beginning AND end | |
340 | if (substr($name, 0, 1) == '"' && | |
341 | substr($name, -1, 1) == '"' | |
342 | ) { | |
343 | $name = substr($name, 1, -1); | |
344 | } | |
345 | ||
346 | if (!empty($name)) { | |
347 | // escape the special characters | |
348 | $name = str_replace(array('<', '"', '>'), | |
349 | array('\<', '\"', '\>'), | |
350 | $name | |
351 | ); | |
352 | if (strpos($name, ',') !== FALSE || | |
353 | $useQuote | |
354 | ) { | |
355 | // quote the string if it has a comma | |
356 | $name = '"' . $name . '"'; | |
357 | } | |
358 | ||
359 | $result = "$name "; | |
360 | } | |
361 | ||
362 | $result .= "<{$email}>"; | |
363 | return $result; | |
364 | } | |
365 | ||
366 | /** | |
367 | * Takes a string and checks to see if it needs to be escaped / double quoted | |
368 | * and if so does the needful and return the formatted name | |
369 | * | |
370 | * This code has been copied and adapted from ezc/Mail/src/tools.php | |
371 | */ | |
00be9182 | 372 | public static function formatRFC2822Name($name) { |
6a488035 TO |
373 | $name = trim($name); |
374 | if (!empty($name)) { | |
375 | // remove the quotes around the name part if they are already there | |
376 | if (substr($name, 0, 1) == '"' && substr($name, -1) == '"') { | |
377 | $name = substr($name, 1, -1); | |
378 | } | |
379 | ||
380 | // add slashes to " and \ and surround the name part with quotes | |
381 | if (strpbrk($name, ",@<>:;'\"") !== FALSE) { | |
382 | $name = '"' . addcslashes($name, '\\"') . '"'; | |
383 | } | |
384 | } | |
385 | ||
386 | return $name; | |
387 | } | |
383c047b DG |
388 | |
389 | /** | |
383c047b DG |
390 | * @param string $fileName |
391 | * @param string $html | |
392 | * @param string $format | |
393 | * | |
394 | * @return array $attachments | |
395 | */ | |
00be9182 | 396 | public static function appendPDF($fileName, $html, $format = NULL) { |
383c047b DG |
397 | $pdf_filename = CRM_Core_Config::singleton()->templateCompileDir . CRM_Utils_File::makeFileName($fileName); |
398 | ||
399 | //FIXME : CRM-7894 | |
400 | //xmlns attribute is required in XHTML but it is invalid in HTML, | |
401 | //Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, | |
402 | //and will be added to the <html> tag even if you do not include it. | |
403 | $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\s]+["\'])(.+)?(>)/', '\1\3\4', $html); | |
404 | ||
405 | file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html, | |
406 | $fileName, | |
407 | TRUE, | |
408 | $format) | |
409 | ); | |
410 | return array( | |
411 | 'fullPath' => $pdf_filename, | |
412 | 'mime_type' => 'application/pdf', | |
413 | 'cleanName' => $fileName, | |
414 | ); | |
415 | } | |
6a488035 | 416 | } |