Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(develop) Metrolist listing form not being emailed to users. (hotfix) DIG-3674 #3266

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/default/bos_email.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ alerts:
recipient: [email protected]
token: '1'
honeypot: '1'
monitor:
recipient: [email protected]
all: '1'
hardbounce:
hardbounce: '1'
recipient: [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface
->set("alerts.recipient", $input["alerts"]["conditions"]["recipient"] ?? "")
->set("hardbounce.hardbounce", $input["alerts"]["hb"]["hardbounce"] ?? 0)
->set("hardbounce.recipient", $input["alerts"]["hb"]["recipient"] ?? "")
->set("alerts.recipient", $input["alerts"]["conditions"]["recipient"] ?? 0)
->set("alerts.token", $input["alerts"]["conditions"]["token"] ?? 0)
->set("alerts.honeypot", $input["alerts"]["conditions"]["honeypot"] ?? 0)
->set("monitor.recipient", $input["alerts"]["monitoring"]["recipient"] ?? 0)
->set("monitor.all", $input["alerts"]["monitoring"]["all"] ?? 0)
->save();
}
}
Expand All @@ -44,12 +47,14 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface

"enabled" => [
'#type' => 'checkbox',
'#title' => t('Postmark Email Service Enabled (Enables/disables ALL emails via PostMark'),
'#title' => t('Postmark Email Service Enabled'),
'#description' => t('When selected, emails will be sent via Postmark. When unselected all emails are added to the queue.'),
'#default_value' => $config->get('enabled'),
],
"q_enabled" => [
'#type' => 'checkbox',
'#title' => t('Postmark Queue Enabled (ALL emails which PostMark cannot send will be queued'),
'#title' => t('Postmark-fail Queue Enabled'),
'#description' => t('When selected, emails that Postmark cannot process will be queued and there will be attempts to be resend. When unselected failed emails are discarded.'),
'#default_value' => $config->get('q_enabled'),
],

Expand All @@ -59,11 +64,10 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface
'#description' => 'Configure outbound emails for issues which arise with PostMark integration.',
'#open' => FALSE,


