diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 89f79c1db..8f8bb6c57 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -7,6 +7,7 @@ # class NotificationMailer < ApplicationMailer include ActionView::Helpers::SanitizeHelper + include ActionView::Helpers::TextHelper before_action :set_dossier before_action :set_services_publics_plus, only: :send_notification @@ -67,7 +68,7 @@ class NotificationMailer < ApplicationMailer mail_template = @dossier.procedure.mail_template_for(params[:state]) @email = @dossier.user_email_for(:notification) - @subject = mail_template.subject_for_dossier(@dossier) + @subject = truncate(mail_template.subject_for_dossier(@dossier), length: 100) @body = mail_template.body_for_dossier(@dossier) @actions = mail_template.actions_for_dossier(@dossier) @attachment = mail_template.attachment_for_dossier(@dossier) diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index e778c681d..a5035bbd8 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -10,7 +10,7 @@ module MailTemplateConcern end def subject_for_dossier(dossier) - replace_tags(subject, dossier) + replace_tags(subject, dossier).presence || replace_tags(self.class::DEFAULT_SUBJECT, dossier) end def body_for_dossier(dossier) diff --git a/config/locales/en.yml b/config/locales/en.yml index 17d80a709..dc8be05ec 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -395,6 +395,19 @@ en: zone: This procedure is run by champs: value: Value + default_mail_attributes: &default_mail_attributes + hints: + subject: The generated subject will be truncated if it exceeds 100 characters. + mails/closed_mail: + << : *default_mail_attributes + mails/initiated_mail: + << : *default_mail_attributes + mails/received_mail: + << : *default_mail_attributes + mails/refused_mail: + << : *default_mail_attributes + mails/without_continuation_mail: + << : *default_mail_attributes errors: messages: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index efd5e4d63..d4b864c28 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -392,6 +392,19 @@ fr: zone: La démarche est mise en œuvre par champs: value: Valeur du champ + default_mail_attributes: &default_mail_attributes + hints: + subject: "L’objet généré sera tronqué s’il dépasse 100 caractères." + mails/closed_mail: + << : *default_mail_attributes + mails/initiated_mail: + << : *default_mail_attributes + mails/received_mail: + << : *default_mail_attributes + mails/refused_mail: + << : *default_mail_attributes + mails/without_continuation_mail: + << : *default_mail_attributes errors: messages: diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 005db343c..25e77eb4d 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -101,4 +101,27 @@ RSpec.describe NotificationMailer, type: :mailer do end end end + + describe 'subject length' do + let(:procedure) { create(:simple_procedure, libelle: "My super long title " + ("xo " * 100)) } + let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) } + let(:email_template) { create(:closed_mail, subject:, body: 'Your dossier was accepted. Thanks.') } + + before do + dossier.procedure.closed_mail = email_template + end + + subject(:mail) { described_class.send_accepte_notification(dossier) } + + context "subject is too long" do + let(:subject) { 'Un long libellé --libellé démarche--' } + it { expect(mail.subject.length).to be <= 100 } + end + + context "subject should fallback to default" do + let(:subject) { "" } + it { expect(mail.subject).to match(/^Votre dossier .+ a été accepté \(My super long title/) } + it { expect(mail.subject.length).to be <= 100 } + end + end end