demarches-normaliennes/app/mailers/application_mailer.rb

56 lines
1.7 KiB
Ruby
Raw Normal View History

2015-12-14 16:09:20 +01:00
class ApplicationMailer < ActionMailer::Base
helper :application # gives access to all helpers defined within `application_helper`.
default from: "#{APPLICATION_NAME} <#{CONTACT_EMAIL}>"
2015-12-14 16:09:20 +01:00
layout 'mailer'
before_action :add_dolist_header
# Dont retry to send a message if the server rejects the recipient address
2023-01-09 23:16:14 +01:00
rescue_from Net::SMTPSyntaxError do |_exception|
message.perform_deliveries = false
end
2023-01-09 23:16:14 +01:00
rescue_from Net::SMTPServerBusy do |exception|
if /unexpected recipients/.match?(exception.message)
message.perform_deliveries = false
2023-01-09 23:16:14 +01:00
else
log_smtp_error(exception)
end
end
2023-01-09 23:16:14 +01:00
rescue_from Net::SMTPError, with: :log_smtp_error
# Attach the procedure logo to the email (if any).
# Returns the attachment url.
def attach_logo(procedure)
2019-08-28 13:11:58 +02:00
if procedure.logo.attached?
logo_filename = procedure.logo.filename.to_s
attachments.inline[logo_filename] = procedure.logo.download
2019-08-28 14:27:41 +02:00
attachments[logo_filename].url
end
2019-08-28 13:11:58 +02:00
rescue StandardError => e
# A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ?
2021-01-28 14:49:22 +01:00
Sentry.capture_exception(e, extra: { procedure_id: procedure.id })
2019-08-28 13:11:58 +02:00
nil
end
# mandatory for dolist
# used for tracking in Dolist UI
# the delivery_method is yet unknown (:balancer)
# so we add the dolist header for everyone
def add_dolist_header
headers['X-Dolist-Message-Name'] = action_name
end
2023-01-09 23:16:14 +01:00
def log_smtp_error(exception)
if defined?(message) && message.to.present?
EmailEvent.create_from_message!(message, status: "dispatch_error")
end
Sentry.capture_exception(exception, extra: { to: message&.to, subject: message&.subject })
# TODO find a way to re attempt the job
end
2015-12-14 16:09:20 +01:00
end