简体   繁体   English

image_tag在rails中以base64编码

[英]image_tag encoded in base64 in rails

Is there any way to make image_tag in rails 3.1 convert the image into base64 format to send it in the email. 有什么方法可以使rails 3.1中的image_tag将图像转换为base64格式以通过电子邮件发送。

If there is no way to do this can anyone tell me the way to send emails containing images which are stored server side? 如果没有办法这样做,谁能告诉我发送包含存储在服务器端的图像的电子邮件的方法吗? If i just use image_tag for rendering images they are not displayed in the email. 如果我只是使用image_tag渲染图像,则它们不会显示在电子邮件中。

To send images with emails use attachment property on mail to sent the file with mail: 要使用电子邮件发送图像,请使用邮件的附件属性通过邮件发送文件:

attachments['logo.png'] = File.read(Rails.public_path + '/images/logo.png')

This file will be sent with mail in attachments. 该文件将与附件一起发送。 To use this image on email body use attachments.inline[] and then in template image_tag attachments[].url . 要在电子邮件正文上使用此图像,请使用attachments.inline[] ,然后在模板image_tag attachments[].url

Also note that Rails 3 have issue with inline attachments, as they present, all ordinary attachments will not be visible in major email clients, as Thunderbird or Outlook. 还要注意,Rails 3的内嵌附件存在问题 ,因为它们存在,所有普通附件在主要的电子邮件客户端(如Thunderbird或Outlook)中将不可见。

in the mailer you can add inline attachments that you can use in the html for the mail: for example you have a mailer action send_mail(user) 在邮件程序中,您可以添加可以在html中用于邮件的内联附件:例如,您有一个邮件程序动作send_mail(user)

 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 

and then in the mail view: the file send_mail.html 然后在邮件视图中:文件send_mail.html

 <center> <%= image_tag attachments['logo.gif'].url, :alt => "Our logo" -%> </center>

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

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