Skip to content

Commit dab8451

Browse files
committed
Queued emails: Handle non-submission saving webforms and cleanup
1 parent 82da267 commit dab8451

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.
66

77
## [Under udvikling]
88

9+
## [4.2.2] 2025-05-07
10+
11+
* OS2Forms kø emails
12+
* Håndterede formularer der ikke gemmer indsendelser.
13+
* Opdaterede fejlbeskeder.
14+
915
## [4.2.1] 2025-05-06
1016

1117
* Sikrede at OS2Forms attachment elementer bliver detekteret korrekt
@@ -633,7 +639,8 @@ og [OS2Forms 3.7.0](https://github.com/OS2Forms/os2forms/releases/tag/3.7.0)
633639

634640
* GO borgersager
635641

636-
[Under udvikling]: https://github.com/itk-dev/os2forms_selvbetjening/compare/4.2.1...HEAD
642+
[Under udvikling]: https://github.com/itk-dev/os2forms_selvbetjening/compare/4.2.2...HEAD
643+
[4.2.2]: https://github.com/itk-dev/os2forms_selvbetjening/compare/4.2.1...4.2.2
637644
[4.2.1]: https://github.com/itk-dev/os2forms_selvbetjening/compare/4.2.0...4.2.1
638645
[4.2.0]: https://github.com/itk-dev/os2forms_selvbetjening/compare/4.1.0...4.2.0
639646
[4.1.0]: https://github.com/itk-dev/os2forms_selvbetjening/compare/4.0.1...4.1.0

web/modules/custom/os2forms_queued_email/src/Plugin/AdvancedQueue/JobType/QueuedEmail.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ public function process(Job $job): JobResult {
7777
$payload = $job->getPayload();
7878
$message = json_decode($payload['message'], TRUE);
7979

80-
// Load the Webform submission entity by ID.
81-
$submission = WebformSubmission::load($payload['submissionId']);
82-
8380
// Gather filenames for os2forms attachments for deletion later.
8481
$os2formsAttachmentFilenames = [];
8582

@@ -88,10 +85,15 @@ public function process(Job $job): JobResult {
8885
// Handle OS2Forms attachments.
8986
if (isset($attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME])) {
9087
$os2formsAttachmentFilenames[] = $attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME];
88+
89+
if (FALSE === file_exists($attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME])) {
90+
throw new \Exception('OS2Forms attachment file not found: ' . $attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME]);
91+
}
92+
9193
$attachment[self::FILECONTENT] = file_get_contents($attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME]);
9294

9395
if (FALSE === $attachment[self::FILECONTENT]) {
94-
throw new \Exception('OS2Forms attachment file not found: ' . $attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME]);
96+
throw new \Exception('OS2Forms attachment file cannot be read: ' . $attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME]);
9597
}
9698

9799
unset($attachment[self::OS2FORMS_QUEUED_EMAIL_CONFIG_NAME]);
@@ -110,42 +112,56 @@ public function process(Job $job): JobResult {
110112

111113
}
112114

113-
$this->mailManager->createInstance('SMTPMailSystem')->mail($message);
115+
$result = $this->mailManager->createInstance('SMTPMailSystem')->mail($message);
114116

115-
$logger_context = [
116-
'handler_id' => 'os2forms_queued_email',
117-
'channel' => 'webform_submission',
118-
'webform_submission' => $submission,
119-
'operation' => 'email sent',
120-
];
117+
// Logging of failed mail is handled in catch.
118+
if (!$result) {
119+
throw new \Exception('Failed sending email');
120+
}
121121

122-
$this->submissionLogger->notice($this->t('The submission #@serial was successfully delivered', ['@serial' => $submission->serial()]), $logger_context);
122+
// Load the Webform submission entity by ID.
123+
// Be aware that some webforms may be configured to NOT save submissions,
124+
// submission may therefore be null.
125+
$submission = WebformSubmission::load($payload['submissionId']);
126+
127+
if ($submission) {
128+
$logger_context = [
129+
'handler_id' => 'os2forms_queued_email',
130+
'channel' => 'webform_submission',
131+
'webform_submission' => $submission,
132+
'operation' => 'email sent',
133+
];
134+
135+
$this->submissionLogger->notice($this->t('The submission #@serial was successfully delivered', ['@serial' => $submission->serial()]), $logger_context);
136+
}
123137

124138
// Remove OS2Forms attachments.
125139
foreach ($os2formsAttachmentFilenames as $os2formsAttachmentFilename) {
126140
unlink($os2formsAttachmentFilename);
127141
}
128142

129-
$msg = sprintf('Email, %s, sent to %s. Webform id: %s.', $message['subject'], $message['to'], $submission->getWebform()->id());
143+
$msg = sprintf('Email, %s, sent to %s. Webform id: %s.', $payload['subject'] ?? NULL, $payload['to'] ?? NULL, $payload['webformId'] ?? NULL);
130144
$this->auditLogger->info('Email', $msg);
131145

132146
return JobResult::success();
133147
}
134148
catch (\Exception $e) {
135149

136-
$submission = $payload['submissionId'] ? WebformSubmission::load($payload['submissionId']) : NULL;
137-
138-
$logger_context = [
139-
'handler_id' => 'os2forms_queued_email',
140-
'channel' => 'webform_submission',
141-
'webform_submission' => $submission,
142-
'operation' => 'email failed',
143-
];
150+
$submission = WebformSubmission::load($payload['submissionId']);
144151

145-
$this->submissionLogger->error($this->t('The submission #@serial failed (@message)', [
146-
'@serial' => $submission?->serial(),
147-
'@message' => $e->getMessage(),
148-
]), $logger_context);
152+
if ($submission) {
153+
$logger_context = [
154+
'handler_id' => 'os2forms_queued_email',
155+
'channel' => 'webform_submission',
156+
'webform_submission' => $submission,
157+
'operation' => 'email failed',
158+
];
159+
160+
$this->submissionLogger->error($this->t('The submission #@serial failed (@message)', [
161+
'@serial' => $submission->serial(),
162+
'@message' => $e->getMessage(),
163+
]), $logger_context);
164+
}
149165

150166
return JobResult::failure($e->getMessage());
151167
}

web/modules/custom/os2forms_queued_email/src/Plugin/Mail/QueuedSmtpPhpMail.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public function mail(array $message) {
115115
'key' => $message['key'],
116116
'to' => $message['to'],
117117
'subject' => $message['subject'],
118+
// Note that submissions may not exist later when job is processed.
118119
'submissionId' => $submission->id(),
120+
'webformId' => $submission->getWebform()->id(),
119121
'message' => json_encode($message),
120122
]);
121123

0 commit comments

Comments
 (0)