Rails 4, Mailer preview, preview attached images

半城伤御伤魂 提交于 2020-01-01 05:11:26

问题


I have problem with rails 4.1.6 mailer preview.

I want to see attached images in preview mode, but it doesn't work. I think it's incorrect

There is my code:

Mailer 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 I go to

/rails/mailers/mailer/app_mailer

I see preview page, but images doesn't work. There is resulted html code

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

So. I think, I should to find a way to get path/to/file instead CID in preview mode

(When I sent letter to my mailbox - letter looks fine)

What I am doing wrong in preview mode?


回答1:


For Rails >= 4.2 to preview images you should create initializer:

# config/initializer/preview_interceptors.rb
ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)



回答2:


Until the Rails mail previewer is enhanced to support attachment viewing, I'm using this (hack-ish) enhancement to Mail::Part#url to embed the attachment data into the URL itself. This lets see my inline images in the previewer (assuming I have INLINE_MAIL_PART_URLS turned on) while preserving the original behaviour 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 to config/initializers/inline_mail_part_urls.

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




回答3:


You're not doing anything wrong; it's a shortcoming in the way the Rails Mail Previewer is designed.

The Rails Mailer code, reasonably, generates URLs for attachments that reference mail "parts" in a multipart email. The "cid" in the URL for your <img> tag refers to the "content ID" of the particular part/attachment. This is how URLs within an email work.

However, the previewer controller isn't rendering in the context of an email client: it's a standard web browser. There is no "cid" URL protocol scheme, and no multi-part email to reference (it's all just standard HTML documents). Rails::MailersController currently isn't smart enough to realize this, and just renders the email as-is.

To make this work, it'd have to detect all references to cid: URLs and replace them with regular http: URLs back to itself, then return the content for the various attachments.

There's an open issue on GitHub/rails to do this, but as of yet it's not complete.



来源:https://stackoverflow.com/questions/26574811/rails-4-mailer-preview-preview-attached-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!