Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Action Mailer: images in emails

I'm trying to paste images into email. The problem is emails comming without images inside

development.rb

  config.action_mailer.default_url_options = {     :host => 'localhost:3000',     :only_path => false   }   config.action_mailer.raise_delivery_errors = false   config.action_mailer.asset_host = 'http://localhost:3000' 

view file:

<div class="image">   <%= image_tag image_path('email-logo.png') %> </div> 

Where did I make a mistake? Please ask if you need more information.

like image 657
Pavel Babin Avatar asked Mar 04 '14 20:03

Pavel Babin


2 Answers

try

<div class="image">   <%= image_tag('email-logo.png') %> </div> 

Make sure you set config.action_controller.asset_host and config.action_mailer.asset_host

like image 55
a14m Avatar answered Sep 19 '22 13:09

a14m


You're sending emails from localhost:3000, which isn't publicly available (and limited to your machine only).

You have to expose your local environment, so that images can be downloaded in your mail client.

Use service like ngrok to expose your local domain.

Once done, be sure to replace config.action_mailer.asset_host = 'http://localhost:3000'

with the ngrok URL (something like config.action_mailer.asset_host = 'http://<xxx>.ngrok.com')

Also, in your view file, you'll have to ensure that you specify the absolute url for the image (and not just the relative path). You can read more on that here: How do I get an absolute URL for an asset in Rails 3.1?

like image 30
Utsav Kesharwani Avatar answered Sep 18 '22 13:09

Utsav Kesharwani