From 8b80c486792e04b5a21fcf94f4cd25b1dc9ca540 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 17 Jul 2019 12:41:37 +0000 Subject: [PATCH] emails: use the notification template for more messages This allow to display the procedure logo and contact footer at the bottom of these emails. --- app/mailers/application_mailer.rb | 18 ++++++++++++++++++ app/mailers/dossier_mailer.rb | 10 +++++++++- app/mailers/notification_mailer.rb | 13 +------------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index a2612765e..88f1a3353 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -2,4 +2,22 @@ class ApplicationMailer < ActionMailer::Base helper :application # gives access to all helpers defined within `application_helper`. default from: "demarches-simplifiees.fr <#{CONTACT_EMAIL}>" layout 'mailer' + + # Attach the procedure logo to the email (if any). + # Returns the attachment url. + def attach_logo(procedure) + return nil if !procedure.logo? + + begin + logo_filename = procedure.logo.filename + attachments.inline[logo_filename] = procedure.logo.read + attachments[logo_filename].url + + rescue StandardError => e + # A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ? + Raven.extra_context(procedure_id: procedure.id) + Raven.capture_exception(e) + nil + end + end end diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index 53b44f817..8483fde87 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -6,13 +6,21 @@ class DossierMailer < ApplicationMailer def notify_new_draft(dossier) @dossier = dossier + @service = dossier.procedure.service + @logo_url = attach_logo(dossier.procedure) + subject = "Retrouvez votre brouillon pour la démarche « #{dossier.procedure.libelle} »" - mail(to: dossier.user.email, subject: subject) + mail(to: dossier.user.email, subject: subject) do |format| + format.html { render layout: 'mailers/notification' } + end end def notify_new_answer(dossier) @dossier = dossier + @service = dossier.procedure.service + @logo_url = attach_logo(dossier.procedure) + subject = "Nouveau message pour votre dossier nº #{dossier.id} (#{dossier.procedure.libelle})" mail(to: dossier.user.email, subject: subject) do |format| diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 2822d405a..87379f6df 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -38,20 +38,9 @@ class NotificationMailer < ApplicationMailer create_commentaire_for_notification(dossier, subject, body) - if dossier.procedure.logo? - begin - logo_filename = dossier.procedure.logo.filename - attachments.inline[logo_filename] = dossier.procedure.logo.read - @logo_url = attachments[logo_filename].url - rescue StandardError => e - # A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ? - Raven.extra_context(procedure_id: dossier.procedure.id) - Raven.capture_exception(e) - end - end - @dossier = dossier @service = dossier.procedure.service + @logo_url = attach_logo(dossier.procedure) mail(subject: subject, to: email) do |format| # rubocop:disable Rails/OutputSafety