我已经保存了一个签名,该签名以图像数据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....
如果您没有图像数据uri,另一个解决方案是在您的帮助器
中的方法下面app/帮助/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
应用程序/视图/campaign_mailer
= email_data_uri 'email/livechat-logo.png', style: 'width: 34px'
添加样式选项
您需要调用Base64.decode64
来解码base64编码的内容,而不指定编码
attachments.inline['signature'] = {
content: Base64.decode64(@project_addition.signature.split(',')[1]),
mime_type: 'image/png'
}