生产环境下内联邮件图片显示,但开发环境下不显示

4

我正在通过Action Mailer发送HTML邮件,但发现一个奇怪的问题:在生产环境下发送的电子邮件中有一张嵌入图片,但在本地开发环境中没有出现。

professionnel_mailer.rb

class ProfessionnelMailer < ApplicationMailer

    layout 'professionnelmailer'

    def notification(professionnel)
        attachments.inline['image200.png'] = File.read("#{Rails.root}/app/assets/images/image200.png")
        @professionnel = professionnel
        mail(to: @professionnel.email, subject: "You have received a notification !")
    end    
end

notification.html.erb

<%= image_tag(attachments['image200.png'].url)%>
<h1>Hello <%= @professionnel.first_name %> !</h1>

当然,image200.png在本地和远程都存在。并且在两种情况下都能接收电子邮件,那么我的Amazon AWS SES设置在两个环境中都是正确的。不太确定出了什么问题。

检查生产服务器上图像的读取权限,或者检查MIME类型(阅读此文章https://dev59.com/HF3Va4cB1Zd3GeqPEuAa#8385736)。 - Moamen Naanou
1
你是否检查了客户端中电子邮件的原始代码?你能看到包含图片的消息部分吗?image_tag 看起来正常吗? - Frederick Cheung
4个回答

0

基本上,这些图像在开发模式下具有 localhost:3000 的网址,在生产模式下将具有服务器的 IP 或域名。

为了解决这个问题,并且你正在使用 Amazon SES,请将所需的图像添加到 S3 存储桶中,并直接从该存储桶中读取电子邮件中的图像。

这将帮助你在开发或生产环境下查看图像。


谢谢 Roc。我想要内联附加电子邮件,而不仅仅是引用 S3 对象。我希望我的用户可以在离线状态下查看标志。 - Maxence
啊,好的,我不确定inline是什么意思。试着从S3添加inline文件,也许那样会起作用;这肯定是一个URL的问题。 - Roc Khalil
是的,我已经为用户提交的图像执行了这个操作。我从S3中检索它,对其进行Base64编码,并将其内联附加到我的电子邮件中。我更想知道为什么在开发中我有不同的行为。(它在生产中可以工作,所以一切都很好,只是要正确理解开发设置) - Maxence

0

根据您在上面的评论中所说,您的base64字符串为空,我认为这是缺失的部分,请尝试在您的代码中包含它,使用this website将您的图像转换为base64,如下所示

<img height="240" width="240" src="data:your_image/png;base64,/* your converted base64-string */" />

请尝试这个。


0

对于Rails >= 4.2,如果要预览图像,您应该创建初始化程序:

# config/initializer/preview_interceptors.rb
ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)

感谢@Aniket,我已经添加了你的代码行,但没有成功。从开发环境发送的电子邮件具有内联附件,但BASE64字符串几乎为空。字符串仅为“iVBORwo =”,尽管图像在开发中存在。如果从生产环境发送电子邮件,则文件将出现在电子邮件中,并带有长的BASE64字符串... - Maxence
我在我的开发中也有这一行:config.action_mailer.asset_host = "https://myappnamehere.herokuapp.com/" 但是当我将其删除时,它并没有产生任何作用。 - Maxence
你能给我那张图片的浏览器日志吗? - Aniket Tiwari

0

我在我的程序中遇到了同样的问题。我通过使用另一种方法克服了这个问题。我将发送我已转换的示例代码。

这是我的旧代码:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
String UserName = "xyz@someorg.com";
String Password = "my password";
Message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com"));
Message.From = new  
System.Net.Mail.MailAddress("fromaddress@fromaddress.com");              
Message.Subject = "test subject";
Message.Body = "<img src=@'C:\\Sunset.jpg'/>";                
Message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = "hostname";
smtpClient.Port = 25;
smtpClient.Credentials = new 
System.Net.NetworkCredential(UserName,Password);
smtpClient.Send(message);

这是我的新代码:

string attachmentPath = Environment.CurrentDirectory + @"\test.png";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);

message.Attachments.Add(inline);

我认为这可能会对你有所帮助。

参考资料:http://www.systemnetmail.com/faq/4.4.aspx


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接