3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
11 namespace Civi\FlexMailer
;
16 * Within CiviMail, we have a few competing data-structures to chose from when
17 * representing a mail message:
21 * - $mailParams (from Hook::alterMailParams)
23 * The $mailParams data-structure is probably the quirkiest, but it's also
24 * the one for which we have the strongest obligation (e.g. it's part of
25 * a published hook). This class includes helper functions for
26 * converting or validating the $mailParams format.
28 * @see \CRM_Utils_Hook::alterMailParams
33 * Convert from "mail params" to PEAR's Mail_mime.
35 * The data-structure which represents a message for purposes of
36 * hook_civicrm_alterMailParams does not match the data structure for
39 * @param array $mailParams
41 * @see \CRM_Utils_Hook::alterMailParams
43 public static function convertMailParamsToMime($mailParams) {
44 // The general assumption is that key-value pairs in $mailParams should
45 // pass through as email headers, but there are several special-cases
46 // (e.g. 'toName', 'toEmail', 'text', 'html', 'attachments', 'headers').
48 $message = new \
Mail_mime("\n");
50 // 1. Consolidate: 'toName' and 'toEmail' should be 'To'.
51 $toName = trim($mailParams['toName']);
52 $toEmail = trim($mailParams['toEmail']);
53 if ($toName == $toEmail ||
strpos($toName, '@') !== FALSE) {
57 $toName = \CRM_Utils_Mail
::formatRFC2822Name($toName);
59 unset($mailParams['toName']);
60 unset($mailParams['toEmail']);
61 $mailParams['To'] = "$toName <$toEmail>";
63 // 2. Apply the other fields.
64 foreach ($mailParams as $key => $value) {
71 $message->setTxtBody($mailParams['text']);
75 $message->setHTMLBody($mailParams['html']);
79 foreach ($mailParams['attachments'] as $fileID => $attach) {
80 $message->addAttachment($attach['fullPath'],
88 $message->headers($value);
92 $message->headers(array($key => $value), TRUE);
96 \CRM_Utils_Mail
::setMimeParams($message);