Skip to content

Commit

Permalink
[REF] Use CRM_Utils_Mail::send for sending emails for confirming unsu…
Browse files Browse the repository at this point in the history
…bscribe resubscribe auto replies and subscribing
  • Loading branch information
seamuslee001 committed May 27, 2020
1 parent 83ca20b commit d3f76f5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 81 deletions.
32 changes: 12 additions & 20 deletions CRM/Mailing/Event/BAO/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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?
Expand All @@ -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);
}

/**
Expand Down
32 changes: 12 additions & 20 deletions CRM/Mailing/Event/BAO/Resubscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
31 changes: 12 additions & 19 deletions CRM/Mailing/Event/BAO/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -246,24 +246,17 @@ public function send_confirm_request($email) {
// render the &amp; entities in text mode, so that the links work
$text = str_replace('&amp;', '&', $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);
}

/**
Expand Down
33 changes: 12 additions & 21 deletions CRM/Mailing/Event/BAO/Unsubscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit d3f76f5

Please sign in to comment.