In Germany and many other countries as a company you are required to add a legal footer to your emails.
Of course you could just add the legal snippet into the footer of each of your emails.
Or you can create a partial and include it.
But theres a better way.
So let's start with a test.
require 'spec_helper' class FakeMailer < ActionMailer::Base def any_email mail(from: 'email@example.com', to: 'firstname.lastname@example.org', subject: "Hello", body: 'A body') end end describe 'Email Signature' do it 'appends a signature to every email' do signature_divider = "-- n" email = FakeMailer.any_email.deliver email.body.to_s.should match(signature_divider) end end
This fails of course. I did match on the signature divider which contains of 2 dashes, a space and a newline. This is a old convention from the Newsnet and can be automatically parsed by many email clients.
An elegant way of solving this is to implement an mail interceptor.
An interceptor catches mails that are send and can modify their content.
class EmailSignature def self.delivering_email(message) message.body = String(message.body) + footer end def self.footer "-- n" + "Insert your footer here" end end Mail.register_interceptor(EmailSignature)
The interceptor in our case appends the footer to the body of the message. It needs to be registered with
This is a very simple way to add a footer to all your text emails.
It does not take into account HTML emails and email's with multiple parts. (Though it's no black magic to modify the code.)