Skip to content

Handle error sending confirmation email#1580

Merged
stephencdaly merged 3 commits intomainfrom
handle-error-sending-confirmation-email
Aug 18, 2025
Merged

Handle error sending confirmation email#1580
stephencdaly merged 3 commits intomainfrom
handle-error-sending-confirmation-email

Conversation

@stephencdaly
Copy link
Contributor

@stephencdaly stephencdaly commented Aug 14, 2025

What problem does this pull request solve?

Trello card: https://trello.com/c/wujuVVVe

Previously, when ActionMailer failed to parse the To email address for the confirmation email, an error would not be raised until the GovukNotifyRails::Delivery code attempts to call message.to.map which raises an error due to message.to returning a String rather than a Map. It returns a String rather than a map only when there is an error parsing the "To" field.

Sentry issue: https://govuk-forms.sentry.io/issues/6656667020

This PR makes it so that we check whether there was an error constructing the confirmation email due to the recipient's email address before we try to submit the form. If there is, this results in a user being shown the validation error "Enter an email address in the correct format, like name@example.com" on the check your answers page.

An error is also sent to Sentry with the error message from ActionMailer, but with the email address redacted so we don't store PII.

Testing

I don't know how to make an email that's accepted by our email validation, but not by ActionMailer so I tested locally by manipulating the email in the code after validation to add a new line character and checking the validation error message was shown and an error was sent to Sentry.

Message sent to Sentry:
Screenshot 2025-08-15 at 09 51 52

Things to consider when reviewing

  • Ensure that you consider the wider context.
  • Does it work when run on your machine?
  • Is it clear what the code is doing?
  • Do the commit messages explain why the changes were made?
  • Are there all the unit tests needed?
  • Do the end to end tests need updating before these changes will pass?
  • Has all relevant documentation been updated?

@stephencdaly stephencdaly force-pushed the handle-error-sending-confirmation-email branch 3 times, most recently from ec35b35 to 7fb2e4c Compare August 15, 2025 09:35
@stephencdaly stephencdaly marked this pull request as ready for review August 15, 2025 09:43
@stephencdaly stephencdaly force-pushed the handle-error-sending-confirmation-email branch 3 times, most recently from c492301 to 9addd31 Compare August 15, 2025 13:27
@stephencdaly stephencdaly force-pushed the handle-error-sending-confirmation-email branch from 9addd31 to d099c61 Compare August 18, 2025 08:51
Copy link
Contributor

@theseanything theseanything left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Ultimately we don't want to be logging these validation errors (as they should just be displayed to the user, but useful to know what the problem is atm).

Some optional changes suggested.

We want to send the error message we get when ActionMailer is not able
to parse the "To" email address for a Mail message to Sentry so we can
try to figure out why certain invalid email addresses are getting
through our validation.

However, we do not want to sent PII to Sentry. This helper method
attempts to obfuscate email addresses, retaining any special
characters which will hopefully give us enough information while
ensuring the email address is not identifiable. We replace the @ with
(at) so that Sentry doesn't match an email address in the
action_mailer_error we send and strip it out completely.
Previously, when ActionMailer failed to parse the To email address for
the confirmation email, an error would not be raised until the
GovukNotifyRails::Delivery code attempts to call `message.to.map`
which raises an error due to `message.to` returning a String rather
than a Map.

It returns a String rather than a map only when there is an error
parsing the "To" field.

This commit makes it so that we look at the `mail.messages.errors` to
establish whether there is an error with the "To" field. If there is,
we send an error to Sentry and raise a ConfirmationEmailToAddressError
to be handled by the CheckYourAnswersController in a subsequent
commit.
Handle the ConfirmationEmailToAddressError raised by
FormSubmissionService in CheckYourAnswersController#submit_answers by
re-rendering the check your answers page with a validation error
message stating that the email address is invalid.
@stephencdaly stephencdaly force-pushed the handle-error-sending-confirmation-email branch from d099c61 to afc8245 Compare August 18, 2025 14:57
@sonarqubecloud
Copy link

@github-actions
Copy link
Contributor

🎉 A review copy of this PR has been deployed! It is made of up two components

  1. A review copy of forms-runner
  2. A production copy of forms-admin

Important

Not all of the functionality of forms-runner is present in review apps.
Functionality such as sending emails, file upload, and S3 submission types are
deliberately disabled for the sake of simplifying review apps.

You should use the full dev environment to test the functionality which is disabled here.

It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready
after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account
to debug, or otherwise ask an infrastructure person.

For the sign in details and more information, see the review apps wiki page.

@stephencdaly stephencdaly merged commit 014541a into main Aug 18, 2025
11 checks passed
@stephencdaly stephencdaly deleted the handle-error-sending-confirmation-email branch August 18, 2025 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants