使用Rails将图像嵌入到电子邮件中的正确方法是什么? [英] What is the right way to embed image into email using Rails?
问题描述
使用Rails将图像嵌入到电子邮件中的正确方法是什么?
What is the right way to embed an image into email using Rails?
推荐答案
我将Oksana的答案与自定义帮助程序方法,并使得以下功能很好地工作。
I combined the answer from Oksana with a custom helper approach and got the following to work quite nicely.
app / helpers / email_helper.rb
module EmailHelper
def email_image_tag(image, **options)
attachments[image] = File.read(Rails.root.join("app/assets/images/#{image}"))
image_tag attachments[image].url, **options
end
end
app / mailers / base_mailer.rb
class BaseMailer < ActionMailer::Base
add_template_helper(EmailHelper)
end
app / mailers / my_mailer.rb
class MyMailer < BaseMailer
def send_my_mail(email)
mail to: email, subject: "My Subject"
end
end
然后,例如,我要将公司徽标附加到我要使用的电子邮件布局文件中
Then for example where I want to attach the company logo in my email layout file I would use
app / views / layouts / email.html.erb
<% = email_image_tag( company_logo.png)%>
请注意**选项使标签更具扩展性,但仅适用于> = 2的ruby。要使此工作在ruby中< 2您将不得不使用较旧的方式来处理关键字选项。
Note the **options makes the tag more extensible but it will only work in ruby >=2. To make this work in ruby < 2 you will have to use the older way of handling key word options.
这篇关于使用Rails将图像嵌入到电子邮件中的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!