"conditions" => [
'#type' => 'fieldset',
'#title' => 'Service Abuse',
'#markup' => 'Emails will be sent to the recipient below when these unexpected events occur:',
'#markup' => 'Emails will be sent to the recipient below when these potential abuse events occur:',
'#collapsible' => FALSE,

"token" => [
Expand All @@ -79,12 +83,31 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface
"recipient" => [
'#type' => 'textfield',
"#title" => "Email recipient",
"#description" => "The email (or email group) to receive service abuse emails.",
"#description" => "The email (or email group) to receive hardbounce alerts.",
"#attributes" => ["placeholder" => "[email protected]"],
"#default_value" => $config->get('alerts.recipient') ?? "",
],
],

"monitoring" => [
'#type' => 'fieldset',
'#title' => 'Service Monitoring',
'#markup' => 'Emails will be sent to the recipient below when these unexpected service error events occur:',
'#collapsible' => FALSE,
"all" => [
'#type' => 'checkbox',
'#title' => t('All non-abuse failures when connecting to Postmark API.'),
'#default_value' => $config->get('monitor.all') ?? 0,
],
"recipient" => [
'#type' => 'textfield',
"#title" => "Email recipient",
"#description" => "The email (or email group) to receive service error emails.",
"#attributes" => ["placeholder" => "[email protected]"],
"#default_value" => $config->get('monitor.recipient') ?? "",
],
],

"hb" => [
'#type' => 'fieldset',
'#title' => 'Hard Bounce / Recipient Supression',
Expand Down Expand Up @@ -121,7 +144,8 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface
],
"q_enabled" => [
'#type' => 'checkbox',
'#title' => t('Contact Form queue enabled'),
'#title' => t('Contact Form queue processing enabled'),
'#description' => t('When selected, emails which initially fail to send and are queued will be processed on each cron run.'),
'#default_value' => $config->get('contactform.q_enabled'),
],
],
Expand All @@ -139,7 +163,8 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface
],
"q_enabled" => [
'#type' => 'checkbox',
'#title' => t('Contact Form queue enabled'),
'#title' => t('Registry queue processing enabled'),
'#description' => t('When selected, emails which initially fail to send and are queued will be processed on each cron run.'),
'#default_value' => $config->get('registry.q_enabled'),
],
],
Expand All @@ -157,7 +182,8 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface
],
"q_enabled" => [
'#type' => 'checkbox',
'#title' => t('Commissions queue enabled'),
'#title' => t('Commissions queue processing enabled'),
'#description' => t('When selected, emails which initially fail to send and are queued will be processed on each cron run.'),
'#default_value' => $config->get('commissions.q_enabled'),
],
],
Expand All @@ -175,7 +201,8 @@ function bos_email_form_bos_core_admin_settings_alter(&$form, FormStateInterface
],
"q_enabled" => [
'#type' => 'checkbox',
'#title' => t('metrolist queue enabled'),
'#title' => t('metrolist queue processing enabled'),
'#description' => t('When selected, emails which initially fail to send and are queued will be processed on each cron run.'),
'#default_value' => $config->get('metrolist.q_enabled'),
],
],
Expand Down Expand Up @@ -216,6 +243,14 @@ function bos_email_mail($key, &$message, $params) {
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
break;

case "monitor.all":
$message["from"] = "boston.gov<[email protected]>";
$message["subject"] = "Postmark mailer issue on Boston.gov.";
$custom_msg = "There was an issue sending the following via Postmark.";
$message["body"] = _make_HTML_body($params, $custom_msg, Drupal::request()->headers->all());
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
break;

case "TestDrupalmail.plain":
default:
$message["from"] = $params["From"];
Expand Down Expand Up @@ -250,17 +285,23 @@ function _make_HTML_body($params, $custom_msg, $headers) {
// Now build the html message.
$custom_table = "<p><table>";
if (!empty($params["Subject"])) {
$custom_table .= "<tr><td>subject</td><td>{$params["Subject"]}</td></tr>";
$custom_table .= "<tr><td>Subject: </td><td>{$params["Subject"]}</td></tr>";
}
if (!empty($params["To"])) {
$custom_table .= "<tr><td>recipient</td><td>{$params["To"]}</td></tr>";
$custom_table .= "<tr><td>Recipient: </td><td>{$params["To"]}</td></tr>";
}
if (!empty($params["server"])) {
$custom_table .= "<tr><td>service</td><td>{$params["server"]}</td></tr>";
$custom_table .= "<tr><td>Service: </td><td>{$params["server"]}</td></tr>";
}
$custom_table .= "</table></p>";
$custom_table .= "<table>";
$custom_table .= "<tr><td>Endpoint called</td><td>" . \Drupal::request()->getRequestUri() ."</td></tr>";
if (!empty($params["ErrorCode"])) {
$custom_table .= "<tr><td>HTTPCode: </td><td>{$params["ErrorCode"]}</td></tr>";
}
if (!empty($params["Message"])) {
$custom_table .= "<tr><td>Reported Error: </td><td>{$params["Message"]}</td></tr>";
}
$custom_table .= "<tr><td>Sender IP</td><td>" . \Drupal::request()->getClientIp() . "</td></tr>";
$custom_table .= "<tr><td>Headers</td><td>";
foreach($headers as $key => $values) {
Expand All @@ -273,7 +314,7 @@ function _make_HTML_body($params, $custom_msg, $headers) {
$custom_table .= "</td></tr>";
if (\Drupal::request()->request && \Drupal::request()->request->has("email")) {
$custom_table .= "<tr><td>Original Payload</td><td>";
foreach(\Drupal::request()->request->get("email", []) as $key => $value) {
foreach(\Drupal::request()->get("email", []) as $key => $value) {
$custom_table .= "- <b>{$key}:</b> {$value}<br>";
}
$custom_table .= "</td></tr>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,12 @@ private static function getDomain(): string {
return implode(".", array_reverse($domain));
}

public function removeEmpty() {
foreach($this->emailFields as $key => $value) {
if ($value == "" && !in_array($key, $this->requiredFields)) {
$this->delField($key);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ private function formatEmail(array &$emailFields) {

}

// Remove empty fields here.
// $emailFields["postmark_data"]->removeEmpty();

if ($this->debug) {
try {
$json = json_encode(@$emailFields["postmark_data"]->data());
Expand Down Expand Up @@ -722,4 +725,12 @@ public function callback(string $service, string $stream) {

}

protected function removeEmptyFields(CobEmail &$data): void {
foreach($data as $key => $value) {
if ($value == "") {
$data->delField($key);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ public function sendEmail($item) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_code != 200) {
throw new \Exception("Postmark Error {$http_code}<br>PAYLOAD: {$item_json}<br>RESPONSE:{$response_json}");
throw new \Exception("Postmark Error {$http_code}<br>HEADERS: {$headers}<br>PAYLOAD: {$item_json}<br>RESPONSE:{$response_json}");
}

if (strtolower($response["ErrorCode"]) != "0") {
throw new \Exception("Postmark Error Code: {$response['ErrorCode']}<br>PAYLOAD: {$item_json}<br>RESPONSE:{$response_json}");
throw new \Exception("Postmark Error Code: {$response['ErrorCode']}<br>HEADERS: {$headers}<br>PAYLOAD: {$item_json}<br>RESPONSE:{$response_json}");
}

return TRUE;
Expand All @@ -136,6 +136,7 @@ public static function alertHandler($item, $response, $http_code, $config, $erro
if (empty($config)) {
$config = \Drupal::configFactory()->get("bos_email.settings");
}

$recipient = $config->get("alerts.recipient") ?? FALSE;
if ($recipient) {

Expand Down Expand Up @@ -195,6 +196,20 @@ public static function alertHandler($item, $response, $http_code, $config, $erro
\Drupal::logger("bos_email:PostmarkOps")->warning(t("Email sending from Drupal has failed."));
}
}
}

// If no other issues, but the email failed to send.
if (!isset($mailManager)
&& $config->get("monitor.all")) {
$recipient = $config->get("monitor.recipient") ?? FALSE;
if ($recipient) {
$mailManager = \Drupal::service('plugin.manager.mail');
if (!$mailManager->mail("bos_email", 'monitor.all', $recipient, "en", array_merge($item, $response), NULL, TRUE)) {
\Drupal::logger("bos_email:PostmarkOps")
->warning(t("Email sending from Drupal has failed."));
}
}

}

// Do dome logging if this is a local dev environment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static function formatOutboundEmail(array &$emailFields): void {
$cobdata->delField("TemplateID");

}

/**
* @inheritDoc
*/
Expand Down