Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Mail { | |
36 | ||
247eb841 TO |
37 | /** |
38 | * Create a new mailer to send any mail from the application. | |
39 | * | |
40 | * Note: The mailer is opened in persistent mode. | |
41 | * | |
42 | * Note: You probably don't want to call this directly. Get a reference | |
43 | * to the mailer through the container. | |
44 | * | |
45 | * @return Mail | |
46 | */ | |
47 | public static function createMailer() { | |
48 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
49 | 'mailing_backend' | |
50 | ); | |
51 | ||
52 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || | |
53 | (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) | |
54 | ) { | |
55 | $mailer = self::_createMailer('CRM_Mailing_BAO_Spool', array()); | |
56 | } | |
57 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
58 | if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { | |
59 | CRM_Core_Error::debug_log_message(ts('There is no valid smtp server setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
60 | CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
61 | } | |
62 | ||
63 | $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; | |
64 | $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25; | |
65 | ||
66 | if ($mailingInfo['smtpAuth']) { | |
67 | $params['username'] = $mailingInfo['smtpUsername']; | |
68 | $params['password'] = CRM_Utils_Crypt::decrypt($mailingInfo['smtpPassword']); | |
69 | $params['auth'] = TRUE; | |
70 | } | |
71 | else { | |
72 | $params['auth'] = FALSE; | |
73 | } | |
74 | ||
75 | // set the localhost value, CRM-3153 | |
76 | $params['localhost'] = CRM_Utils_Array::value('SERVER_NAME', $_SERVER, 'localhost'); | |
77 | ||
78 | // also set the timeout value, lets set it to 30 seconds | |
79 | // CRM-7510 | |
80 | $params['timeout'] = 30; | |
81 | ||
82 | // CRM-9349 | |
83 | $params['persist'] = TRUE; | |
84 | ||
85 | $mailer = self::_createMailer('smtp', $params); | |
86 | } | |
87 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
88 | if ($mailingInfo['sendmail_path'] == '' || | |
89 | !$mailingInfo['sendmail_path'] | |
90 | ) { | |
91 | CRM_Core_Error::debug_log_message(ts('There is no valid sendmail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
92 | CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
93 | } | |
94 | $params['sendmail_path'] = $mailingInfo['sendmail_path']; | |
95 | $params['sendmail_args'] = $mailingInfo['sendmail_args']; | |
96 | ||
97 | $mailer = self::_createMailer('sendmail', $params); | |
98 | } | |
99 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { | |
100 | $mailer = self::_createMailer('mail', array()); | |
101 | } | |
102 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { | |
103 | $mailer = self::_createMailer('mock', array()); | |
104 | } | |
105 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { | |
106 | CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
107 | CRM_Core_Session::setStatus(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
108 | } | |
109 | else { | |
110 | CRM_Core_Error::debug_log_message(ts('There is no valid SMTP server Setting Or SendMail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
111 | CRM_Core_Session::setStatus(ts('There is no valid SMTP server Setting Or sendMail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
112 | CRM_Core_Error::debug_var('mailing_info', $mailingInfo); | |
113 | } | |
114 | return $mailer; | |
115 | } | |
116 | ||
117 | /** | |
118 | * Create a new instance of a PEAR Mail driver. | |
119 | * | |
120 | * @param string $driver | |
121 | * 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory(). | |
122 | * @param array $params | |
123 | * @return object | |
124 | * More specifically, a class which implements the "send()" function | |
125 | */ | |
126 | public static function _createMailer($driver, $params) { | |
127 | if ($driver == 'CRM_Mailing_BAO_Spool') { | |
128 | $mailer = new CRM_Mailing_BAO_Spool($params); | |
129 | } | |
130 | else { | |
131 | $mailer = Mail::factory($driver, $params); | |
132 | } | |
133 | CRM_Utils_Hook::alterMail($mailer, $driver, $params); | |
134 | return $mailer; | |
135 | } | |
136 | ||
6a488035 TO |
137 | /** |
138 | * Wrapper function to send mail in CiviCRM. Hooks are called from this function. The input parameter | |
139 | * is an associateive array which holds the values of field needed to send an email. These are: | |
140 | * | |
141 | * from : complete from envelope | |
142 | * toName : name of person to send email | |
143 | * toEmail : email address to send to | |
144 | * cc : email addresses to cc | |
145 | * bcc : email addresses to bcc | |
146 | * subject : subject of the email | |
147 | * text : text of the message | |
148 | * html : html version of the message | |
149 | * replyTo : reply-to header in the email | |
150 | * attachments: an associative array of | |
151 | * fullPath : complete pathname to the file | |
152 | * mime_type: mime type of the attachment | |
153 | * cleanName: the user friendly name of the attachmment | |
154 | * | |
77855840 TO |
155 | * @param array $params |
156 | * (by reference). | |
6a488035 | 157 | * |
ae5ffbb7 TO |
158 | * @return bool |
159 | * TRUE if a mail was sent, else FALSE. | |
6a488035 | 160 | */ |
00be9182 | 161 | public static function send(&$params) { |
353ffa53 | 162 | $returnPath = CRM_Core_BAO_MailSettings::defaultReturnPath(); |
6a488035 | 163 | $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId(); |
353ffa53 TO |
164 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); |
165 | $from = CRM_Utils_Array::value('from', $params); | |
6a488035 TO |
166 | if (!$returnPath) { |
167 | $returnPath = self::pluckEmailFromHeader($from); | |
168 | } | |
169 | $params['returnPath'] = $returnPath; | |
170 | ||
171 | // first call the mail alter hook | |
172 | CRM_Utils_Hook::alterMailParams($params); | |
173 | ||
174 | // check if any module has aborted mail sending | |
8cc574cf | 175 | if (!empty($params['abortMailSend']) || empty($params['toEmail'])) { |
6a488035 TO |
176 | return FALSE; |
177 | } | |
178 | ||
179 | $textMessage = CRM_Utils_Array::value('text', $params); | |
180 | $htmlMessage = CRM_Utils_Array::value('html', $params); | |
181 | $attachments = CRM_Utils_Array::value('attachments', $params); | |
182 | ||
183 | // CRM-6224 | |
184 | if (trim(CRM_Utils_String::htmlToText($htmlMessage)) == '') { | |
185 | $htmlMessage = FALSE; | |
186 | } | |
187 | ||
353ffa53 | 188 | $headers = array(); |
6a488035 | 189 | // CRM-10699 support custom email headers |
a7488080 | 190 | if (!empty($params['headers'])) { |
6a488035 TO |
191 | $headers = array_merge($headers, $params['headers']); |
192 | } | |
193 | $headers['From'] = $params['from']; | |
ae5ffbb7 TO |
194 | $headers['To'] = self::formatRFC822Email( |
195 | CRM_Utils_Array::value('toName', $params), | |
196 | CRM_Utils_Array::value('toEmail', $params), | |
197 | FALSE | |
198 | ); | |
6a488035 TO |
199 | $headers['Cc'] = CRM_Utils_Array::value('cc', $params); |
200 | $headers['Bcc'] = CRM_Utils_Array::value('bcc', $params); | |
201 | $headers['Subject'] = CRM_Utils_Array::value('subject', $params); | |
202 | $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8'; | |
203 | $headers['Content-Disposition'] = 'inline'; | |
204 | $headers['Content-Transfer-Encoding'] = '8bit'; | |
205 | $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params); | |
361fb6c0 | 206 | |
6a488035 TO |
207 | // CRM-11295: Omit reply-to headers if empty; this avoids issues with overzealous mailservers |
208 | $replyTo = CRM_Utils_Array::value('replyTo', $params, $from); | |
361fb6c0 | 209 | |
6a488035 TO |
210 | if (!empty($replyTo)) { |
211 | $headers['Reply-To'] = $replyTo; | |
212 | } | |
213 | $headers['Date'] = date('r'); | |
214 | if ($includeMessageId) { | |
215 | $headers['Message-ID'] = '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; | |
216 | } | |
a7488080 | 217 | if (!empty($params['autoSubmitted'])) { |
6a488035 TO |
218 | $headers['Auto-Submitted'] = "Auto-Generated"; |
219 | } | |
220 | ||
221 | //make sure we has to have space, CRM-6977 | |
222 | foreach (array('From', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path') as $fld) { | |
223 | if (isset($headers[$fld])) { | |
224 | $headers[$fld] = str_replace('"<', '" <', $headers[$fld]); | |
225 | } | |
226 | } | |
227 | ||
228 | // quote FROM, if comma is detected AND is not already quoted. CRM-7053 | |
229 | if (strpos($headers['From'], ',') !== FALSE) { | |
230 | $from = explode(' <', $headers['From']); | |
231 | $headers['From'] = self::formatRFC822Email( | |
232 | $from[0], | |
233 | substr(trim($from[1]), 0, -1), | |
234 | TRUE | |
235 | ); | |
236 | } | |
237 | ||
238 | require_once 'Mail/mime.php'; | |
239 | $msg = new Mail_mime("\n"); | |
240 | if ($textMessage) { | |
241 | $msg->setTxtBody($textMessage); | |
242 | } | |
243 | ||
244 | if ($htmlMessage) { | |
245 | $msg->setHTMLBody($htmlMessage); | |
246 | } | |
247 | ||
248 | if (!empty($attachments)) { | |
249 | foreach ($attachments as $fileID => $attach) { | |
250 | $msg->addAttachment( | |
251 | $attach['fullPath'], | |
252 | $attach['mime_type'], | |
253 | $attach['cleanName'] | |
254 | ); | |
255 | } | |
256 | } | |
257 | ||
258 | $message = self::setMimeParams($msg); | |
259 | $headers = &$msg->headers($headers); | |
260 | ||
261 | $to = array($params['toEmail']); | |
e7292422 | 262 | $result = NULL; |
247eb841 | 263 | $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); |
cbcec379 DL |
264 | |
265 | // Mail_smtp and Mail_sendmail mailers require Bcc anc Cc emails | |
266 | // be included in both $to and $headers['Cc', 'Bcc'] | |
267 | if (get_class($mailer) != "Mail_mail") { | |
e7292422 TO |
268 | //get emails from headers, since these are |
269 | //combination of name and email addresses. | |
270 | if (!empty($headers['Cc'])) { | |
481a74f4 | 271 | $to[] = CRM_Utils_Array::value('Cc', $headers); |
e7292422 TO |
272 | } |
273 | if (!empty($headers['Bcc'])) { | |
481a74f4 | 274 | $to[] = CRM_Utils_Array::value('Bcc', $headers); |
e7292422 | 275 | } |
6a488035 | 276 | } |
6a488035 | 277 | if (is_object($mailer)) { |
6a4257d4 | 278 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 279 | $result = $mailer->send($to, $headers, $message); |
6a488035 TO |
280 | if (is_a($result, 'PEAR_Error')) { |
281 | $message = self::errorMessage($mailer, $result); | |
282 | // append error message in case multiple calls are being made to | |
283 | // this method in the course of sending a batch of messages. | |
361fb6c0 | 284 | CRM_Core_Session::setStatus($message, ts('Mailing Error'), 'error'); |
6a488035 TO |
285 | return FALSE; |
286 | } | |
287 | // CRM-10699 | |
288 | CRM_Utils_Hook::postEmailSend($params); | |
289 | return TRUE; | |
290 | } | |
291 | return FALSE; | |
292 | } | |
293 | ||
5bc392e6 EM |
294 | /** |
295 | * @param $mailer | |
296 | * @param $result | |
297 | * | |
298 | * @return string | |
299 | */ | |
00be9182 | 300 | public static function errorMessage($mailer, $result) { |
6a488035 | 301 | $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( |
ae5ffbb7 | 302 | 1 => 'SMTP', |
353ffa53 | 303 | )) . '</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>'; |
6a488035 TO |
304 | |
305 | if (is_a($mailer, 'Mail_smtp')) { | |
306 | $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>'; | |
307 | } | |
308 | else { | |
309 | $message .= '<ul>' . '<li>' . ts('Your Sendmail path is incorrect.') . '</li>' . '<li>' . ts('Your Sendmail argument is incorrect.') . '</li>'; | |
310 | } | |
311 | ||
312 | $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( | |
ae5ffbb7 | 313 | 1 => CRM_Utils_System::docURL2('user/advanced-configuration/email-system-configuration', TRUE), |
353ffa53 | 314 | )) . '</p>'; |
6a488035 TO |
315 | |
316 | return $message; | |
317 | } | |
318 | ||
5bc392e6 EM |
319 | /** |
320 | * @param $to | |
321 | * @param $headers | |
322 | * @param $message | |
323 | */ | |
00be9182 | 324 | public static function logger(&$to, &$headers, &$message) { |
6a488035 TO |
325 | if (is_array($to)) { |
326 | $toString = implode(', ', $to); | |
327 | $fileName = $to[0]; | |
328 | } | |
329 | else { | |
330 | $toString = $fileName = $to; | |
331 | } | |
332 | $content = "To: " . $toString . "\n"; | |
333 | foreach ($headers as $key => $val) { | |
334 | $content .= "$key: $val\n"; | |
335 | } | |
336 | $content .= "\n" . $message . "\n"; | |
337 | ||
338 | if (is_numeric(CIVICRM_MAIL_LOG)) { | |
339 | $config = CRM_Core_Config::singleton(); | |
340 | // create the directory if not there | |
341 | $dirName = $config->configAndLogDir . 'mail' . DIRECTORY_SEPARATOR; | |
342 | CRM_Utils_File::createDir($dirName); | |
343 | $fileName = md5(uniqid(CRM_Utils_String::munge($fileName))) . '.txt'; | |
344 | file_put_contents($dirName . $fileName, | |
345 | $content | |
346 | ); | |
347 | } | |
348 | else { | |
349 | file_put_contents(CIVICRM_MAIL_LOG, $content, FILE_APPEND); | |
350 | } | |
351 | } | |
352 | ||
353 | /** | |
354 | * Get the email address itself from a formatted full name + address string | |
355 | * | |
356 | * Ugly but working. | |
357 | * | |
77855840 TO |
358 | * @param string $header |
359 | * The full name + email address string. | |
6a488035 | 360 | * |
a6c01b45 CW |
361 | * @return string |
362 | * the plucked email address | |
6a488035 | 363 | */ |
00be9182 | 364 | public static function pluckEmailFromHeader($header) { |
6a488035 TO |
365 | preg_match('/<([^<]*)>$/', $header, $matches); |
366 | ||
367 | if (isset($matches[1])) { | |
368 | return $matches[1]; | |
369 | } | |
370 | return NULL; | |
371 | } | |
372 | ||
373 | /** | |
fe482240 | 374 | * Get the Active outBound email. |
6a488035 | 375 | * |
ae5ffbb7 TO |
376 | * @return bool |
377 | * TRUE if valid outBound email configuration found, false otherwise. | |
6a488035 | 378 | */ |
00be9182 | 379 | public static function validOutBoundMail() { |
6a488035 TO |
380 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, |
381 | 'mailing_backend' | |
382 | ); | |
383 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { | |
384 | return TRUE; | |
385 | } | |
386 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
387 | if (!isset($mailingInfo['smtpServer']) || $mailingInfo['smtpServer'] == '' || | |
388 | $mailingInfo['smtpServer'] == 'YOUR SMTP SERVER' || | |
389 | ($mailingInfo['smtpAuth'] && ($mailingInfo['smtpUsername'] == '' || $mailingInfo['smtpPassword'] == '')) | |
390 | ) { | |
391 | return FALSE; | |
392 | } | |
393 | return TRUE; | |
394 | } | |
395 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
396 | if (!$mailingInfo['sendmail_path'] || !$mailingInfo['sendmail_args']) { | |
397 | return FALSE; | |
398 | } | |
399 | return TRUE; | |
400 | } | |
401 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) { | |
402 | return TRUE; | |
403 | } | |
404 | return FALSE; | |
405 | } | |
406 | ||
5bc392e6 EM |
407 | /** |
408 | * @param $message | |
100fef9d | 409 | * @param array $params |
5bc392e6 EM |
410 | * |
411 | * @return mixed | |
412 | */ | |
00be9182 | 413 | public static function &setMimeParams(&$message, $params = NULL) { |
6a488035 TO |
414 | static $mimeParams = NULL; |
415 | if (!$params) { | |
416 | if (!$mimeParams) { | |
417 | $mimeParams = array( | |
418 | 'text_encoding' => '8bit', | |
419 | 'html_encoding' => '8bit', | |
420 | 'head_charset' => 'utf-8', | |
421 | 'text_charset' => 'utf-8', | |
422 | 'html_charset' => 'utf-8', | |
423 | ); | |
424 | } | |
425 | $params = $mimeParams; | |
426 | } | |
427 | return $message->get($params); | |
428 | } | |
429 | ||
5bc392e6 | 430 | /** |
100fef9d | 431 | * @param string $name |
5bc392e6 EM |
432 | * @param $email |
433 | * @param bool $useQuote | |
434 | * | |
435 | * @return null|string | |
436 | */ | |
00be9182 | 437 | public static function formatRFC822Email($name, $email, $useQuote = FALSE) { |
6a488035 TO |
438 | $result = NULL; |
439 | ||
440 | $name = trim($name); | |
441 | ||
442 | // strip out double quotes if present at the beginning AND end | |
443 | if (substr($name, 0, 1) == '"' && | |
444 | substr($name, -1, 1) == '"' | |
445 | ) { | |
446 | $name = substr($name, 1, -1); | |
447 | } | |
448 | ||
449 | if (!empty($name)) { | |
450 | // escape the special characters | |
451 | $name = str_replace(array('<', '"', '>'), | |
452 | array('\<', '\"', '\>'), | |
453 | $name | |
454 | ); | |
455 | if (strpos($name, ',') !== FALSE || | |
456 | $useQuote | |
457 | ) { | |
458 | // quote the string if it has a comma | |
459 | $name = '"' . $name . '"'; | |
460 | } | |
461 | ||
462 | $result = "$name "; | |
463 | } | |
464 | ||
465 | $result .= "<{$email}>"; | |
466 | return $result; | |
467 | } | |
468 | ||
469 | /** | |
470 | * Takes a string and checks to see if it needs to be escaped / double quoted | |
471 | * and if so does the needful and return the formatted name | |
472 | * | |
473 | * This code has been copied and adapted from ezc/Mail/src/tools.php | |
474 | */ | |
00be9182 | 475 | public static function formatRFC2822Name($name) { |
6a488035 TO |
476 | $name = trim($name); |
477 | if (!empty($name)) { | |
478 | // remove the quotes around the name part if they are already there | |
479 | if (substr($name, 0, 1) == '"' && substr($name, -1) == '"') { | |
480 | $name = substr($name, 1, -1); | |
481 | } | |
482 | ||
483 | // add slashes to " and \ and surround the name part with quotes | |
484 | if (strpbrk($name, ",@<>:;'\"") !== FALSE) { | |
485 | $name = '"' . addcslashes($name, '\\"') . '"'; | |
486 | } | |
487 | } | |
488 | ||
489 | return $name; | |
490 | } | |
383c047b DG |
491 | |
492 | /** | |
383c047b DG |
493 | * @param string $fileName |
494 | * @param string $html | |
495 | * @param string $format | |
496 | * | |
a6c01b45 | 497 | * @return array |
383c047b | 498 | */ |
00be9182 | 499 | public static function appendPDF($fileName, $html, $format = NULL) { |
383c047b DG |
500 | $pdf_filename = CRM_Core_Config::singleton()->templateCompileDir . CRM_Utils_File::makeFileName($fileName); |
501 | ||
502 | //FIXME : CRM-7894 | |
503 | //xmlns attribute is required in XHTML but it is invalid in HTML, | |
504 | //Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, | |
505 | //and will be added to the <html> tag even if you do not include it. | |
506 | $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\s]+["\'])(.+)?(>)/', '\1\3\4', $html); | |
507 | ||
508 | file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html, | |
353ffa53 TO |
509 | $fileName, |
510 | TRUE, | |
511 | $format) | |
383c047b DG |
512 | ); |
513 | return array( | |
514 | 'fullPath' => $pdf_filename, | |
515 | 'mime_type' => 'application/pdf', | |
516 | 'cleanName' => $fileName, | |
517 | ); | |
518 | } | |
96025800 | 519 | |
6a488035 | 520 | } |