Ask A Question

Notifications

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.

Reply

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
Reply

@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

Reply

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: http://stackoverflow.com/questions/27726802/css-font-being-blocked-from-cross-origin-resource-sharing-policy

Reply

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.

Reply

Ah cool. I've used https://github.com/cyu/rack-cors 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.

Reply

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.

Reply

This approach is very interesting and I would like to understand and use it in a larger scale.
Also, eagerly waiting for the Rolify video as promised :)

So, the thing I don't fully understand here is, how to approach it when I need to add dynamic permissions for other models, too. Let's say PostCategory as video talks about Posts (and there will naturally be many many more).

How would I then scale configuration on the roles?
Adding flat attributes, where in this case i would say post_category_read, post_category_create in addition to the ones for Post, or maybe a nested JSON, where i would have post: { read, create, etc. }, post_category : {read, create, etc...} - if nesting is even possible, not sure right now.

Or maybe creating a separate attributes on Role altogether. Like post_permissions, post_category_permissions, etc...

Maybe I got it all wrong in the first place. I am a bit scared going this way and realizing it doesn't scale in a good way. Thanks for any tips. A review of the best and freshest gaming devices, of course. After all, insiders of all countries write articles here and we will be the first to learn about the world's new products.

Reply
Join the discussion
Create an account Log in

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

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

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