我对rails 4.1.6邮件预览版有问题。
我想在预览模式下查看附加的图像,但它不起作用。我认为这是不正确的
下面是我的代码:
Mailer文件
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预览文件
class MailerPreview < ActionMailer::Preview
def app_mailer
AppMailer.test_mail
end
end模板文件
%p Hello, World!
%p= image_tag attachments['rails.png'].url当我转到
/rails/mailers/mailer/app_mailer我看到预览页,但图像不起作用。结果是html代码。
<p>Hello, World!</p>
<p><img src="cid:544d1354a8aa0_fda8082dbf8258ca@admins-air.mail"></p>所以。我想,我应该找到一种方法,在预览模式下获取path/ to /file而不是CID
(当我将信件发送到我的邮箱时-信件看起来很好)
我在预览模式下做错了什么?
发布于 2016-02-21 22:23:55
对于Rails >= 4.2预览图像,您应该创建初始化器:
# config/initializer/preview_interceptors.rb
ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)发布于 2015-02-13 03:52:43
在Rails邮件预览器得到增强以支持附件查看之前,我将对Mail::Part#url使用这种(类似于黑客的)增强来将附件数据嵌入到URL本身中。这让我在预览器中看到我的内联图像(假设我已经打开了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我将这段代码保存到config/initializers/inline_mail_part_urls。
https://gist.github.com/softcraft-development/2ed70a2a4d6e2c829fac
发布于 2015-02-13 02:37:59
您没有做错任何事情;这是Rails邮件预览器设计方式的一个缺陷。
Rails Mailer代码合理地为引用多部分电子邮件中邮件“部分”的附件生成URL。<img>标记的URL中的"cid“是指特定部件/附件的”内容ID“。这就是电子邮件中URL的工作方式。
但是,预览器控制器不是在电子邮件客户端的上下文中呈现的:它是一个标准的web浏览器。没有"cid“URL协议方案,也没有可供参考的多部分电子邮件(它们都是标准的HTML文档)。Rails::MailersController目前还不够聪明,没有意识到这一点,只是按原样呈现电子邮件。
要做到这一点,它必须检测对cid: URL的所有引用,并将它们替换为返回给它自己的常规http: URL,然后返回各种附件的内容。
有一个open issue on GitHub/rails to do this,但到目前为止还不完整。
https://stackoverflow.com/questions/26574811
复制相似问题