[英]Rails 4, Mailer preview, preview attached images
I have problem with rails 4.1.6 mailer preview. 我在Rails 4.1.6邮件预览中遇到问题。
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
有结果HTML代码
<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
我认为,我应该找到一种在预览模式下获取路径/到/文件而不是CID的方法
(When I sent letter to my mailbox - letter looks fine) (当我向信箱发送信件时-信件看起来不错)
What I am doing wrong in preview mode? 在预览模式下我做错了什么?
For Rails >= 4.2 to preview images you should create initializer: 对于> = 4.2的Rails预览图像,您应该创建初始化程序:
# config/initializer/preview_interceptors.rb
ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)
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. 在增强Rails邮件预览器以支持附件查看之前,我将使用
Mail::Part#url
此增强功能将附件数据嵌入URL本身。 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. 这样可以在预览器中查看我的嵌入式图像(假设我已
INLINE_MAIL_PART_URLS
),同时在适当的设置中保留了原始行为。
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
. 我将此代码保存到
config/initializers/inline_mail_part_urls
。
https://gist.github.com/softcraft-development/2ed70a2a4d6e2c829fac https://gist.github.com/softcraft-development/2ed70a2a4d6e2c829fac
You're not doing anything wrong; 您没有做错任何事; it's a shortcoming in the way the Rails Mail Previewer is designed.
这是Rails Mail Previewer设计方式的一个缺点。
The Rails Mailer code, reasonably, generates URLs for attachments that reference mail "parts" in a multipart email. Rails Mailer代码合理地为引用多部分电子邮件中的邮件“部分”的附件生成URL。 The "cid" in the URL for your
<img>
tag refers to the "content ID" of the particular part/attachment. 您的
<img>
标签的URL中的“ cid”是指特定零件/附件的“内容ID”。 This is how URLs within an email work. 这是电子邮件中URL的工作方式。
However, the previewer controller isn't rendering in the context of an email client: it's a standard web browser. 但是,预览器控制器未在电子邮件客户端的上下文中呈现:它是标准的Web浏览器。 There is no "cid" URL protocol scheme, and no multi-part email to reference (it's all just standard HTML documents).
没有“ cid” URL协议方案,也没有多部分的电子邮件可供参考(全部都是标准HTML文档)。
Rails::MailersController
currently isn't smart enough to realize this, and just renders the email as-is. Rails::MailersController
当前还不足以实现这一目标,只能按原样呈现电子邮件。
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. 为了使这项工作有效,它必须检测到所有对
cid:
URL的引用,并用常规的http:
URL替换它们,然后返回各种附件的内容。
There's an open issue on GitHub/rails to do this , but as of yet it's not complete. GitHub / rails上存在一个未解决的问题 ,但到目前为止还没有完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.