RubyOnRails 发送 mail 带附件

本文详细介绍了如何在邮件中同时处理图片和附件,包括引用图片显示与以附件形式发送两种情况,适用于不同场景的应用。

   

   content_type "multipart/mixed"        #邮件里带内容和附件
    part "multipart/alternative" do |alternative|
      alternative.part "text/html" do |html|
        html.body = render_message("test.text.html", body)
      end
      alternative.part "text/plain" do |plain|
        plain.body = render_message("test.text.plain", body)
      end
    end
    @data = ""

    file_name = "/images/test.png"
    File.open(file_name,"rb").each {|read| @data << read}
    attachment :content_type => "image/png", :filename => File.basename(file_name), :body => @data


 

   如果是pdf类型的

 

attachment :content_type => "image/png", :filename => File.basename(file_name), :body => @data

 

  在页面里,如果不引用图片,直接以附件形式发送;

  如果引用图片,可是在邮件的内容处显示图片。

 

<img src="test.png" alt="test mail with picture"/>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值