Search code examples
ruby-on-railsrubyemailmailer

Rails mailer logo. url_for vs. attachment


I'm implementing a feature with mailer in Rails 4. I managed to display the logo by using inline attachment, on the email that the user receives. It's working well but my supervisor asked me to try to find another way. This was his question:


Is it necessary to attach the logo? instead of using attachments, maybe url_for? Using url_for gives the absolute path of the image. This way, the image could be hosted in our website, while being used in the email.

--

I'm trying to find a answer but I can't find it. Also the only way that I can make it to work is by using inline attachment.


Solution

  • If you want to use url_for or xxx_url helpers in mails, you must do some configuration in the files under config/environments/, for example

    config.action_mailer.default_url_options = {host: 'www.example.org', protocol: 'https'}
    

    To use image_url or asset_url in mail templates, you also need to add

    config.action_mailer.asset_host = 'https://www.example.org'
    

    By the way, inline attachments are attachments, and they do increase the size of mails, but can be displayed without the recipient's permission.

    On the other hand, the images referenced by url_for or some sort are only URL's thus reduces the mails' size, but the MUA's have to send a separate request to download the image, and they can't do this without the permission of the recipients. If the recipient refuses to download, he will see an ugly broken image icon.