Skip to content

Commit f23c677

Browse files
committed
feat: remove template after sending bulk emails
Refs: RW-90
1 parent 7a30acb commit f23c677

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

html/modules/custom/reliefweb_subscriptions/src/ReliefwebSubscriptionsMailer.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ protected function sendViaAwsSesApiBulk(array $subscription, $subject, $html, ar
721721
);
722722
}
723723
catch (\Exception $exception) {
724-
$this->logger->error('Error create template for {name} subscription: {error}', [
724+
$this->logger->error('Unable to create template for {name} subscription: {error}', [
725725
'name' => $subscription_name,
726726
'error' => $exception->getMessage(),
727727
]);
@@ -799,6 +799,17 @@ protected function sendViaAwsSesApiBulk(array $subscription, $subject, $html, ar
799799
}
800800
}
801801

802+
// Delete the template.
803+
try {
804+
$this->awsSesClient->deleteTemplate($template_name);
805+
}
806+
catch (\Exception $exception) {
807+
$this->logger->error('Unable to delete template for {name} subscription: {error}', [
808+
'name' => $subscription_name,
809+
'error' => $exception->getMessage(),
810+
]);
811+
}
812+
802813
return TRUE;
803814
}
804815

html/modules/custom/reliefweb_subscriptions/src/Services/AwsSesClient.php

+28-15
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,7 @@ public function getAwsSesClient() {
9898
*/
9999
public function createTemplate($name, $subject, $html, $text) {
100100
// Delete the template if it already exists.
101-
try {
102-
$this->getAwsSesClient()->deleteEmailTemplate([
103-
'TemplateName' => $name,
104-
]);
105-
}
106-
catch (AwsException $exception) {
107-
if ($exception->getStatusCode() != 404) {
108-
$this->logger->error('AWS SES - Unable to delete template @name: @error', [
109-
'@name' => $name,
110-
'@error' => $exception->getMessage(),
111-
]);
112-
// @todo extract the error message.
113-
throw $exception;
114-
}
115-
}
101+
$this->deleteTemplate($name);
116102

117103
// Create the template.
118104
try {
@@ -135,6 +121,33 @@ public function createTemplate($name, $subject, $html, $text) {
135121
}
136122
}
137123

124+
/**
125+
* Delete an email template.
126+
*
127+
* @param string $name
128+
* Template name.
129+
*
130+
* @throws \Exception
131+
* Exception if the request to AWS SES failed.
132+
*/
133+
public function deleteTemplate($name) {
134+
try {
135+
$this->getAwsSesClient()->deleteEmailTemplate([
136+
'TemplateName' => $name,
137+
]);
138+
}
139+
catch (AwsException $exception) {
140+
if ($exception->getStatusCode() != 404) {
141+
$this->logger->error('AWS SES - Unable to delete template @name: @error', [
142+
'@name' => $name,
143+
'@error' => $exception->getMessage(),
144+
]);
145+
// @todo extract the error message.
146+
throw $exception;
147+
}
148+
}
149+
}
150+
138151
/**
139152
* Send a bulk message.
140153
*

0 commit comments

Comments
 (0)