vicky
vicky

Reputation: 33

How to display image in email using action mailer in ruby on rails

I am making a application for sending mail to users, for that I am using action mailer in my application.
I want show a image in mail but I am not able to do this.Can anyone help in how to make it possible. I am using paper clip gem for storing images in my application. Field in database storing image name like
prescription_file_name => "foo_bar_thumbnail_image7.jpg" but the actually complete URL for that image is like

"/system/service_requests/prescriptions/000/000/237/medium/foo_bar_thumbnail_image7.jpg?1455861395".

Thanks in advance

Upvotes: 1

Views: 1873

Answers (2)

Vishal
Vishal

Reputation: 7361

You should try this in your mailer.rb

attachments['image.jpg'] = File.read(@photo.prescription_file.path(:original))

@photo is contained the details of the image

then in the email view file

<%= image_tag attachments['image.jpg'].url %>

add this.. it works perfectly for me.

Upvotes: 2

Devd
Devd

Reputation: 343

You need to have a complete web url in emails to display images

"/system/service_requests/prescriptions/000/000/237/medium/foo_bar_thumbnail_image7.jpg?1455861395".
This won't work. Its a path only known to your server.

So you need to have it like this(complete url):

"http://www.example.com/system/service_requests/prescriptions/000/000/237/medium/foo_bar_thumbnail_image7.jpg?1455861395".

Here http://www.example.com is your domain.

If image is stored into app's public directory, Use this:

Rails.root.join(@object.image_name.url)

If image is stored somewhere else like S3, Use this:

@object.image_name.url

PS: This image should be publicly accessible.

Upvotes: 2

Related Questions