Sunday, November 19, 2017

Inline email image shown in production not development

Leave a Comment

I am sending html emails through the action mailer and something strange occur: an inline image does appear on the email sent from the production deploy but not from local develomment.

professionnel_mailer.rb

class ProfessionnelMailer < ApplicationMailer      layout 'professionnelmailer'      def notification(professionnel)         attachments.inline['image200.png'] = File.read("#{Rails.root}/app/assets/images/image200.png")         @professionnel = professionnel         mail(to: @professionnel.email, subject: "You have received a notification !")     end     end 

notification.html.erb

<%= image_tag(attachments['image200.png'].url)%> <h1>Hello <%= @professionnel.first_name %> !</h1> 

Of course image200.png is present locally and remotely. And email is received in both cases, then my Amazon AWS SES setup is corrrect in both environments.. Not very sure where it breaks..

4 Answers

Answers 1

For Rails >= 4.2 to preview images you should create initializer:

# config/initializer/preview_interceptors.rb ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor) 

Answers 2

Basically, these images have a localhost:3000 url in them when it's in development mode, while it will have the server's IP or the domain name on production.

To fix this issue, and since you're using Amazon SES, add the needed image to an S3 bucket, and read the image from that bucket directly in your email,

This will help you see the image whether it's from development or production.

Answers 3

As you said in above comments , your base64 string is empty ,i think that is missing part try to include that in your code, use this website for convert your image to base64, like below

<img height="240" width="240" src="data:your_image/png;base64,/* your converted base64-string */" />

try this.

Answers 4

I had the same problem in my program. I have overcome this problem by using another approach. I will send my sample code which i have converted.

This is my old code:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); String UserName = "xyz@someorg.com"; String Password = "my password"; Message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com")); Message.From = new   System.Net.Mail.MailAddress("fromaddress@fromaddress.com");               Message.Subject = "test subject"; Message.Body = "<img src=@'C:\\Sunset.jpg'/>";                 Message.IsBodyHtml = true; System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); smtpClient.Host = "hostname"; smtpClient.Port = 25; smtpClient.Credentials = new  System.Net.NetworkCredential(UserName,Password); smtpClient.Send(message); 

This is my new code:

string attachmentPath = Environment.CurrentDirectory + @"\test.png"; Attachment inline = new Attachment(attachmentPath); inline.ContentDisposition.Inline = true; inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline; inline.ContentId = contentID; inline.ContentType.MediaType = "image/png"; inline.ContentType.Name = Path.GetFileName(attachmentPath);  message.Attachments.Add(inline); 

I think this might help you.

Reference :http://www.systemnetmail.com/faq/4.4.aspx

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment