Ruby-on-rails 使用 Rails 将图像嵌入电子邮件的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4918414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the right way to embed image into email using Rails?
提问by Alexey Zakharov
What is the right way to embed an image into email using Rails?
使用 Rails 将图像嵌入电子邮件的正确方法是什么?
回答by Tyrone Wilson
I combined the answer from Oksana with a custom helper approach and got the following to work quite nicely.
我将 Oksana 的答案与自定义帮助程序方法相结合,并使以下内容可以很好地工作。
app/helpers/email_helper.rb
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
app/mailers/base_mailer.rb
class BaseMailer < ActionMailer::Base
add_template_helper(EmailHelper)
end
app/mailers/my_mailer.rb
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
app/views/layouts/email.html.erb
<%= email_image_tag("company_logo.png") %>
<%= email_image_tag("company_logo.png") %>
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.
请注意 **options 使标签更具可扩展性,但它只能在 ruby >=2 中工作。要在 ruby < 2 中完成这项工作,您必须使用旧的处理关键字选项的方法。
回答by Joel
RAILS 5
导轨 5
In your mail method add your inline attachment pointing to your image:
在您的邮件方法中添加指向您的图像的内联附件:
class ConfirmationMailer < ActionMailer::Base
def confirmation_email
attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png")
mail(to: email, subject: 'test subject')
end
end
Then in your mail html view an image_tagwith the attachment url:
然后在您的邮件 html 查看image_tag带有附件 url 的一个:
<%= image_tag(attachments['logo.png'].url) %>
回答by Pavan Katepalli
Adding onto Oksana and tdubs' answers
添加到 Oksana 和 tdubs 的答案
The module tdubs wrote works on desktop, but for the mobile gmail client, the images appeared as attachments. To fix this, do this for the
模块 tdubs 在桌面上编写作品,但对于移动 gmail 客户端,图像显示为附件。要解决此问题,请为
app/helpers/email_helper.rb
应用程序/helpers/email_helper.rb
module EmailHelper
def email_image_tag(image, **options)
attachments[image] = {
:data => File.read(Rails.root.join("app/assets/images/emails/#{image}")),
:mime_type => "image/png",
:encoding => "base64"
}
image_tag attachments[image].url, **options
end
end
For the rest, follow tdubs's answer.
其余的,请遵循 tdubs 的回答。
回答by Aamir
After a lot of research i have found very cleaner way to embed image in email.
Just add following line in production.rband development.rb
经过大量研究,我发现在电子邮件中嵌入图像的方法非常简洁。只需添加以下行production.rb和development.rb
config.action_mailer.asset_host = 'YOUR HOST URL'
In your view embed image by using following code.
在您的视图中使用以下代码嵌入图像。
<%= image_tag('My Web Site Logo.png') %>
Note: Make sure to update YOUR HOST URLand My Web Site Logo.pngin above code.
注意:确保在上面的代码中更新您的主机 URL和我的网站 Logo.png。
For basic details of usage of Action Mailer, please refer to ActionMailer::Base.
Action Mailer 的基本使用方法请参考ActionMailer::Base。
回答by strohy1210
Copy pasted from here
从这里复制粘贴
Inline Attachments
内嵌附件
You can also specify that a file should be displayed inline with other HTML. This is useful if you want to display a corporate logo or a photo.
您还可以指定文件应与其他 HTML 内联显示。如果您想显示公司徽标或照片,这将非常有用。
class Notifier < ApplicationMailer
def welcome(recipient)
attachments.inline['photo.png'] = File.read('path/to/photo.png')
mail(to: recipient, subject: "Here is what we look like")
end
end
And then to reference the image in the view, you create a welcome.html.erb file and make a call to image_tag passing in the attachment you want to display and then call url on the attachment to get the relative content id path for the image source:
然后要在视图中引用图像,创建一个welcome.html.erb 文件并调用image_tag 传入要显示的附件,然后调用附件上的url 以获取图像的相对内容ID 路径来源:
<h1>Please Don't Cringe</h1>
<%= image_tag attachments['photo.png'].url -%>
As we are using Action View's image_tag method, you can pass in any other options you want:
当我们使用 Action View 的 image_tag 方法时,您可以传入任何其他您想要的选项:
<h1>Please Don't Cringe</h1>
<%= image_tag attachments['photo.png'].url, alt: 'Our Photo', class: 'photo' -%>
回答by regex
I don't know much about rails, but I've worked on projects in C# that create emails and then insert them in a users inbox via the Google APIs. To create the email, I had to generate the email string from scratch. If you enable multipart for the email, then the image bytes will be included in a multipart section using base64 encoding.
我对 Rails 了解不多,但我曾在 C# 中开发过创建电子邮件然后通过 Google API 将它们插入用户收件箱的项目。要创建电子邮件,我必须从头开始生成电子邮件字符串。如果您为电子邮件启用 multipart,则图像字节将包含在使用 base64 编码的 multipart 部分中。
You may want to check out the TMail and RubyMail packages to see if they support these actions for you.
您可能想查看 TMail 和 RubyMail 包,看看它们是否支持您的这些操作。

