来自数据uri的Rails邮件程序图像附件

时间:2016-10-21 22:41:11

标签: ruby-on-rails email gmail email-attachments

我保存了一个签名,该签名是图像数据uri,png的形式,并以字符串形式存储在我的数据库中。

发送电子邮件时,我想将其附加到内联中,但图片仍然显示为无法在Gmail中使用。这是我的代码:

邮件程序:

attachments.inline['signature'] = {
  content: @project_addition.signature.split(',')[1],
  mime_type: 'image/png',
  encoding: 'base64'
}

查看:

<%= image_tag attachments.inline['signature'].url, width: '25%' %>

字符串的前几个字符:

"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABowAAAC4CA....

2 个答案:

答案 0 :(得分:1)

您需要调用def calcMarks(): # input Jack, <marks>... name, marks = input("Enter Marks"), map(int, input().split(", ")) average = sum(marks) / len(marks) print(name + ' ' + str(average)) 来解码base64编码的内容而不是指定编码

Base64.decode64

答案 1 :(得分:1)

如果您没有图像数据uri,则可以在助手中的方法下面放置另一种解决方案

app / helpers / email_helper.rb

  def email_data_uri(image, **options)
    code = File.open("#{Rails.root}/app/assets/images/#{image}", 'r').read
    data = Base64.strict_encode64(code)
    type = image.split('.')[-1]
    name = image.split('/')[-1]
    attachments.inline[name] = {
      content: Base64.decode64(data),
      mime_type: "image/#{type}"
    }
    image_tag attachments.inline[name].url, **options
  end

app / view / campaign_mailer

= email_data_uri 'email/livechat-logo.png', style: 'width: 34px'

添加样式选项