0

I have a column in my User table called img. The image is stored as a Base64 string. I tried using the following tag to show the image:

<img src="data:image/png;base64,${user.img}" alt="User image" />

While this works with letter opener / preview mode, it seems that Gmail doesn't support this.

I'm now trying to create the image as an inline image based on this post: https://stackoverflow.com/a/39710468/891359

What I don't understand is how to get the image correctly displayed. I tried the following combination:

In my mailer:

attachments.inline["user.png"] = {
  :data => @user.img,
  :mime_type => "image/png",
  :encoding => "base64"
}

And in my template:

= image_tag attachments['user.png'].url

Unfortunately this shows up as a broken image icon in Gmail. Am I doing something wrong?

bo-oz
  • 2,842
  • 2
  • 24
  • 44

1 Answers1

1

Ok, solved it myself. The trick was to use content instead of data. Also there was a prefix in my Base64 encoded string that had to be removed. Final solution was changing the mailer:

attachments.inline["user.png"] = {
  :content => @user.img.remove('data:image/png;base64,'),
  :mime_type => "image/png",
  :encoding => "base64"
}
bo-oz
  • 2,842
  • 2
  • 24
  • 44