postal: Attachments are not displayed/retrieved in some sent emails

I am having a problem with attachments in emails I send via Postal API. Specifically, recipients using Outlook (tested with Outlook 2013 and Outlook 2016) can’t retrieve e-mail attachments.

Here are the results for emails I sent to different services:

  • Office 365: – Webmail: Attachment displayed. – Outlook: No attachment.

  • Yandex: – Webmail: No attachment. – Outlook: No attachment.

  • Gmail (G-Suite): – Webmail: Attachment displayed. – Outlook: No attachment.

Here is the header of the test email I sent:

received: from api (portal.avrupasafak.com [10.11.10.18]) by Postal with HTTP; Sun, 11 Jun 2017 10:14:43 +0000
date: Sun, 11 Jun 2017 13:14:43 +0300
from: "Sender name" <sender email>
to: recipient email
message-id: <e2a55472-9500-4293-8b51-c52422a841d1@rp.postal.avrupasafak.com>
subject: test
mime-version: 1.0
content-type: multipart/alternative; boundary="--==_mimepart_593d1813bad83_67d336b2002467ac"; charset=UTF-8
content-transfer-encoding: 7bit
dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=avrupasafak.com; s=postal-l18PfP; t=1497176083; bh=R77Icyn7Tg6+HtTNYXqJX2OX71cewJzveVRyUHMb6cw=; h=date:from:to:message-id:subject; b=BWK8ADUgSLGS8MdneI5AB/OaUDdEVBCx6/UPl5AXmzrci4+Tm6cTa5EfNge29GebTvdmivgB7mUmpZe35AkqW0QrLVt6oqO1xQCKXePqb0yC/R3vFsVkuQG91ORwIISUFCuDHqHdBFM9RgbrLljQpgtlpke5GeFK2EdD2/O9Mrc=
x-postal-msgid: mnEIPBw9ec9I

I think the problem might be related to the header of the e-mail. When I send the same e-mail with other services (eg SendGrid) the content-type field in the header is set to “multipart/mixed”. But in Postal this field is “multipart/alternative”. As far as I know, when an attachment is added to an email in the form of “multipart/alternative”, the main content type must be “multipart/mixed”.

About this issue

Most upvoted comments

Test this and add a pull request if it is an improvement.

On Sep 15, 2017 12:39 PM, “alexroosenstein” notifications@github.com wrote:

Quick update, the following snippet seems to work. Please keep in mind, I only tested this on a development node (nor did I test this extensively), so if anyone else could test this as well, that’d be great.

This is a replacement for https://github.com/atech/postal/blob/ 67d0f6514d674ebe823052f0cde0f2931f87e315/app/models/ outgoing_message_prototype.rb#L151-L186

def raw_message @raw_message ||= begin mail = Mail.new if @custom_headers.is_a?(Hash) @custom_headers.each { |key, value| mail[key.to_s] = value.to_s } end mail.to = self.to_addresses.join(', ‘) if self.to_addresses.present? mail.cc = self.cc_addresses.join(’, ') if self.cc_addresses.present? mail.from = @from mail.sender = @sender mail.subject = @subject mail.reply_to = @reply_to if @html_body.blank? && attachments.empty? mail.body = @plain_body else text_part = nil html_part = nil

    if !@plain_body.blank?
      text_part = Mail::Part.new
      text_part.body = @plain_body
    end
    if !@html_body.blank?
      html_part = Mail::Part.new
      html_part.content_type = "text/html; charset=UTF-8"
      html_part.body = @html_body
    end
    if !attachments.empty?
      mail.part :content_type => "multipart/alternative"  do |p|
        p.text_part = text_part
        p.html_part = html_part
      end
      attachments.each do |attachment|
        mail.attachments[attachment[:name]] = {
          :mime_type => attachment[:content_type],
          :content => attachment[:data]
        }
      end
    else
      mail.text_part = text_part
      mail.html_part = html_part
    end
  end
  mail.header['Received'] = "from #{@source_type}

(#{self.resolved_hostname} [#{@ip}]) by Postal with HTTP; #{Time.now.utc.rfc2822.to_s}" mail.message_id = “<#{@message_id}>” mail.to_s end end

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/atech/postal/issues/228#issuecomment-329834754, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMA5weB8LtRrnBG7HICnwpsu0IApRkWks5siqg2gaJpZM4N3Ap3 .

I’ve been experiencing this issue as well.

I did some more digging and eventually stumbled upon this issue in the Ruby mail library: https://github.com/mikel/mail/issues/118

And indeed, when only sending a plaintext body, attachments seem to appear in Outlook. As soon as you send a html body the attachment disappears. This is only on Windows by the way, Outlook on Mac OS seems to work just fine.