4

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..

Maxence
  • 2,029
  • 4
  • 18
  • 37
  • Check the read permission of image on production server, or check the mime-type (read this https://stackoverflow.com/a/8385736/5746504) – Moamen Naanou Nov 13 '17 at 10:27
  • 1
    Have you examined the raw source of the email in your client? Can you see the message part that contains the image? Does the image_tag look ok? – Frederick Cheung Nov 18 '17 at 20:22

4 Answers4

0

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

# config/initializer/preview_interceptors.rb
ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)
Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
  • thanks @Aniket I ave added your line of code but no success. The email sent from development has an inline attachment but the BASE64 string is nearly empty. String is only `iVBORwo=` eventhough the image is present in development. If email sent from production then the file is present in the email with a long BASE64 string ... – Maxence Nov 13 '17 at 12:46
  • I have also this line in my development : `config.action_mailer.asset_host = "https://myappnamehere.herokuapp.com/"` But when I remove it it doesnt do anything – Maxence Nov 13 '17 at 12:52
  • Can you give me browser log of that image. – Aniket Tiwari Nov 13 '17 at 15:36
0

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.

Roc Khalil
  • 1,365
  • 6
  • 22
  • Thanks Roc. I want to inline attach the email, not only make a reference to an S3 object. I want my users to have the logo viewable even if my user is offline – Maxence Nov 13 '17 at 12:47
  • Ah okay, I wasn't sure what the inline thing does.. try adding the inline file from S3 maybe that might work; it's surely an issue with the URL. – Roc Khalil Nov 13 '17 at 13:29
  • Yes I do that already for user submitted image. I retrieve it from S3, Base64 encode it and inline-attach it in my email. I am more into knowing why in development I have a different behavior. (it does work in production so all fine, it's all really to understand the development setup properly) – Maxence Nov 13 '17 at 13:32
0

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.

Manish Jaiswal
  • 442
  • 5
  • 19
0

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

Aravindhan R
  • 270
  • 5
  • 26