All threads / How to add logo image to mailer

Ask A Question


You’re not receiving notifications from this thread.

How to add logo image to mailer

Francisco Quinones asked in Rails

Hi how can we add image from my assets to the email like my logo when sending email to the users.

You should just be able to use the regular old <%= image_tag("logo.png") %> helpers just like you would use in your views. You may need to set your asset_host so that it includes a full URL for the images in the emails since they aren't displayed in the browser under your domain.

# I believe these should do the trick:
config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host

@chris I use that approach but that get me another error.

 Font from origin has been blocked from loading by Cross-Origin Resource Sharing policy 

The email show the logo but now fonts are missing.
has I'm using subdomain

Oh fun, CORS is not a super fun thing to mess with. I'm not sure if you can do that with email, but you have to allow origins with CORS to determine where your fonts can be loaded from:

O yes, I start reading about CORS but looks to shallow for me at the moment. As the problem with the fonts is on my site and not in the email. If I disable the image on email and hide the config for that all goes back to normal. So yes need to do a workaround the email images and the font cross site.

Ah cool. I've used in the past to do that, but then I just started paying $50/yr for Typekit and no longer have to deal with that for almost any fonts I could want to use.

Hi Chris,

Why is it necessary to set both action_controllerand action_mailer asset_host?

config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host

Is it not possible to just set the config.action_mailer.asset_host = 'http://localhost:3000' to have the full url in the mail?
If we set the action_controller, all images_tag across the application, in normal views, will also use the the full usrl as src instead os just the relative path.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 53,130+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.