如何在邮件中加载图像

时间:2016-03-28 04:47:40

标签: ruby-on-rails

我正在尝试在发送给用户的邮件程序中插入图像。我试过这个

class UserMailer < ApplicationMailer
    default from: 'vamsi@digifutura.com'

    def welcome_email(email)
        email_with_name = %('gates', <gvpmahesh@gmail.com>)
        attachments.inline['truck.png'] = File.read("#{Rails.root.to_s + '/app/assets/images/truck.png'}")
        mail(to: email, subject: 'Look at the truck2')
    end

end

这是视图

This is an experiment to send an email with image
<%= image_tag attachments['truck.png'].url %>

这是控制器

class MailersController < ApplicationController
  def email_send
    UserMailer.welcome_email('gvpm1994@gmail.com').deliver_now
  end

  def show
    render plain: 'OK'
    email_send
  end
end

但上面的代码发送了一个附件,但我正在尝试将图像与电子邮件文本一起呈现。

this is the outcome

3 个答案:

答案 0 :(得分:0)

如果您希望电子邮件内嵌图片(不在外部托管),那么我认为您想要的是Data URI。您的HTML看起来像this

<img width="16" 
     height="16" 
     alt="star" 
     src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" />

我不知道rails是否内置了对此功能的支持,但您自己很容易实现。

答案 1 :(得分:0)

以下是我解决问题的方法

<%= image_tag attachments.inline["truck.png"].url %>

答案 2 :(得分:-1)

做这样的改变:

      for (counter=0; counter<fileLen; ++counter)
      {
        int ch = fgetc(pfile1);
        printf("%02X", ch );
        int written = fputc(ch,pfile2);
        if (written != EOF) 
        { 
          buffer[counter]=ch; 
        }
        else // some error handling
      }

查看:

class UserMailer < ApplicationMailer
    default from: 'vamsi@digifutura.com'

    def welcome_email(email)
        email_with_name = %('gates', <gvpmahesh@gmail.com>)
        attachments.inline['truck.png'] = File.read("#{Rails.root.to_s + '/assets/images/truck.png'}")
        @truck_url = "#{Rails.root.to_s + '/assets/images/truck.png'"
        mail(to: email, subject: 'Look at the truck2')
    end

end