Skip to content

Commit

Permalink
dev/core#3177 Switch sms to use flexmailer token rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed May 20, 2022
1 parent 7b79a6f commit f3a27bf
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 56 deletions.
19 changes: 2 additions & 17 deletions CRM/Mailing/BAO/MailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function create($params) {
* @return bool|null
*/
public static function runJobs($testParams = NULL, $mode = NULL) {
$job = new CRM_Mailing_BAO_MailingJob();
$job = $mode === 'sms' ? new CRM_Mailing_BAO_SMSJob() : new CRM_Mailing_BAO_MailingJob();

$jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
$mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
Expand Down Expand Up @@ -580,23 +580,14 @@ public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attach
$mail_sync_interval = Civi::settings()->get('civimail_sync_interval');
$retryGroup = FALSE;

// CRM-15702: Sending bulk sms to contacts without e-mail address fails.
// Solution is to skip checking for on hold
//do include a statement to check wether e-mail address is on hold
$skipOnHold = TRUE;
if ($mailing->sms_provider_id) {
//do not include a statement to check wether e-mail address is on hold
$skipOnHold = FALSE;
}

foreach ($fields as $key => $field) {
$params[] = $field['contact_id'];
}

[$details] = CRM_Utils_Token::getTokenDetails(
$params,
$returnProperties,
$skipOnHold, TRUE, NULL,
TRUE, TRUE, NULL,
$mailing->getFlattenedTokens(),
get_class($this),
$this->id
Expand Down Expand Up @@ -633,12 +624,6 @@ public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attach
$body = $message->get();
$headers = $message->headers();

if ($mailing->sms_provider_id) {
$provider = CRM_SMS_Provider::singleton(['mailing_id' => $mailing->id]);
$body = $provider->getMessage($message, $field['contact_id'], $details[$contactID]);
$headers = $provider->getRecipientDetails($field, $details[$contactID]);
}

// make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
$recipient = $headers['To'];
$result = NULL;
Expand Down
108 changes: 108 additions & 0 deletions CRM/Mailing/BAO/SMSJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* Job for SMS deliery functions.
*/
class CRM_Mailing_BAO_SMSJob extends CRM_Mailing_BAO_MailingJob {

/**
* This is used by CiviMail but will be made redundant by FlexMailer.
* @param array $fields
* List of intended recipients.
* Each recipient is an array with keys 'hash', 'contact_id', 'email', etc.
* @param $mailing
* @param $mailer
* @param $job_date
* @param $attachments
*
* @return bool|null
* @throws Exception
*/
public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) {
$count = 0;
// dev/core#1768 Get the mail sync interval.
$mail_sync_interval = Civi::settings()->get('civimail_sync_interval');
$retryGroup = FALSE;

foreach ($fields as $field) {
$contact = civicrm_api3('Contact', 'getsingle', ['id' => $field['contact_id']]);

$preview = civicrm_api3('Mailing', 'preview', [
'id' => $mailing->id,
'contact_id' => $field['contact_id'],
])['values'];
$mailParams = [
'text' => $preview['body_text'],
'toName' => $contact['display_name'],
'job_id' => $this->id,
];
CRM_Utils_Hook::alterMailParams($mailParams, 'civimail');
$body = $mailParams['text'];
$headers = ['To' => $fields['phone']];

try {
$result = $mailer->send($headers['To'], $headers, $body, $this->id);

// Register the delivery event.
$deliveredParams[] = $field['id'];
$targetParams[] = $field['contact_id'];

$count++;
// dev/core#1768 Mail sync interval is now configurable.
if ($count % $mail_sync_interval == 0) {
$this->writeToDB(
$deliveredParams,
$targetParams,
$mailing,
$job_date
);
$count = 0;

// hack to stop mailing job at run time, CRM-4246.
// to avoid making too many DB calls for this rare case
// lets do it when we snapshot
$status = CRM_Core_DAO::getFieldValue(
'CRM_Mailing_DAO_MailingJob',
$this->id,
'status',
'id',
TRUE
);

if ($status !== 'Running') {
return FALSE;
}
}
}
catch (CRM_Core_Exception $e) {
// Handle SMS errors: CRM-15426
$job_id = (int) $this->id;
$mailing_id = (int) $mailing->id;
CRM_Core_Error::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows.");
CRM_Core_Error::debug_log_message($e->getMessage());
}

unset($result);

// If we have enabled the Throttle option, this is the time to enforce it.
$mailThrottleTime = Civi::settings()->get('mailThrottleTime');
if (!empty($mailThrottleTime)) {
usleep((int ) $mailThrottleTime);
}
}

$result = $this->writeToDB(
$deliveredParams,
$targetParams,
$mailing,
$job_date
);

if ($retryGroup) {
return FALSE;
}

return $result;
}

}
2 changes: 1 addition & 1 deletion CRM/Mailing/Page/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FAL
$mailing = $result['values'] ?? NULL;

$title = NULL;
if (isset($mailing['body_html']) && empty($_GET['text'])) {
if (!empty($mailing['body_html']) && empty($_GET['text'])) {
$header = 'text/html; charset=utf-8';
$content = $mailing['body_html'];
if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
Expand Down
32 changes: 0 additions & 32 deletions CRM/SMS/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,6 @@ public static function &singleton($providerParams = [], $force = FALSE) {
*/
abstract public function send($recipients, $header, $message, $dncID = NULL);

/**
* Return message text.
*
* Child class could override this function to have better control over the message being sent.
*
* @param Mail_mime $message
* @param int $contactID
* @param array $contactDetails
*
* @return string
*/
public function getMessage($message, $contactID, $contactDetails) {
$html = $message->getHTMLBody();
$text = $message->getTXTBody();

return $html ? $html : $text;
}

/**
* Get recipient details.
*
* @param array $fields
* @param array $additionalDetails
*
* @return mixed
*/
public function getRecipientDetails($fields, $additionalDetails) {
// we could do more altering here
$fields['To'] = $fields['phone'];
return $fields;
}

/**
* @param int $apiMsgID
* @param $message
Expand Down
2 changes: 1 addition & 1 deletion ext/flexmailer/src/API/MailingPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function preview($apiRequest) {
$mailing->copyValues($params);
}

if (!Abdicator::isFlexmailPreferred($mailing)) {
if (!Abdicator::isFlexmailPreferred($mailing) && empty($mailing->sms_provider_id)) {
require_once 'api/v3/Mailing.php';
return civicrm_api3_mailing_preview($params);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/flexmailer/src/Listener/DefaultComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function onCompose(ComposeBatchEvent $e) {
$this->createTokenProcessorContext($e));

$tpls = $this->createMessageTemplates($e);
$tp->addMessage('subject', $tpls['subject'], 'text/plain');
$tp->addMessage('subject', $tpls['subject'] ?? '', 'text/plain');
$tp->addMessage('body_text', isset($tpls['text']) ? $tpls['text'] : '',
'text/plain');
$tp->addMessage('body_html', isset($tpls['html']) ? $tpls['html'] : '',
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/CRM/SMS/PreviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function tearDown(): void {
/**
* Test SMS preview.
*/
public function testSMSPreview() {
public function testSMSPreview(): void {
$result = $this->callAPISuccess('SmsProvider', 'create', [
'title' => 'test SMS provider',
'username' => 'test',
Expand All @@ -41,10 +41,10 @@ public function testSMSPreview() {
$provider_id = $result['id'];
$result = $this->callAPISuccess('Mailing', 'create', [
'name' => "Test1",
'from_name' => "+12223334444",
'from_email' => "test@test.com",
'from_name' => '+12223334444',
'from_email' => 'test@test.com',
'replyto_email' => "test@test.com",
'body_text' => "Testing body",
'body_text' => 'Testing body',
'sms_provider_id' => $provider_id,
'header_id' => NULL,
'footer_id' => NULL,
Expand Down

0 comments on commit f3a27bf

Please sign in to comment.