Rails 4, view Mailer, view attached images

I have a problem with rails 4.1.6. Preview email program.

I want to see attached images in preview mode, but this does not work. I find it wrong

There is my code:

Mail file

class AppMailer < ActionMailer::Base default from: "robot@mysite.com" # AppMailer.test_mail.deliver def test_mail attachments.inline['rails.png'] = File.read(Rails.root.join('app', 'assets', 'images', 'rails.png')) mail( to: 'my-email@gmail.com', subject: "test letter", template_path: "mailers", template_name: "test" ) end end 

Preview file

 class MailerPreview < ActionMailer::Preview def app_mailer AppMailer.test_mail end end 

Template file

 %p Hello, World! %p= image_tag attachments['rails.png'].url 

When will I go to

 /rails/mailers/mailer/app_mailer 

I see a preview page, but the images do not work. Html code output

 <p>Hello, World!</p> <p><img src="cid:544d1354a8aa0_fda8082dbf8258ca@admins-air.mail"></p> 

So. I think I need to find a way to get the path / in / file instead of CID in preview mode

(When I sent a letter to my inbox - the letter looks fine)

What am I doing wrong in preview mode?

+6
ruby-on-rails ruby-on-rails-4 actionmailer
Oct 26 '14 at 15:46
source share
3 answers

For Rails> = 4.2, you must create an initializer to preview the images:

 # config/initializer/preview_interceptors.rb ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor) 
+10
Feb 21 '16 at 14:23
source share

Until the extended viewing of Rails messages is expanded to support viewing attachments, I use this (hacker) upgrade to Mail::Part#url to embed the attachment data in the URL itself. This allows me to see my embedded images in the preview (provided that I turned on INLINE_MAIL_PART_URLS ), keeping the original behavior in the appropriate settings.

 module InlineMailPartUrls def url if ENV["INLINE_MAIL_PART_URLS"] == "true" "data:#{mime_type};base64,#{Base64.encode64(body.decoded)}" else super end end end class Mail::Part prepend InlineMailPartUrls end 

I save this code until config/initializers/inline_mail_part_urls .

https://gist.github.com/softcraft-development/2ed70a2a4d6e2c829fac

+4
Feb 12 '15 at 19:52
source share

You are not doing anything wrong; this is a flaw in the way Rails Mail Previewer is created.

The Rails Mailer code intelligently generates URLs for attachments that link to "parts" of mail in multi-page email. The "led" in the URL for the <img> refers to the "content identifier" of a particular part / attachment. Here's how email URLs work.

However, the preview controller does not render in the context of the mail client: it is a standard web browser. There is no cid URL protocol scheme, and there is no multi-page email for the link (all of these are just standard HTML documents). Rails::MailersController is currently not smart enough to figure this out, and just sends the email as it is.

In order to do this job, you will have to find all the links to cid: URLs and replace them with regular http: URLs to yourself, and then return the contents for various attachments.

Here's a problem open on GitHub / rails to do this , but so far it's not complete.

+1
Feb 12 '15 at 18:37
source share



All Articles