简体   繁体   English

Ruby on Rails:为什么我的嵌入式图像没有显示在Gmail或Office 365中?

[英]Ruby on Rails: Why doesn't my embedded image display in Gmail or Office 365?

I'm trying to embed images within HTML emails so that they display inline, but for some reason the image does show in Outlook software but not in Gmail (online) or Office 365 (online). 我正在尝试将图像嵌入HTML电子邮件中,以便它们以嵌入式方式显示,但是由于某些原因,该图像的确显示在Outlook软件中,但不显示在Gmail(在线)或Office 365(在线)中。 I have been referring to http://api.rubyonrails.org/classes/ActionMailer/Base.html as well as the SO question What is the right way to embed image into email using Rails? 我一直在指http://api.rubyonrails.org/classes/ActionMailer/Base.html以及SO问题。使用Rails将图像嵌入电子邮件中的正确方法是什么?

And my code is the following: 我的代码如下:

environments/development.rb and production.rb: 环境/development.rb和production.rb:

config.action_mailer.asset_host = "http://localhost:3000"

Email view: 电子邮件视图:

<%= image_tag('logo.jpg') %>

This displays the image in Outlook: 这将在Outlook中显示图像:

在此处输入图片说明

But not in Gmail or Office 365: 但不在Gmail或Office 365中:

在此处输入图片说明

In the log I get the following img src for this: 在日志中,我得到以下img src

<img src="http://localhost:3000/assets/logo-9b88158fa35240340cc52aa5d01800b12e6e1de5ddb99e246bd0583c7a5611cd.jpg" alt="Logo" />

I have tried a variety of approaches from different people, which include the SO questions: 我尝试了来自不同人的多种方法,其中包括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 无法在ActionMailer中呈现内嵌附件在 gmail中无法正确显示Rails附件内嵌 在ActionMailer中 使用内嵌图像发送HTML电子邮件

But none of them seem to solve my issue. 但是他们似乎都没有解决我的问题。

The code I have tried is: 我尝试过的代码是:

(Attempt 1) (尝试1)

Mailer: 邮件:

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

Email View: 电子邮件查看:

<%= image_tag attachments['logo.jpg'].url -%>

Log: 日志:

<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--

(Attempt 2) (尝试2)

Mailer: 邮件:

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

Email View: 电子邮件查看:

<%= image_tag attachments['logo.jpg'].url -%>

(Attempt 3) (尝试3)

Mailer: 邮件:

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

Email View: 电子邮件查看:

<%= image_tag attachments['logo.jpg'].url -%>

This gives me a UTF error 这给了我一个UTF错误

(Attempt 4) (尝试4)

Mailer: 邮件:

  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

View: 视图:

<%= image_tag "data:image/jpg;base64,#{attachments['logo.jpg'].url}" %>

Log: 日志:

<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--

But none of these seem to work, its also worth adding that only the code at the top shows the image in Outlook, all the others stop me from seeing the image in Outlook, Gmail, and Office365. 但是这些方法似乎都不起作用,还值得补充的是,只有顶部的代码才能显示Outlook中的图像,所有其他代码使我无法在Outlook,Gmail和Office365中看到图像。

Can someone please help me with this issue as I can't make sense of it. 有人可以帮我解决这个问题,因为我无法理解。

I am using Rails 4.2.6 我正在使用Rails 4.2.6

Gmail absolutely cannot read your image from your computer to show up. Gmail绝对无法从您的计算机上读取图像以显示出来。

You should upload your logo.jpg to your production server or another host like amazon s3 or google picasa or whatever as long as you have an live photo url: " http://abc.def/logo.jpeg ". 您应该将您的logo.jpg上传到您的生产服务器或其他主机,如Amazon s3或google picasa或任何其他主机,只要您具有实时照片网址:“ http://abc.def/logo.jpeg ”。

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM