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']) { | |
2efa8782 | 42 | Civi::log()->error(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 | ) { | |
2efa8782 | 86 | Civi::log()->error(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) { | |
2efa8782 | 101 | Civi::log()->info(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')])); |
be2fb01f | 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 { | |
2efa8782 | 105 | Civi::log()->error(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 | |
52dd4e08 | 162 | * contactId : contact id to send the email to (optional) |
6a488035 | 163 | * |
77855840 TO |
164 | * @param array $params |
165 | * (by reference). | |
6a488035 | 166 | * |
ae5ffbb7 TO |
167 | * @return bool |
168 | * TRUE if a mail was sent, else FALSE. | |
6a488035 | 169 | */ |
83b2b36b | 170 | public static function send(array &$params): bool { |
e633aba2 | 171 | $defaultReturnPath = CRM_Core_BAO_MailSettings::defaultReturnPath(); |
6a488035 | 172 | $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId(); |
353ffa53 | 173 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); |
9c1bc317 | 174 | $from = $params['from'] ?? NULL; |
e633aba2 OT |
175 | if (!$defaultReturnPath) { |
176 | $defaultReturnPath = self::pluckEmailFromHeader($from); | |
6a488035 | 177 | } |
6a488035 TO |
178 | |
179 | // first call the mail alter hook | |
38108cce | 180 | CRM_Utils_Hook::alterMailParams($params, 'singleEmail'); |
6a488035 TO |
181 | |
182 | // check if any module has aborted mail sending | |
8cc574cf | 183 | if (!empty($params['abortMailSend']) || empty($params['toEmail'])) { |
6a488035 TO |
184 | return FALSE; |
185 | } | |
186 | ||
9d0bc570 EM |
187 | $htmlMessage = $params['html'] ?? FALSE; |
188 | if (trim(CRM_Utils_String::htmlToText((string) $htmlMessage)) === '') { | |
6a488035 TO |
189 | $htmlMessage = FALSE; |
190 | } | |
9d0bc570 EM |
191 | $attachments = $params['attachments'] ?? NULL; |
192 | if (!empty($params['text'])) { | |
193 | $textMessage = $params['text']; | |
194 | } | |
195 | else { | |
196 | $textMessage = CRM_Utils_String::htmlToText($htmlMessage); | |
197 | // Render the & entities in text mode, so that the links work. | |
198 | // This is copied from the Action Schedule send code. | |
199 | $textMessage = str_replace('&', '&', $textMessage); | |
200 | } | |
6a488035 | 201 | |
be2fb01f | 202 | $headers = []; |
6a488035 | 203 | // CRM-10699 support custom email headers |
a7488080 | 204 | if (!empty($params['headers'])) { |
6a488035 TO |
205 | $headers = array_merge($headers, $params['headers']); |
206 | } | |
207 | $headers['From'] = $params['from']; | |
ae5ffbb7 TO |
208 | $headers['To'] = self::formatRFC822Email( |
209 | CRM_Utils_Array::value('toName', $params), | |
210 | CRM_Utils_Array::value('toEmail', $params), | |
211 | FALSE | |
212 | ); | |
c69abeee MW |
213 | |
214 | // On some servers mail() fails when 'Cc' or 'Bcc' headers are defined but empty. | |
215 | foreach (['Cc', 'Bcc'] as $optionalHeader) { | |
9c1bc317 | 216 | $headers[$optionalHeader] = $params[strtolower($optionalHeader)] ?? NULL; |
c69abeee MW |
217 | if (empty($headers[$optionalHeader])) { |
218 | unset($headers[$optionalHeader]); | |
219 | } | |
220 | } | |
221 | ||
9c1bc317 | 222 | $headers['Subject'] = $params['subject'] ?? NULL; |
6a488035 TO |
223 | $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8'; |
224 | $headers['Content-Disposition'] = 'inline'; | |
225 | $headers['Content-Transfer-Encoding'] = '8bit'; | |
e633aba2 | 226 | $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params, $defaultReturnPath); |
361fb6c0 | 227 | |
6a488035 | 228 | // CRM-11295: Omit reply-to headers if empty; this avoids issues with overzealous mailservers |
2e35712a | 229 | $replyTo = CRM_Utils_Array::value('replyTo', $params, CRM_Utils_Array::value('from', $params)); |
361fb6c0 | 230 | |
6a488035 TO |
231 | if (!empty($replyTo)) { |
232 | $headers['Reply-To'] = $replyTo; | |
233 | } | |
234 | $headers['Date'] = date('r'); | |
235 | if ($includeMessageId) { | |
52f4ecb2 | 236 | $headers['Message-ID'] = $params['messageId'] ?? '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; |
6a488035 | 237 | } |
a7488080 | 238 | if (!empty($params['autoSubmitted'])) { |
6a488035 TO |
239 | $headers['Auto-Submitted'] = "Auto-Generated"; |
240 | } | |
241 | ||
50bfb460 | 242 | // make sure we has to have space, CRM-6977 |
be2fb01f | 243 | foreach (['From', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path'] as $fld) { |
6a488035 TO |
244 | if (isset($headers[$fld])) { |
245 | $headers[$fld] = str_replace('"<', '" <', $headers[$fld]); | |
246 | } | |
247 | } | |
248 | ||
249 | // quote FROM, if comma is detected AND is not already quoted. CRM-7053 | |
250 | if (strpos($headers['From'], ',') !== FALSE) { | |
251 | $from = explode(' <', $headers['From']); | |
252 | $headers['From'] = self::formatRFC822Email( | |
253 | $from[0], | |
254 | substr(trim($from[1]), 0, -1), | |
255 | TRUE | |
256 | ); | |
257 | } | |
258 | ||
259 | require_once 'Mail/mime.php'; | |
260 | $msg = new Mail_mime("\n"); | |
261 | if ($textMessage) { | |
262 | $msg->setTxtBody($textMessage); | |
263 | } | |
264 | ||
265 | if ($htmlMessage) { | |
266 | $msg->setHTMLBody($htmlMessage); | |
267 | } | |
268 | ||
269 | if (!empty($attachments)) { | |
83b2b36b | 270 | foreach ($attachments as $attach) { |
6a488035 TO |
271 | $msg->addAttachment( |
272 | $attach['fullPath'], | |
273 | $attach['mime_type'], | |
d0d1c38b | 274 | $attach['cleanName'], |
275 | TRUE, | |
276 | 'base64', | |
277 | 'attachment', | |
278 | (isset($attach['charset']) ? $attach['charset'] : '') | |
6a488035 TO |
279 | ); |
280 | } | |
281 | } | |
282 | ||
283 | $message = self::setMimeParams($msg); | |
b2c0dbe4 | 284 | $headers = $msg->headers($headers); |
6a488035 | 285 | |
be2fb01f | 286 | $to = [$params['toEmail']]; |
048222df | 287 | $mailer = \Civi::service('pear_mail'); |
cbcec379 | 288 | |
8125eb18 NG |
289 | // CRM-3795, CRM-7355, CRM-7557, CRM-9058, CRM-9887, CRM-12883, CRM-19173 and others ... |
290 | // The PEAR library requires different parameters based on the mailer used: | |
291 | // * Mail_mail requires the Cc/Bcc recipients listed ONLY in the $headers variable | |
292 | // * All other mailers require that all be recipients be listed in the $to array AND that | |
293 | // the Bcc must not be present in $header as otherwise it will be shown to all recipients | |
294 | // ref: https://pear.php.net/bugs/bug.php?id=8047, full thread and answer [2011-04-19 20:48 UTC] | |
e4c75084 TO |
295 | // TODO: Refactor this quirk-handler as another filter in FilteredPearMailer. But that would merit review of impact on universe. |
296 | $driver = ($mailer instanceof CRM_Utils_Mail_FilteredPearMailer) ? $mailer->getDriver() : NULL; | |
297 | $isPhpMail = (get_class($mailer) === "Mail_mail" || $driver === 'mail'); | |
298 | if (!$isPhpMail) { | |
ea2eebc3 NG |
299 | // get emails from headers, since these are |
300 | // combination of name and email addresses. | |
e7292422 | 301 | if (!empty($headers['Cc'])) { |
9c1bc317 | 302 | $to[] = $headers['Cc'] ?? NULL; |
e7292422 TO |
303 | } |
304 | if (!empty($headers['Bcc'])) { | |
9c1bc317 | 305 | $to[] = $headers['Bcc'] ?? NULL; |
d39f2583 | 306 | unset($headers['Bcc']); |
e7292422 | 307 | } |
6a488035 | 308 | } |
8125eb18 | 309 | |
ea2eebc3 | 310 | if (is_object($mailer)) { |
c9d8809c | 311 | try { |
312 | $result = $mailer->send($to, $headers, $message); | |
313 | } | |
314 | catch (Exception $e) { | |
867052dc MW |
315 | \Civi::log()->error('Mailing error: ' . $e->getMessage()); |
316 | CRM_Core_Session::setStatus(ts('Unable to send email. Please report this message to the site administrator'), ts('Mailing Error'), 'error'); | |
c9d8809c | 317 | return FALSE; |
318 | } | |
6a488035 TO |
319 | if (is_a($result, 'PEAR_Error')) { |
320 | $message = self::errorMessage($mailer, $result); | |
321 | // append error message in case multiple calls are being made to | |
322 | // this method in the course of sending a batch of messages. | |
867052dc MW |
323 | \Civi::log()->error('Mailing error: ' . $message); |
324 | CRM_Core_Session::setStatus(ts('Unable to send email. Please report this message to the site administrator'), ts('Mailing Error'), 'error'); | |
6a488035 TO |
325 | return FALSE; |
326 | } | |
327 | // CRM-10699 | |
328 | CRM_Utils_Hook::postEmailSend($params); | |
329 | return TRUE; | |
330 | } | |
331 | return FALSE; | |
332 | } | |
333 | ||
5bc392e6 EM |
334 | /** |
335 | * @param $mailer | |
336 | * @param $result | |
337 | * | |
338 | * @return string | |
339 | */ | |
00be9182 | 340 | public static function errorMessage($mailer, $result) { |
be2fb01f | 341 | $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 |
342 | 1 => 'SMTP', |
343 | ]) . '</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 |
344 | |
345 | if (is_a($mailer, 'Mail_smtp')) { | |
346 | $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>'; | |
347 | } | |
348 | else { | |
349 | $message .= '<ul>' . '<li>' . ts('Your Sendmail path is incorrect.') . '</li>' . '<li>' . ts('Your Sendmail argument is incorrect.') . '</li>'; | |
350 | } | |
351 | ||
be2fb01f | 352 | $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 |
353 | 1 => CRM_Utils_System::docURL2('user/advanced-configuration/email-system-configuration', TRUE), |
354 | ]) . '</p>'; | |
6a488035 TO |
355 | |
356 | return $message; | |
357 | } | |
358 | ||
5bc392e6 EM |
359 | /** |
360 | * @param $to | |
361 | * @param $headers | |
362 | * @param $message | |
dee6e582 | 363 | * @deprecated |
5bc392e6 | 364 | */ |
00be9182 | 365 | public static function logger(&$to, &$headers, &$message) { |
dee6e582 | 366 | CRM_Utils_Mail_Logger::log($to, $headers, $message); |
6a488035 TO |
367 | } |
368 | ||
369 | /** | |
370 | * Get the email address itself from a formatted full name + address string | |
371 | * | |
372 | * Ugly but working. | |
373 | * | |
77855840 TO |
374 | * @param string $header |
375 | * The full name + email address string. | |
6a488035 | 376 | * |
a6c01b45 CW |
377 | * @return string |
378 | * the plucked email address | |
6a488035 | 379 | */ |
00be9182 | 380 | public static function pluckEmailFromHeader($header) { |
6a488035 TO |
381 | preg_match('/<([^<]*)>$/', $header, $matches); |
382 | ||
383 | if (isset($matches[1])) { | |
384 | return $matches[1]; | |
385 | } | |
386 | return NULL; | |
387 | } | |
388 | ||
389 | /** | |
fe482240 | 390 | * Get the Active outBound email. |
6a488035 | 391 | * |
ae5ffbb7 TO |
392 | * @return bool |
393 | * TRUE if valid outBound email configuration found, false otherwise. | |
6a488035 | 394 | */ |
00be9182 | 395 | public static function validOutBoundMail() { |
aaffa79f | 396 | $mailingInfo = Civi::settings()->get('mailing_backend'); |
6a488035 TO |
397 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { |
398 | return TRUE; | |
399 | } | |
400 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
401 | if (!isset($mailingInfo['smtpServer']) || $mailingInfo['smtpServer'] == '' || | |
402 | $mailingInfo['smtpServer'] == 'YOUR SMTP SERVER' || | |
403 | ($mailingInfo['smtpAuth'] && ($mailingInfo['smtpUsername'] == '' || $mailingInfo['smtpPassword'] == '')) | |
404 | ) { | |
405 | return FALSE; | |
406 | } | |
407 | return TRUE; | |
408 | } | |
409 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
410 | if (!$mailingInfo['sendmail_path'] || !$mailingInfo['sendmail_args']) { | |
411 | return FALSE; | |
412 | } | |
413 | return TRUE; | |
414 | } | |
415 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) { | |
416 | return TRUE; | |
417 | } | |
418 | return FALSE; | |
419 | } | |
420 | ||
5bc392e6 EM |
421 | /** |
422 | * @param $message | |
100fef9d | 423 | * @param array $params |
5bc392e6 EM |
424 | * |
425 | * @return mixed | |
426 | */ | |
ca807bbe | 427 | public static function setMimeParams($message, $params = NULL) { |
6a488035 TO |
428 | static $mimeParams = NULL; |
429 | if (!$params) { | |
430 | if (!$mimeParams) { | |
be2fb01f | 431 | $mimeParams = [ |
6a488035 TO |
432 | 'text_encoding' => '8bit', |
433 | 'html_encoding' => '8bit', | |
434 | 'head_charset' => 'utf-8', | |
435 | 'text_charset' => 'utf-8', | |
436 | 'html_charset' => 'utf-8', | |
be2fb01f | 437 | ]; |
6a488035 TO |
438 | } |
439 | $params = $mimeParams; | |
440 | } | |
441 | return $message->get($params); | |
442 | } | |
443 | ||
5bc392e6 | 444 | /** |
100fef9d | 445 | * @param string $name |
1dd07102 | 446 | * @param string $email |
5bc392e6 EM |
447 | * @param bool $useQuote |
448 | * | |
449 | * @return null|string | |
450 | */ | |
00be9182 | 451 | public static function formatRFC822Email($name, $email, $useQuote = FALSE) { |
6a488035 TO |
452 | $result = NULL; |
453 | ||
454 | $name = trim($name); | |
455 | ||
456 | // strip out double quotes if present at the beginning AND end | |
457 | if (substr($name, 0, 1) == '"' && | |
458 | substr($name, -1, 1) == '"' | |
459 | ) { | |
460 | $name = substr($name, 1, -1); | |
461 | } | |
462 | ||
463 | if (!empty($name)) { | |
464 | // escape the special characters | |
be2fb01f CW |
465 | $name = str_replace(['<', '"', '>'], |
466 | ['\<', '\"', '\>'], | |
6a488035 TO |
467 | $name |
468 | ); | |
469 | if (strpos($name, ',') !== FALSE || | |
470 | $useQuote | |
471 | ) { | |
472 | // quote the string if it has a comma | |
473 | $name = '"' . $name . '"'; | |
474 | } | |
475 | ||
476 | $result = "$name "; | |
477 | } | |
478 | ||
479 | $result .= "<{$email}>"; | |
480 | return $result; | |
481 | } | |
482 | ||
483 | /** | |
484 | * Takes a string and checks to see if it needs to be escaped / double quoted | |
485 | * and if so does the needful and return the formatted name | |
486 | * | |
487 | * This code has been copied and adapted from ezc/Mail/src/tools.php | |
50bfb460 SB |
488 | * |
489 | * @param string $name | |
490 | * | |
491 | * @return string | |
6a488035 | 492 | */ |
00be9182 | 493 | public static function formatRFC2822Name($name) { |
6a488035 TO |
494 | $name = trim($name); |
495 | if (!empty($name)) { | |
496 | // remove the quotes around the name part if they are already there | |
497 | if (substr($name, 0, 1) == '"' && substr($name, -1) == '"') { | |
498 | $name = substr($name, 1, -1); | |
499 | } | |
500 | ||
501 | // add slashes to " and \ and surround the name part with quotes | |
502 | if (strpbrk($name, ",@<>:;'\"") !== FALSE) { | |
503 | $name = '"' . addcslashes($name, '\\"') . '"'; | |
504 | } | |
505 | } | |
506 | ||
507 | return $name; | |
508 | } | |
383c047b DG |
509 | |
510 | /** | |
383c047b DG |
511 | * @param string $fileName |
512 | * @param string $html | |
513 | * @param string $format | |
514 | * | |
a6c01b45 | 515 | * @return array |
383c047b | 516 | */ |
00be9182 | 517 | public static function appendPDF($fileName, $html, $format = NULL) { |
383c047b DG |
518 | $pdf_filename = CRM_Core_Config::singleton()->templateCompileDir . CRM_Utils_File::makeFileName($fileName); |
519 | ||
50bfb460 SB |
520 | // FIXME : CRM-7894 |
521 | // xmlns attribute is required in XHTML but it is invalid in HTML, | |
522 | // Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, | |
523 | // and will be added to the <html> tag even if you do not include it. | |
383c047b DG |
524 | $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\s]+["\'])(.+)?(>)/', '\1\3\4', $html); |
525 | ||
526 | file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html, | |
353ffa53 TO |
527 | $fileName, |
528 | TRUE, | |
529 | $format) | |
383c047b | 530 | ); |
be2fb01f | 531 | return [ |
383c047b DG |
532 | 'fullPath' => $pdf_filename, |
533 | 'mime_type' => 'application/pdf', | |
534 | 'cleanName' => $fileName, | |
be2fb01f | 535 | ]; |
383c047b | 536 | } |
96025800 | 537 | |
2a7e1ddc TS |
538 | /** |
539 | * Format an email string from email fields. | |
540 | * | |
541 | * @param array $fields | |
542 | * The email fields. | |
543 | * @return string | |
544 | * The formatted email string. | |
545 | */ | |
546 | public static function format($fields) { | |
547 | $formattedEmail = ''; | |
548 | if (!empty($fields['email'])) { | |
549 | $formattedEmail = $fields['email']; | |
550 | } | |
551 | ||
be2fb01f | 552 | $formattedSuffix = []; |
2a7e1ddc TS |
553 | if (!empty($fields['is_bulkmail'])) { |
554 | $formattedSuffix[] = '(' . ts('Bulk') . ')'; | |
555 | } | |
556 | if (!empty($fields['on_hold'])) { | |
557 | if ($fields['on_hold'] == 2) { | |
558 | $formattedSuffix[] = '(' . ts('On Hold - Opt Out') . ')'; | |
559 | } | |
560 | else { | |
561 | $formattedSuffix[] = '(' . ts('On Hold') . ')'; | |
562 | } | |
563 | } | |
564 | if (!empty($fields['signature_html']) || !empty($fields['signature_text'])) { | |
565 | $formattedSuffix[] = '(' . ts('Signature') . ')'; | |
566 | } | |
567 | ||
568 | // Add suffixes on a new line, if there is any. | |
569 | if (!empty($formattedSuffix)) { | |
570 | $formattedEmail .= "\n" . implode(' ', $formattedSuffix); | |
571 | } | |
572 | ||
573 | return $formattedEmail; | |
574 | } | |
575 | ||
a7180974 JG |
576 | /** |
577 | * When passed a value, returns the value if it's non-numeric. | |
578 | * If it's numeric, look up the display name and email of the corresponding | |
579 | * contact ID in RFC822 format. | |
580 | * | |
4335f229 | 581 | * @param string $from |
b21f0248 | 582 | * civicrm_email.id or formatted "From address", eg. 12 or "Fred Bloggs" <fred@example.org> |
a7180974 JG |
583 | * @return string |
584 | * The RFC822-formatted email header (display name + address) | |
585 | */ | |
4335f229 JG |
586 | public static function formatFromAddress($from) { |
587 | if (is_numeric($from)) { | |
a7180974 | 588 | $result = civicrm_api3('Email', 'get', [ |
4335f229 | 589 | 'id' => $from, |
a7180974 JG |
590 | 'return' => ['contact_id.display_name', 'email'], |
591 | 'sequential' => 1, | |
592 | ])['values'][0]; | |
4335f229 | 593 | $from = '"' . $result['contact_id.display_name'] . '" <' . $result['email'] . '>'; |
a7180974 | 594 | } |
4335f229 | 595 | return $from; |
a7180974 JG |
596 | } |
597 | ||
6a488035 | 598 | } |