* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
+use Civi\Api4\Email;
use Civi\Api4\MessageTemplate;
use Civi\WorkflowMessage\WorkflowMessage;
$params = array_merge($modelDefaults, $viewDefaults, $envelopeDefaults, $params);
CRM_Utils_Hook::alterMailParams($params, 'messageTemplate');
- if (!is_int($params['messageTemplateID']) && !is_null($params['messageTemplateID'])) {
- CRM_Core_Error::deprecatedWarning('message template id should be an integer');
- $params['messageTemplateID'] = (int) $params['messageTemplateID'];
- }
$mailContent = self::loadTemplate((string) $params['valueName'], $params['isTest'], $params['messageTemplateID'] ?? NULL, $params['groupName'] ?? '', $params['messageTemplate'], $params['subject'] ?? NULL);
$params['tokenContext'] = array_merge([
$params['html'] = $mailContent['html'];
if ($params['toEmail']) {
- $contactParams = [['email', 'LIKE', $params['toEmail'], 0, 1]];
- [$contact] = CRM_Contact_BAO_Query::apiQuery($contactParams);
-
- $prefs = array_pop($contact);
-
- if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] === 'HTML') {
+ // @todo - consider whether we really should be loading
+ // this based on 'the first email in the db that matches'.
+ // when we likely have the contact id. OTOH people probably barely
+ // use preferredMailFormat these days - the good fight against html
+ // emails was lost a decade ago...
+ $preferredMailFormatArray = Email::get(FALSE)->addWhere('email', '=', $params['toEmail'])->addSelect('contact_id.preferred_mail_format')->execute()->first();
+ $preferredMailFormat = $preferredMailFormatArray['contact_id.preferred_mail_format'] ?? 'Both';
+
+ if ($preferredMailFormat === 'HTML') {
$params['text'] = NULL;
}
-
- if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] === 'Text') {
+ if ($preferredMailFormat === 'Text') {
$params['html'] = NULL;
}