Skip to content

Commit

Permalink
Remove mailers (#2850)
Browse files Browse the repository at this point in the history
This removes various references to `ActionMailer` and the mailer classes
since they are no longer being used as we're sending emails using the
GOV.UK Notify API directly now (#2841).

We can't remove the `ApplicationMailer` and stop requiring
`action_mailer/railtie` until
heartcombo/devise#5731 has been released.
  • Loading branch information
thomasleese authored Feb 12, 2025
2 parents 1c4fc62 + b159178 commit 7548f94
Show file tree
Hide file tree
Showing 21 changed files with 15 additions and 954 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ gem "govuk_design_system_formbuilder"
gem "govuk_markdown"
gem "jsonb_accessor"
gem "jwt"
gem "mail-notify"
gem "notifications-ruby-client"
gem "okcomputer"
gem "omniauth_openid_connect"
Expand Down
8 changes: 0 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,6 @@ GEM
net-imap
net-pop
net-smtp
mail-notify (2.0.0)
actionmailer (>= 5.2.8.1)
actionpack (>= 5.2.8.1)
actionview (>= 5.2.8.1)
activesupport (>= 5.2.8.1)
notifications-ruby-client (~> 6.0)
rack (>= 2.1.4.1)
marcel (1.0.4)
matrix (0.4.2)
mechanize (2.14.0)
Expand Down Expand Up @@ -747,7 +740,6 @@ DEPENDENCIES
jsbundling-rails
jsonb_accessor
jwt
mail-notify
mechanize
notifications-ruby-client
okcomputer
Expand Down
10 changes: 6 additions & 4 deletions app/jobs/notify_delivery_job.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true

require "notifications/client"

class NotifyDeliveryJob < ApplicationJob
queue_as { Rails.configuration.action_mailer.deliver_later_queue_name }
queue_as :mailer

retry_on Notifications::Client::ServerError, wait: :polynomially_longer

def self.client
@client ||=
Notifications::Client.new(
Rails.configuration.action_mailer.notify_settings[:api_key]
Settings.govuk_notify["#{Settings.govuk_notify.mode}_key"]
)
end

Expand All @@ -17,11 +19,11 @@ def self.deliveries
end

def self.send_via_notify?
Rails.configuration.action_mailer.delivery_method == :notify
Settings.govuk_notify&.enabled
end

def self.send_via_test?
Rails.configuration.action_mailer.delivery_method == :test
Rails.env.test?
end

class UnknownTemplate < StandardError
Expand Down
111 changes: 1 addition & 110 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,113 +1,4 @@
# frozen_string_literal: true

class ApplicationMailer < Mail::Notify::Mailer
before_action :attach_data_for_notify_log_entry
after_deliver :log_delivery

private

def app_template_mail(template_name)
template_mail(
GOVUK_NOTIFY_EMAIL_TEMPLATES.fetch(template_name),
to:,
reply_to_id:,
personalisation:
)
end

def consent
@consent ||= params[:consent]
end

def consent_form
@consent_form ||= params[:consent_form]
end

def vaccination_record
@vaccination_record ||= params[:vaccination_record]
end

def patient_session
@patient_session ||= params[:patient_session]
end

def patient
@patient ||= params[:patient]
end

def parent
@parent ||= params[:parent]
end

def programme
@programme ||= params[:programme]
end

def session
@session ||= params[:session]
end

def sent_by
@sent_by ||= params[:sent_by]
end

def to
consent_form&.parent_email || consent&.parent&.email || parent.email
end

def reply_to_id
team =
session&.team || patient_session&.team || consent_form&.team ||
vaccination_record&.team

return team.reply_to_id if team&.reply_to_id

organisation =
session&.organisation || patient_session&.organisation ||
consent_form&.organisation || consent&.organisation ||
vaccination_record&.organisation

organisation.reply_to_id
end

def personalisation
GovukNotifyPersonalisation.call(
consent:,
consent_form:,
patient:,
patient_session:,
programme:,
session:,
vaccination_record:
)
end

def attach_data_for_notify_log_entry
# https://stackoverflow.com/a/28004917

patient_id = (patient || consent&.patient || patient_session&.patient)&.id
consent_form_id = consent_form&.id

message.instance_variable_set(:@consent_form_id, consent_form_id)
message.instance_variable_set(:@patient_id, patient_id)
message.instance_variable_set(:@sent_by_user_id, sent_by&.id)

message.class.send(:attr_reader, :consent_form_id)
message.class.send(:attr_reader, :patient_id)
message.class.send(:attr_reader, :sent_by_user_id)
end

def log_delivery
mail.to.map do |recipient|
NotifyLogEntry.create!(
consent_form_id: mail.consent_form_id,
patient_id: mail.patient_id,
recipient:,
recipient_deterministic: recipient,
sent_by_user_id: mail.sent_by_user_id,
template_id: mail.template_id,
type: :email
)
end
end
class ApplicationMailer < ActionMailer::Base
end
39 changes: 0 additions & 39 deletions app/mailers/consent_mailer.rb

This file was deleted.

15 changes: 0 additions & 15 deletions app/mailers/session_mailer.rb

This file was deleted.

15 changes: 0 additions & 15 deletions app/mailers/triage_mailer.rb

This file was deleted.

15 changes: 0 additions & 15 deletions app/mailers/vaccination_mailer.rb

This file was deleted.

2 changes: 0 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class Application < Rails::Application
config.active_job.queue_adapter = :good_job
config.good_job.execution_mode = :async

config.action_mailer.deliver_later_queue_name = :mailer

config.view_component.default_preview_layout = "component_preview"
config.view_component.preview_controller = "ComponentPreviewsController"
config.view_component.preview_paths << Rails.root.join(
Expand Down
19 changes: 0 additions & 19 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,6 @@
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

# Make template changes take effect immediately.
config.action_mailer.perform_caching = false

# Set localhost to be used by links generated in mailer templates.
config.action_mailer.default_url_options = { host: "localhost", port: 4000 }

if Settings.govuk_notify&.enabled
config.action_mailer.delivery_method = :notify
config.action_mailer.notify_settings = {
api_key: Settings.govuk_notify["#{Settings.govuk_notify.mode}_key"]
}
else
config.action_mailer.default_options = { from: "[email protected]" }
config.action_mailer.delivery_method = :file
end

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

Expand Down
21 changes: 0 additions & 21 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,6 @@
# Replace the default in-process and non-durable queuing backend for Active Job.
# config.active_job.queue_adapter = :resque

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false

# Set host to be used by links generated in mailer templates.
config.action_mailer.default_url_options = {
host:
if Settings.is_review
"#{ENV["HEROKU_APP_NAME"]}.herokuapp.com"
else
Settings.host
end,
protocol: "https"
}

# Configure GOV.UK Notify.
config.action_mailer.delivery_method = :notify
config.action_mailer.notify_settings = {
api_key: Settings.govuk_notify.live_key
}

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true
Expand Down
4 changes: 0 additions & 4 deletions config/environments/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@
description: "Track the status of DPS exports"
}
)

config.action_mailer.notify_settings = {
api_key: Settings.govuk_notify["#{Settings.govuk_notify.mode}_key"]
}
end
9 changes: 0 additions & 9 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@
# Store uploaded files on the local file system in a temporary directory.
config.active_storage.service = :test

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test

# Set host to be used by links generated in mailer templates.
config.action_mailer.default_url_options = { host: "localhost:4000" }
config.action_mailer.default_options = { from: "[email protected]" }

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr

Expand Down
2 changes: 2 additions & 0 deletions config/settings/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ cis2:

govuk_notify:
callback_bearer_token: test-bearer-token
enabled: false
mode: test

mesh:
base_url: https://localhost:8700
Expand Down
6 changes: 3 additions & 3 deletions spec/jobs/email_delivery_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe EmailDeliveryJob do
before(:all) do
Rails.configuration.action_mailer.delivery_method = :notify
Rails.configuration.action_mailer.notify_settings = { api_key: "abc" }
Settings.govuk_notify.enabled = true
Settings.govuk_notify.test_key = "abc"
end

after(:all) { Rails.configuration.action_mailer.delivery_method = :test }
after(:all) { Settings.govuk_notify.enabled = false }

let(:response) do
instance_double(
Expand Down
6 changes: 3 additions & 3 deletions spec/jobs/sms_delivery_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe SMSDeliveryJob do
before(:all) do
Rails.configuration.action_mailer.delivery_method = :notify
Rails.configuration.action_mailer.notify_settings = { api_key: "abc" }
Settings.govuk_notify.enabled = true
Settings.govuk_notify.test_key = "abc"
end

after(:all) { Rails.configuration.action_mailer.delivery_method = :test }
after(:all) { Settings.govuk_notify.enabled = false }

let(:response) do
instance_double(
Expand Down
Loading

0 comments on commit 7548f94

Please sign in to comment.