Fix sending issues found by matt and expand unit test to cover sending of subscription emails
$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,
+ 'toEmail' => $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?
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);
}
/**
}
}
- $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;
$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() . '>',
+ 'toEmail' => $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);
}
}
$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}>",
+ 'toEmail' => $email,
+ 'replyTo' => $confirm,
+ 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
];
$url = CRM_Utils_System::url('civicrm/mailing/confirm',
// 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);
}
/**
}
}
- $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;
$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() . '>',
+ 'toEmail' => $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);
}
/**
* text : text of the message
* html : html version of the message
* replyTo : reply-to header in the email
+ * returnpath : email address for bounces to be sent to
+ * messageId : Message ID for this email mesage
* attachments: an associative array of
* fullPath : complete pathname to the file
* mime_type: mime type of the attachment
}
$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";
* Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
*/
public function testMailerProcess() {
+ $this->callAPISuccess('MailSettings', 'create', [
+ 'domain_id' => 1,
+ 'name' => "my mail setting",
+ 'domain' => 'setting.com',
+ 'localpart' => 'civicrm+',
+ 'server' => "localhost",
+ 'username' => 'sue',
+ 'password' => 'pass',
+ 'is_default' => 1,
+ ]);
+ $mut = new CiviMailUtils($this, TRUE);
+ Civi::settings()->set('include_message_id', 1);
$params = [
'first_name' => 'Test',
'last_name' => 'Test',
'time_stamp' => '20101212121212',
];
$result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
+ // Check that subscription email has been sent.
+ $msgs = $mut->getAllMessages();
+ $this->assertCount(1, $msgs, 'Subscription email failed to send');
+ $mut->checkMailLog([
+ 'Message-ID: <civicrm+s',
+ 'To confirm this subscription, reply to this email or click',
+ ]);
$this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
$this->callAPISuccess('mailing_event_confirm', 'create', $params);
$this->contactDelete($contactID);
+ Civi::settings()->set('include_message_id', 0);
}
}