我正在尝试在HTML电子邮件中嵌入图片,以便它们以内联方式显示,但出于某种原因,图像确实显示在Outlook软件中,但不会显示在Gmail(在线)或Office 365(在线)中。我一直在提到http://api.rubyonrails.org/classes/ActionMailer/Base.html以及SO问题What is the right way to embed image into email using Rails?
我的代码如下:
environments / development.rb和production.rb:
config.action_mailer.asset_host = "http://localhost:3000"
电子邮件视图:
<%= image_tag('logo.jpg') %>
这将在Outlook中显示图像:
但不是在Gmail或Office 365中:
在日志中,我得到以下img src
:
<img src="http://localhost:3000/assets/logo-9b88158fa35240340cc52aa5d01800b12e6e1de5ddb99e246bd0583c7a5611cd.jpg" alt="Logo" />
我尝试过不同人的各种方法,其中包括SO问题:
Unable to render inline attachments in ActionMailer Rails attachments inline are not shown correctly in gmail Send HTML emails with inline images with ActionMailer
但他们似乎都没有解决我的问题。
我尝试的代码是:
(尝试1)
邮件程序:
def send_email(email, unsubscribe)
@url = 'http://localhost:3000/users/login'
@email = email
@unsubscribe = unsubscribe
attachments.inline['logo.jpg'] = File.read('C:/Sites/MySite/app/assets/images/logo.jpg')
mail(from: "#{@email.from_name} <#{@email.account.email}>", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
end
电子邮件视图:
<%= image_tag attachments['logo.jpg'].url -%>
日志:
<img src="cid:57cd4b6a997ae_25703efc6dc786d@My-Laptop.mail" />
----==_mimepart_57cd4b6a9af1e_25703efc6dc78787
Content-Type: image/jpeg;
filename=logo.jpg
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename=logo.jpg
Content-ID: <57cd4b6a997ae_25703efc6dc786d@My-Laptop.mail>
QzovU2l0ZXMvRWFzeU1haWwvYXBwL2Fzc2V0cy9pbWFnZXMvbG9nby5qcGc=
----==_mimepart_57cd4b6a9af1e_25703efc6dc78787--
(尝试2)
邮件程序:
def send_email(email, unsubscribe)
@url = 'http://localhost:3000/users/login'
@email = email
@unsubscribe = unsubscribe
# THE BELOW LINE
attachments.inline['logo.jpg'] = File.join(Rails.root, 'app/assets/images/logo.jpg')
mail(from: "#{@email.from_name} <#{@email.account.email}>", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
end
电子邮件视图:
<%= image_tag attachments['logo.jpg'].url -%>
(尝试3)
邮件程序:
def send_email(email, unsubscribe)
@url = 'http://localhost:3000/users/login'
@email = email
@unsubscribe = unsubscribe
attachments.inline['logo.jpg'] = {
:data => File.read('C:/Sites/MySite/app/assets/images/logo.jpg'),
:mime_type => "image/jpg",
:encoding => "base64"
}
mail(from: "#{@email.from_name} <#{@email.account.email}>", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
end
电子邮件视图:
<%= image_tag attachments['logo.jpg'].url -%>
这给了我一个UTF错误
(尝试4)
邮件程序:
def send_email(email, unsubscribe)
@url = 'http://localhost:3000/users/login'
@email = email
@unsubscribe = unsubscribe
attachments.inline["logo.jpg"] = {
mime_type: "image/jpg",
content: Base64.encode64(File.read(Rails.root.join("app/assets/images/logo.jpg")))
}
mail(from: "#{@email.from_name} <#{@email.account.email}>", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
end
查看:
<%= image_tag "data:image/jpg;base64,#{attachments['logo.jpg'].url}" %>
日志:
<img src="data:image/jpg;base64,cid:57cd4c32e96d0_25704cedd5878982@My-Laptop.mail" />
----==_mimepart_57cd4c32ea670_25704cedd5879035
Content-Type: image/jpg
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename=logo.jpg
Content-ID: <57cd4c32e96d0_25704cedd5878982@My-Laptop.mail>
aVZCT1J3bz0K
----==_mimepart_57cd4c32ea670_25704cedd5879035--
但这些似乎都不起作用,还值得补充的是,只有顶部的代码显示Outlook中的图像,所有其他代码阻止我在Outlook,Gmail和Office365中看到图像。
有人可以帮助我解决这个问题,因为我无法理解它。
我正在使用Rails 4.2.6
答案 0 :(得分:0)
Gmail绝对无法从您的计算机上读取您的图像以显示。
您应该将logo.jpg上传到您的制作服务器或其他主机,例如亚马逊s3或google picasa或其他任何内容,只要您有实时照片网址:“http://abc.def/logo.jpeg”。
希望这会对你有所帮助。