From d3f76f5d068b89f53462bf9058ff86e74a251620 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 26 May 2020 15:36:28 +1000 Subject: [PATCH] [REF] Use CRM_Utils_Mail::send for sending emails for confirming unsubscribe resubscribe auto replies and subscribing --- CRM/Mailing/Event/BAO/Reply.php | 32 ++++++++++---------------- CRM/Mailing/Event/BAO/Resubscribe.php | 32 ++++++++++---------------- CRM/Mailing/Event/BAO/Subscribe.php | 31 ++++++++++--------------- CRM/Mailing/Event/BAO/Unsubscribe.php | 33 ++++++++++----------------- CRM/Utils/Mail.php | 2 +- 5 files changed, 49 insertions(+), 81 deletions(-) diff --git a/CRM/Mailing/Event/BAO/Reply.php b/CRM/Mailing/Event/BAO/Reply.php index 25fb8f4fff57..cc9848299cfb 100644 --- a/CRM/Mailing/Event/BAO/Reply.php +++ b/CRM/Mailing/Event/BAO/Reply.php @@ -161,7 +161,6 @@ public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTM else { $fromName = empty($eq->display_name) ? $eq->email : "{$eq->display_name} ({$eq->email})"; - $message = new Mail_mime("\n"); $headers = [ 'Subject' => "Re: {$mailing->subject}", @@ -227,17 +226,15 @@ private static function autoRespond(&$mailing, $queue_id, $replyto) { $component->id = $mailing->reply_id; $component->find(TRUE); - $message = new Mail_Mime("\n"); - $domain = CRM_Core_BAO_Domain::getDomain(); list($domainEmailName, $_) = CRM_Core_BAO_Domain::getNameAndEmail(); - $headers = [ - 'Subject' => $component->subject, - 'To' => $to, - 'From' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>', - 'Reply-To' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), - 'Return-Path' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + $params = [ + 'subject' => $component->subject, + 'to' => $to, + 'from' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>', + 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), ]; // TODO: do we need reply tokens? @@ -257,24 +254,19 @@ private static function autoRespond(&$mailing, $queue_id, $replyto) { if ($eq->format == 'HTML' || $eq->format == 'Both') { $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); $html = CRM_Utils_Token::replaceMailingTokens($html, $mailing, NULL, $tokens['html']); - $message->setHTMLBody($html); } if (!$html || $eq->format == 'Text' || $eq->format == 'Both') { $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']); $text = CRM_Utils_Token::replaceMailingTokens($text, $mailing, NULL, $tokens['text']); - $message->setTxtBody($text); } + $params['html'] = $html; + $params['text'] = $text; - $b = CRM_Utils_Mail::setMimeParams($message); - $h = $message->headers($headers); - CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 'a', $eq->job_id, queue_id, $eq->hash); - - $mailer = \Civi::service('pear_mail'); - if (is_object($mailer)) { - $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); - $mailer->send($to, $h, $b); - unset($errorScope); + CRM_Mailing_BAO_Mailing::addMessageIdHeader($params, 'a', $eq->job_id, queue_id, $eq->hash); + if (CRM_Core_BAO_MailSettings::includeMessageId()) { + $params['messageId'] = $params['Message-ID']; } + CRM_Utils_Mail::send($params); } /** diff --git a/CRM/Mailing/Event/BAO/Resubscribe.php b/CRM/Mailing/Event/BAO/Resubscribe.php index e13ee159b613..cc83158f90db 100644 --- a/CRM/Mailing/Event/BAO/Resubscribe.php +++ b/CRM/Mailing/Event/BAO/Resubscribe.php @@ -229,8 +229,6 @@ public static function send_resub_response($queue_id, $groups, $is_domain = FALS } } - $message = new Mail_mime("\n"); - list($addresses, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls($job, $queue_id, $eq->hash, $eq->email); $bao = new CRM_Mailing_BAO_Mailing(); $bao->body_text = $text; @@ -241,34 +239,28 @@ public static function send_resub_response($queue_id, $groups, $is_domain = FALS $html = CRM_Utils_Token::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id, $eq->hash); $html = CRM_Utils_Token::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']); $html = CRM_Utils_Token::replaceMailingTokens($html, $dao, NULL, $tokens['html']); - $message->setHTMLBody($html); } if (!$html || $eq->format == 'Text' || $eq->format == 'Both') { $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, TRUE, $tokens['text']); $text = CRM_Utils_Token::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id, $eq->hash); $text = CRM_Utils_Token::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']); $text = CRM_Utils_Token::replaceMailingTokens($text, $dao, NULL, $tokens['text']); - $message->setTxtBody($text); } - $headers = [ - 'Subject' => $component->subject, - 'From' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>', - 'To' => $eq->email, - 'Reply-To' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), - 'Return-Path' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + $params = [ + 'subject' => $component->subject, + 'from' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>', + 'to' => $eq->email, + 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + 'html' => $html, + 'text' => $text, ]; - CRM_Mailing_BAO_Mailing::addMessageIdHeader($headers, 'e', $job, $queue_id, $eq->hash); - $b = CRM_Utils_Mail::setMimeParams($message); - $h = $message->headers($headers); - - $mailer = \Civi::service('pear_mail'); - - if (is_object($mailer)) { - $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); - $mailer->send($eq->email, $h, $b); - unset($errorScope); + CRM_Mailing_BAO_Mailing::addMessageIdHeader($params, 'e', $job, $queue_id, $eq->hash); + if (CRM_Core_BAO_MailSettings::includeMessageId()) { + $params['messageId'] = $params['Message-ID']; } + CRM_Utils_Mail::send($params); } } diff --git a/CRM/Mailing/Event/BAO/Subscribe.php b/CRM/Mailing/Event/BAO/Subscribe.php index 1dfad54a4a42..e2a3b3a07d00 100644 --- a/CRM/Mailing/Event/BAO/Subscribe.php +++ b/CRM/Mailing/Event/BAO/Subscribe.php @@ -205,12 +205,12 @@ public function send_confirm_request($email) { $component->find(TRUE); - $headers = [ - 'Subject' => $component->subject, - 'From' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", - 'To' => $email, - 'Reply-To' => $confirm, - 'Return-Path' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + $params = [ + 'subject' => $component->subject, + 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", + 'to' => $email, + 'replyTo' => $confirm, + 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), ]; $url = CRM_Utils_System::url('civicrm/mailing/confirm', @@ -246,24 +246,17 @@ public function send_confirm_request($email) { // render the & entities in text mode, so that the links work $text = str_replace('&', '&', $text); - $message = new Mail_mime("\n"); - - $message->setHTMLBody($html); - $message->setTxtBody($text); - $b = CRM_Utils_Mail::setMimeParams($message); - $h = $message->headers($headers); - CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 's', + CRM_Mailing_BAO_Mailing::addMessageIdHeader($params, 's', $this->contact_id, $this->id, $this->hash ); - $mailer = \Civi::service('pear_mail'); - - if (is_object($mailer)) { - $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); - $mailer->send($email, $h, $b); - unset($errorScope); + $params['html'] = $html; + $params['text'] = $text; + if (CRM_Core_BAO_MailSettings::includeMessageId()) { + $params['messageId'] = $params['Message-ID']; } + CRM_Utils_Mail::send($params); } /** diff --git a/CRM/Mailing/Event/BAO/Unsubscribe.php b/CRM/Mailing/Event/BAO/Unsubscribe.php index 920ae7d3bcfe..a3e9a3894ae8 100644 --- a/CRM/Mailing/Event/BAO/Unsubscribe.php +++ b/CRM/Mailing/Event/BAO/Unsubscribe.php @@ -365,8 +365,6 @@ public static function send_unsub_response($queue_id, $groups, $is_domain = FALS } } - $message = new Mail_mime("\n"); - list($addresses, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls($job, $queue_id, $eq->hash, $eq->email); $bao = new CRM_Mailing_BAO_Mailing(); $bao->body_text = $text; @@ -377,37 +375,30 @@ public static function send_unsub_response($queue_id, $groups, $is_domain = FALS $html = CRM_Utils_Token::replaceUnsubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id, $eq->hash); $html = CRM_Utils_Token::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']); $html = CRM_Utils_Token::replaceMailingTokens($html, $dao, NULL, $tokens['html']); - $message->setHTMLBody($html); } if (!$html || $eq->format == 'Text' || $eq->format == 'Both') { $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']); $text = CRM_Utils_Token::replaceUnsubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id, $eq->hash); $text = CRM_Utils_Token::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']); $text = CRM_Utils_Token::replaceMailingTokens($text, $dao, NULL, $tokens['text']); - $message->setTxtBody($text); } $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); - $headers = [ - 'Subject' => $component->subject, - 'From' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>', - 'To' => $eq->email, - 'Reply-To' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), - 'Return-Path' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + $params = [ + 'subject' => $component->subject, + 'from' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>', + 'to' => $eq->email, + 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), + 'html' => $html, + 'text' => $text, ]; - CRM_Mailing_BAO_Mailing::addMessageIdHeader($headers, 'u', $job, $queue_id, $eq->hash); - - $b = CRM_Utils_Mail::setMimeParams($message); - $h = $message->headers($headers); - - $mailer = \Civi::service('pear_mail'); - - if (is_object($mailer)) { - $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); - $mailer->send($eq->email, $h, $b); - unset($errorScope); + CRM_Mailing_BAO_Mailing::addMessageIdHeader($params, 'u', $job, $queue_id, $eq->hash); + if (CRM_Core_BAO_MailSettings::includeMessageId()) { + $params['messageId'] = $params['Message-ID']; } + CRM_Utils_Mail::send($params); } /** diff --git a/CRM/Utils/Mail.php b/CRM/Utils/Mail.php index 2b6e5120a74a..033bff3698b7 100644 --- a/CRM/Utils/Mail.php +++ b/CRM/Utils/Mail.php @@ -223,7 +223,7 @@ public static function send(&$params) { } $headers['Date'] = date('r'); if ($includeMessageId) { - $headers['Message-ID'] = '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; + $headers['Message-ID'] = $params['messageId'] ?? '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; } if (!empty($params['autoSubmitted'])) { $headers['Auto-Submitted'] = "Auto-Generated";