有没有办法让Rails 3.1中的image_tag将图像转换为base64格式,以便将其发送到电子邮件中。
如果没有这样的方法,是否有人可以告诉我如何发送包含存储在服务器端的图像的电子邮件?如果只使用image_tag来渲染图像,则图像不会在电子邮件中显示。
有没有办法让Rails 3.1中的image_tag将图像转换为base64格式,以便将其发送到电子邮件中。
如果没有这样的方法,是否有人可以告诉我如何发送包含存储在服务器端的图像的电子邮件?如果只使用image_tag来渲染图像,则图像不会在电子邮件中显示。
attachments['logo.png'] = File.read(Rails.public_path + '/images/logo.png')
这个文件将作为附件随邮件发送。如果要在电子邮件正文中使用此图像,请使用attachments.inline []
,然后在模板中使用 image_tag attachments [].url
。
此外请注意,Rails 3存在与内联附件有关的问题。由于这个问题,所有普通附件在主要电子邮件客户端(如Thunderbird或Outlook)中都不可见。
def send_mail(user)
attachments.inline['logo.gif'] = File.read("#{Rails.root}/public/logo.gif")
mail( :to=>"#{user.name} #{user.last_name} <#{user.email}>", :subject => "Payment received")
end
然后在邮件视图中:发送邮件的文件send_mail.html
<center> <%= image_tag attachments['logo.gif'].url, :alt => "Our logo" -%> </center>
Paperclip
请参见答案。 - Mark Huk@attachments = {} @attachments['blank'] = File.read("#{Rails.root + '/app/assets/images/logo.png'}")
但是我遇到了这样的错误:NoMethodError: undefined method
inline' for #Hash:0x1051c86c from (pry):55:indaily_email'
出了什么问题? - romanattachments
,因为这是ActionMailer::Base
上的一个方法。只需使用attachments
而不需要@
和初始化。 - Mark Huk