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