Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Utils_Mail { | |
18 | ||
247eb841 TO |
19 | /** |
20 | * Create a new mailer to send any mail from the application. | |
21 | * | |
22 | * Note: The mailer is opened in persistent mode. | |
23 | * | |
24 | * Note: You probably don't want to call this directly. Get a reference | |
25 | * to the mailer through the container. | |
26 | * | |
27 | * @return Mail | |
ee3db087 SL |
28 | * |
29 | * @throws CRM_Core_Exception | |
247eb841 TO |
30 | */ |
31 | public static function createMailer() { | |
aaffa79f | 32 | $mailingInfo = Civi::settings()->get('mailing_backend'); |
247eb841 | 33 | |
c9d8809c | 34 | /*@var Mail $mailer*/ |
247eb841 TO |
35 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || |
36 | (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) | |
37 | ) { | |
be2fb01f | 38 | $mailer = self::_createMailer('CRM_Mailing_BAO_Spool', []); |
247eb841 TO |
39 | } |
40 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
41 | if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { | |
be2fb01f | 42 | 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.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); |
ee3db087 | 43 | throw new CRM_Core_Exception(ts('There is no valid smtp server setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the SMTP Server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); |
247eb841 TO |
44 | } |
45 | ||
46 | $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; | |
47 | $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25; | |
48 | ||
49 | if ($mailingInfo['smtpAuth']) { | |
50 | $params['username'] = $mailingInfo['smtpUsername']; | |
03bfb187 | 51 | $params['password'] = \Civi::service('crypto.token')->decrypt($mailingInfo['smtpPassword']); |
247eb841 TO |
52 | $params['auth'] = TRUE; |
53 | } | |
54 | else { | |
55 | $params['auth'] = FALSE; | |
56 | } | |
57 | ||
ef84aa00 | 58 | /* |
59 | * Set the localhost value, CRM-3153 | |
60 | * Use the host name of the web server, falling back to the base URL | |
61 | * (eg when using the PHP CLI), and then falling back to localhost. | |
62 | */ | |
63 | $params['localhost'] = CRM_Utils_Array::value( | |
64 | 'SERVER_NAME', | |
65 | $_SERVER, | |
66 | CRM_Utils_Array::value( | |
67 | 'host', | |
68 | parse_url(CIVICRM_UF_BASEURL), | |
69 | 'localhost' | |
70 | ) | |
71 | ); | |
247eb841 TO |
72 | |
73 | // also set the timeout value, lets set it to 30 seconds | |
74 | // CRM-7510 | |
75 | $params['timeout'] = 30; | |
76 | ||
77 | // CRM-9349 | |
78 | $params['persist'] = TRUE; | |
79 | ||
80 | $mailer = self::_createMailer('smtp', $params); | |
81 | } | |
82 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
83 | if ($mailingInfo['sendmail_path'] == '' || | |
84 | !$mailingInfo['sendmail_path'] | |
85 | ) { | |
be2fb01f | 86 | 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.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); |
ee3db087 | 87 | throw new CRM_Core_Exception(ts('There is no valid sendmail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the sendmail server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); |
247eb841 TO |
88 | } |
89 | $params['sendmail_path'] = $mailingInfo['sendmail_path']; | |
90 | $params['sendmail_args'] = $mailingInfo['sendmail_args']; | |
91 | ||
92 | $mailer = self::_createMailer('sendmail', $params); | |
93 | } | |
94 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { | |
be2fb01f | 95 | $mailer = self::_createMailer('mail', []); |
247eb841 TO |
96 | } |
97 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { | |
c9d8809c | 98 | $mailer = self::_createMailer('mock', $mailingInfo); |
247eb841 TO |
99 | } |
100 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { | |
be2fb01f CW |
101 | 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.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); |
102 | CRM_Core_Error::statusBounce(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); | |
247eb841 TO |
103 | } |
104 | else { | |
be2fb01f | 105 | 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.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); |
247eb841 | 106 | CRM_Core_Error::debug_var('mailing_info', $mailingInfo); |
be2fb01f | 107 | CRM_Core_Error::statusBounce(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.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); |
247eb841 TO |
108 | } |
109 | return $mailer; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Create a new instance of a PEAR Mail driver. | |
114 | * | |
115 | * @param string $driver | |
116 | * 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory(). | |
117 | * @param array $params | |
118 | * @return object | |
119 | * More specifically, a class which implements the "send()" function | |
120 | */ | |
121 | public static function _createMailer($driver, $params) { | |
122 | if ($driver == 'CRM_Mailing_BAO_Spool') { | |
123 | $mailer = new CRM_Mailing_BAO_Spool($params); | |
124 | } | |
125 | else { | |
126 | $mailer = Mail::factory($driver, $params); | |
dee6e582 TO |
127 | } |
128 | ||
129 | // Previously, CiviCRM bundled patches to change the behavior of 3 specific drivers. Use wrapper/filters to avoid patching. | |
130 | $mailer = new CRM_Utils_Mail_FilteredPearMailer($driver, $params, $mailer); | |
131 | if (in_array($driver, ['smtp', 'mail', 'sendmail'])) { | |
132 | $mailer->addFilter('2000_log', ['CRM_Utils_Mail_Logger', 'filter']); | |
133 | $mailer->addFilter('2100_validate', function ($mailer, &$recipients, &$headers, &$body) { | |
134 | if (!is_array($headers)) { | |
135 | return PEAR::raiseError('$headers must be an array'); | |
136 | } | |
137 | }); | |
247eb841 | 138 | } |
f2b63bd3 | 139 | CRM_Utils_Hook::alterMailer($mailer, $driver, $params); |
247eb841 TO |
140 | return $mailer; |
141 | } | |
142 | ||
6a488035 TO |
143 | /** |
144 | * Wrapper function to send mail in CiviCRM. Hooks are called from this function. The input parameter | |
145 | * is an associateive array which holds the values of field needed to send an email. These are: | |
146 | * | |
147 | * from : complete from envelope | |
148 | * toName : name of person to send email | |
149 | * toEmail : email address to send to | |
150 | * cc : email addresses to cc | |
151 | * bcc : email addresses to bcc | |
152 | * subject : subject of the email | |
153 | * text : text of the message | |
154 | * html : html version of the message | |
155 | * replyTo : reply-to header in the email | |
52f4ecb2 SL |
156 | * returnpath : email address for bounces to be sent to |
157 | * messageId : Message ID for this email mesage | |
6a488035 TO |
158 | * attachments: an associative array of |
159 | * fullPath : complete pathname to the file | |
160 | * mime_type: mime type of the attachment | |
161 | * cleanName: the user friendly name of the attachmment | |
162 | * | |
77855840 TO |
163 | * @param array $params |
164 | * (by reference). | |
6a488035 | 165 | * |
ae5ffbb7 TO |
166 | * @return bool |
167 | * TRUE if a mail was sent, else FALSE. | |
6a488035 | 168 | */ |
83b2b36b | 169 | public static function send(array &$params): bool { |
e633aba2 | 170 | $defaultReturnPath = CRM_Core_BAO_MailSettings::defaultReturnPath(); |
6a488035 | 171 | $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId(); |
353ffa53 | 172 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); |
9c1bc317 | 173 | $from = $params['from'] ?? NULL; |
e633aba2 OT |
174 | if (!$defaultReturnPath) { |
175 | $defaultReturnPath = self::pluckEmailFromHeader($from); | |
6a488035 | 176 | } |
6a488035 TO |
177 | |
178 | // first call the mail alter hook | |
38108cce | 179 | CRM_Utils_Hook::alterMailParams($params, 'singleEmail'); |
6a488035 TO |
180 | |
181 | // check if any module has aborted mail sending | |
8cc574cf | 182 | if (!empty($params['abortMailSend']) || empty($params['toEmail'])) { |
6a488035 TO |
183 | return FALSE; |
184 | } | |
185 | ||
9c1bc317 CW |
186 | $textMessage = $params['text'] ?? NULL; |
187 | $htmlMessage = $params['html'] ?? NULL; | |
188 | $attachments = $params['attachments'] ?? NULL; | |
6a488035 TO |
189 | |
190 | // CRM-6224 | |
191 | if (trim(CRM_Utils_String::htmlToText($htmlMessage)) == '') { | |
192 | $htmlMessage = FALSE; | |
193 | } | |
194 | ||
be2fb01f | 195 | $headers = []; |
6a488035 | 196 | // CRM-10699 support custom email headers |
a7488080 | 197 | if (!empty($params['headers'])) { |
6a488035 TO |
198 | $headers = array_merge($headers, $params['headers']); |
199 | } | |
200 | $headers['From'] = $params['from']; | |
ae5ffbb7 TO |
201 | $headers['To'] = self::formatRFC822Email( |
202 | CRM_Utils_Array::value('toName', $params), | |
203 | CRM_Utils_Array::value('toEmail', $params), | |
204 | FALSE | |
205 | ); | |
c69abeee MW |
206 | |
207 | // On some servers mail() fails when 'Cc' or 'Bcc' headers are defined but empty. | |
208 | foreach (['Cc', 'Bcc'] as $optionalHeader) { | |
9c1bc317 | 209 | $headers[$optionalHeader] = $params[strtolower($optionalHeader)] ?? NULL; |
c69abeee MW |
210 | if (empty($headers[$optionalHeader])) { |
211 | unset($headers[$optionalHeader]); | |
212 | } | |
213 | } | |
214 | ||
9c1bc317 | 215 | $headers['Subject'] = $params['subject'] ?? NULL; |
6a488035 TO |
216 | $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8'; |
217 | $headers['Content-Disposition'] = 'inline'; | |
218 | $headers['Content-Transfer-Encoding'] = '8bit'; | |
e633aba2 | 219 | $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params, $defaultReturnPath); |
361fb6c0 | 220 | |
6a488035 | 221 | // CRM-11295: Omit reply-to headers if empty; this avoids issues with overzealous mailservers |
2e35712a | 222 | $replyTo = CRM_Utils_Array::value('replyTo', $params, CRM_Utils_Array::value('from', $params)); |
361fb6c0 | 223 | |
6a488035 TO |
224 | if (!empty($replyTo)) { |
225 | $headers['Reply-To'] = $replyTo; | |
226 | } | |
227 | $headers['Date'] = date('r'); | |
228 | if ($includeMessageId) { | |
52f4ecb2 | 229 | $headers['Message-ID'] = $params['messageId'] ?? '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; |
6a488035 | 230 | } |
a7488080 | 231 | if (!empty($params['autoSubmitted'])) { |
6a488035 TO |
232 | $headers['Auto-Submitted'] = "Auto-Generated"; |
233 | } | |
234 | ||
50bfb460 | 235 | // make sure we has to have space, CRM-6977 |
be2fb01f | 236 | foreach (['From', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path'] as $fld) { |
6a488035 TO |
237 | if (isset($headers[$fld])) { |
238 | $headers[$fld] = str_replace('"<', '" <', $headers[$fld]); | |
239 | } | |
240 | } | |
241 | ||
242 | // quote FROM, if comma is detected AND is not already quoted. CRM-7053 | |
243 | if (strpos($headers['From'], ',') !== FALSE) { | |
244 | $from = explode(' <', $headers['From']); | |
245 | $headers['From'] = self::formatRFC822Email( | |
246 | $from[0], | |
247 | substr(trim($from[1]), 0, -1), | |
248 | TRUE | |
249 | ); | |
250 | } | |
251 | ||
252 | require_once 'Mail/mime.php'; | |
253 | $msg = new Mail_mime("\n"); | |
254 | if ($textMessage) { | |
255 | $msg->setTxtBody($textMessage); | |
256 | } | |
257 | ||
258 | if ($htmlMessage) { | |
259 | $msg->setHTMLBody($htmlMessage); | |
260 | } | |
261 | ||
262 | if (!empty($attachments)) { | |
83b2b36b | 263 | foreach ($attachments as $attach) { |
6a488035 TO |
264 | $msg->addAttachment( |
265 | $attach['fullPath'], | |
266 | $attach['mime_type'], | |
d0d1c38b | 267 | $attach['cleanName'], |
268 | TRUE, | |
269 | 'base64', | |
270 | 'attachment', | |
271 | (isset($attach['charset']) ? $attach['charset'] : '') | |
6a488035 TO |
272 | ); |
273 | } | |
274 | } | |
275 | ||
276 | $message = self::setMimeParams($msg); | |
b2c0dbe4 | 277 | $headers = $msg->headers($headers); |
6a488035 | 278 | |
be2fb01f | 279 | $to = [$params['toEmail']]; |
048222df | 280 | $mailer = \Civi::service('pear_mail'); |
cbcec379 | 281 | |
8125eb18 NG |
282 | // CRM-3795, CRM-7355, CRM-7557, CRM-9058, CRM-9887, CRM-12883, CRM-19173 and others ... |
283 | // The PEAR library requires different parameters based on the mailer used: | |
284 | // * Mail_mail requires the Cc/Bcc recipients listed ONLY in the $headers variable | |
285 | // * All other mailers require that all be recipients be listed in the $to array AND that | |
286 | // the Bcc must not be present in $header as otherwise it will be shown to all recipients | |
287 | // ref: https://pear.php.net/bugs/bug.php?id=8047, full thread and answer [2011-04-19 20:48 UTC] | |
e4c75084 TO |
288 | // TODO: Refactor this quirk-handler as another filter in FilteredPearMailer. But that would merit review of impact on universe. |
289 | $driver = ($mailer instanceof CRM_Utils_Mail_FilteredPearMailer) ? $mailer->getDriver() : NULL; | |
290 | $isPhpMail = (get_class($mailer) === "Mail_mail" || $driver === 'mail'); | |
291 | if (!$isPhpMail) { | |
ea2eebc3 NG |
292 | // get emails from headers, since these are |
293 | // combination of name and email addresses. | |
e7292422 | 294 | if (!empty($headers['Cc'])) { |
9c1bc317 | 295 | $to[] = $headers['Cc'] ?? NULL; |
e7292422 TO |
296 | } |
297 | if (!empty($headers['Bcc'])) { | |
9c1bc317 | 298 | $to[] = $headers['Bcc'] ?? NULL; |
d39f2583 | 299 | unset($headers['Bcc']); |
e7292422 | 300 | } |
6a488035 | 301 | } |
8125eb18 | 302 | |
ea2eebc3 | 303 | if (is_object($mailer)) { |
c9d8809c | 304 | try { |
305 | $result = $mailer->send($to, $headers, $message); | |
306 | } | |
307 | catch (Exception $e) { | |
308 | CRM_Core_Session::setStatus($e->getMessage(), ts('Mailing Error'), 'error'); | |
309 | return FALSE; | |
310 | } | |
6a488035 TO |
311 | if (is_a($result, 'PEAR_Error')) { |
312 | $message = self::errorMessage($mailer, $result); | |
313 | // append error message in case multiple calls are being made to | |
314 | // this method in the course of sending a batch of messages. | |
361fb6c0 | 315 | CRM_Core_Session::setStatus($message, ts('Mailing Error'), 'error'); |
6a488035 TO |
316 | return FALSE; |
317 | } | |
318 | // CRM-10699 | |
319 | CRM_Utils_Hook::postEmailSend($params); | |
320 | return TRUE; | |
321 | } | |
322 | return FALSE; | |
323 | } | |
324 | ||
5bc392e6 EM |
325 | /** |
326 | * @param $mailer | |
327 | * @param $result | |
328 | * | |
329 | * @return string | |
330 | */ | |
00be9182 | 331 | public static function errorMessage($mailer, $result) { |
be2fb01f | 332 | $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.', [ |
6714d8d2 SL |
333 | 1 => 'SMTP', |
334 | ]) . '</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 |
335 | |
336 | if (is_a($mailer, 'Mail_smtp')) { | |
337 | $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>'; | |
338 | } | |
339 | else { | |
340 | $message .= '<ul>' . '<li>' . ts('Your Sendmail path is incorrect.') . '</li>' . '<li>' . ts('Your Sendmail argument is incorrect.') . '</li>'; | |
341 | } | |
342 | ||
be2fb01f | 343 | $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.', [ |
6714d8d2 SL |
344 | 1 => CRM_Utils_System::docURL2('user/advanced-configuration/email-system-configuration', TRUE), |
345 | ]) . '</p>'; | |
6a488035 TO |
346 | |
347 | return $message; | |
348 | } | |
349 | ||
5bc392e6 EM |
350 | /** |
351 | * @param $to | |
352 | * @param $headers | |
353 | * @param $message | |
dee6e582 | 354 | * @deprecated |
5bc392e6 | 355 | */ |
00be9182 | 356 | public static function logger(&$to, &$headers, &$message) { |
dee6e582 | 357 | CRM_Utils_Mail_Logger::log($to, $headers, $message); |
6a488035 TO |
358 | } |
359 | ||
360 | /** | |
361 | * Get the email address itself from a formatted full name + address string | |
362 | * | |
363 | * Ugly but working. | |
364 | * | |
77855840 TO |
365 | * @param string $header |
366 | * The full name + email address string. | |
6a488035 | 367 | * |
a6c01b45 CW |
368 | * @return string |
369 | * the plucked email address | |
6a488035 | 370 | */ |
00be9182 | 371 | public static function pluckEmailFromHeader($header) { |
6a488035 TO |
372 | preg_match('/<([^<]*)>$/', $header, $matches); |
373 | ||
374 | if (isset($matches[1])) { | |
375 | return $matches[1]; | |
376 | } | |
377 | return NULL; | |
378 | } | |
379 | ||
380 | /** | |
fe482240 | 381 | * Get the Active outBound email. |
6a488035 | 382 | * |
ae5ffbb7 TO |
383 | * @return bool |
384 | * TRUE if valid outBound email configuration found, false otherwise. | |
6a488035 | 385 | */ |
00be9182 | 386 | public static function validOutBoundMail() { |
aaffa79f | 387 | $mailingInfo = Civi::settings()->get('mailing_backend'); |
6a488035 TO |
388 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { |
389 | return TRUE; | |
390 | } | |
391 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
392 | if (!isset($mailingInfo['smtpServer']) || $mailingInfo['smtpServer'] == '' || | |
393 | $mailingInfo['smtpServer'] == 'YOUR SMTP SERVER' || | |
394 | ($mailingInfo['smtpAuth'] && ($mailingInfo['smtpUsername'] == '' || $mailingInfo['smtpPassword'] == '')) | |
395 | ) { | |
396 | return FALSE; | |
397 | } | |
398 | return TRUE; | |
399 | } | |
400 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
401 | if (!$mailingInfo['sendmail_path'] || !$mailingInfo['sendmail_args']) { | |
402 | return FALSE; | |
403 | } | |
404 | return TRUE; | |
405 | } | |
406 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) { | |
407 | return TRUE; | |
408 | } | |
409 | return FALSE; | |
410 | } | |
411 | ||
5bc392e6 EM |
412 | /** |
413 | * @param $message | |
100fef9d | 414 | * @param array $params |
5bc392e6 EM |
415 | * |
416 | * @return mixed | |
417 | */ | |
ca807bbe | 418 | public static function setMimeParams($message, $params = NULL) { |
6a488035 TO |
419 | static $mimeParams = NULL; |
420 | if (!$params) { | |
421 | if (!$mimeParams) { | |
be2fb01f | 422 | $mimeParams = [ |
6a488035 TO |
423 | 'text_encoding' => '8bit', |
424 | 'html_encoding' => '8bit', | |
425 | 'head_charset' => 'utf-8', | |
426 | 'text_charset' => 'utf-8', | |
427 | 'html_charset' => 'utf-8', | |
be2fb01f | 428 | ]; |
6a488035 TO |
429 | } |
430 | $params = $mimeParams; | |
431 | } | |
432 | return $message->get($params); | |
433 | } | |
434 | ||
5bc392e6 | 435 | /** |
100fef9d | 436 | * @param string $name |
5bc392e6 EM |
437 | * @param $email |
438 | * @param bool $useQuote | |
439 | * | |
440 | * @return null|string | |
441 | */ | |
00be9182 | 442 | public static function formatRFC822Email($name, $email, $useQuote = FALSE) { |
6a488035 TO |
443 | $result = NULL; |
444 | ||
445 | $name = trim($name); | |
446 | ||
447 | // strip out double quotes if present at the beginning AND end | |
448 | if (substr($name, 0, 1) == '"' && | |
449 | substr($name, -1, 1) == '"' | |
450 | ) { | |
451 | $name = substr($name, 1, -1); | |
452 | } | |
453 | ||
454 | if (!empty($name)) { | |
455 | // escape the special characters | |
be2fb01f CW |
456 | $name = str_replace(['<', '"', '>'], |
457 | ['\<', '\"', '\>'], | |
6a488035 TO |
458 | $name |
459 | ); | |
460 | if (strpos($name, ',') !== FALSE || | |
461 | $useQuote | |
462 | ) { | |
463 | // quote the string if it has a comma | |
464 | $name = '"' . $name . '"'; | |
465 | } | |
466 | ||
467 | $result = "$name "; | |
468 | } | |
469 | ||
470 | $result .= "<{$email}>"; | |
471 | return $result; | |
472 | } | |
473 | ||
474 | /** | |
475 | * Takes a string and checks to see if it needs to be escaped / double quoted | |
476 | * and if so does the needful and return the formatted name | |
477 | * | |
478 | * This code has been copied and adapted from ezc/Mail/src/tools.php | |
50bfb460 SB |
479 | * |
480 | * @param string $name | |
481 | * | |
482 | * @return string | |
6a488035 | 483 | */ |
00be9182 | 484 | public static function formatRFC2822Name($name) { |
6a488035 TO |
485 | $name = trim($name); |
486 | if (!empty($name)) { | |
487 | // remove the quotes around the name part if they are already there | |
488 | if (substr($name, 0, 1) == '"' && substr($name, -1) == '"') { | |
489 | $name = substr($name, 1, -1); | |
490 | } | |
491 | ||
492 | // add slashes to " and \ and surround the name part with quotes | |
493 | if (strpbrk($name, ",@<>:;'\"") !== FALSE) { | |
494 | $name = '"' . addcslashes($name, '\\"') . '"'; | |
495 | } | |
496 | } | |
497 | ||
498 | return $name; | |
499 | } | |
383c047b DG |
500 | |
501 | /** | |
383c047b DG |
502 | * @param string $fileName |
503 | * @param string $html | |
504 | * @param string $format | |
505 | * | |
a6c01b45 | 506 | * @return array |
383c047b | 507 | */ |
00be9182 | 508 | public static function appendPDF($fileName, $html, $format = NULL) { |
383c047b DG |
509 | $pdf_filename = CRM_Core_Config::singleton()->templateCompileDir . CRM_Utils_File::makeFileName($fileName); |
510 | ||
50bfb460 SB |
511 | // FIXME : CRM-7894 |
512 | // xmlns attribute is required in XHTML but it is invalid in HTML, | |
513 | // Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, | |
514 | // and will be added to the <html> tag even if you do not include it. | |
383c047b DG |
515 | $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\s]+["\'])(.+)?(>)/', '\1\3\4', $html); |
516 | ||
517 | file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html, | |
353ffa53 TO |
518 | $fileName, |
519 | TRUE, | |
520 | $format) | |
383c047b | 521 | ); |
be2fb01f | 522 | return [ |
383c047b DG |
523 | 'fullPath' => $pdf_filename, |
524 | 'mime_type' => 'application/pdf', | |
525 | 'cleanName' => $fileName, | |
be2fb01f | 526 | ]; |
383c047b | 527 | } |
96025800 | 528 | |
2a7e1ddc TS |
529 | /** |
530 | * Format an email string from email fields. | |
531 | * | |
532 | * @param array $fields | |
533 | * The email fields. | |
534 | * @return string | |
535 | * The formatted email string. | |
536 | */ | |
537 | public static function format($fields) { | |
538 | $formattedEmail = ''; | |
539 | if (!empty($fields['email'])) { | |
540 | $formattedEmail = $fields['email']; | |
541 | } | |
542 | ||
be2fb01f | 543 | $formattedSuffix = []; |
2a7e1ddc TS |
544 | if (!empty($fields['is_bulkmail'])) { |
545 | $formattedSuffix[] = '(' . ts('Bulk') . ')'; | |
546 | } | |
547 | if (!empty($fields['on_hold'])) { | |
548 | if ($fields['on_hold'] == 2) { | |
549 | $formattedSuffix[] = '(' . ts('On Hold - Opt Out') . ')'; | |
550 | } | |
551 | else { | |
552 | $formattedSuffix[] = '(' . ts('On Hold') . ')'; | |
553 | } | |
554 | } | |
555 | if (!empty($fields['signature_html']) || !empty($fields['signature_text'])) { | |
556 | $formattedSuffix[] = '(' . ts('Signature') . ')'; | |
557 | } | |
558 | ||
559 | // Add suffixes on a new line, if there is any. | |
560 | if (!empty($formattedSuffix)) { | |
561 | $formattedEmail .= "\n" . implode(' ', $formattedSuffix); | |
562 | } | |
563 | ||
564 | return $formattedEmail; | |
565 | } | |
566 | ||
a7180974 JG |
567 | /** |
568 | * When passed a value, returns the value if it's non-numeric. | |
569 | * If it's numeric, look up the display name and email of the corresponding | |
570 | * contact ID in RFC822 format. | |
571 | * | |
4335f229 JG |
572 | * @param string $from |
573 | * contact ID or formatted "From address", eg. 12 or "Fred Bloggs" <fred@example.org> | |
a7180974 JG |
574 | * @return string |
575 | * The RFC822-formatted email header (display name + address) | |
576 | */ | |
4335f229 JG |
577 | public static function formatFromAddress($from) { |
578 | if (is_numeric($from)) { | |
a7180974 | 579 | $result = civicrm_api3('Email', 'get', [ |
4335f229 | 580 | 'id' => $from, |
a7180974 JG |
581 | 'return' => ['contact_id.display_name', 'email'], |
582 | 'sequential' => 1, | |
583 | ])['values'][0]; | |
4335f229 | 584 | $from = '"' . $result['contact_id.display_name'] . '" <' . $result['email'] . '>'; |
a7180974 | 585 | } |
4335f229 | 586 | return $from; |
a7180974 JG |
587 | } |
588 | ||
6a488035 | 589 | } |