Sending email with attachments
Asked Answered
D

3

6

I've got a mailer that as follows:

class Payments::LateNoticesMailer < AsyncMailer
  def notice(payment_id)
    @payment = PaymentDecorator.find(payment_id)
    @invoice = @payment.invoice
    template = "payments/invoices/#{@payment.made_with_type.downcase}/show"

    attachments["#{@payment.invoice_filename}.pdf"] =
      WickedPdf.new.pdf_from_string( render_to_string( pdf:      @payment.invoice_filename,
                                                       formats:  [:pdf],
                                                       template: template,
                                                       layout:   "layouts/pdf.html"))

    mail to:      @payment.payer_email,
         from:    '"RentingSmart" <[email protected]>',
         cc:      @payment.landlord_email,
         subject: "*** Your rent payment of #{@payment.amount_due} is overdue ***"
  end
end

which I send using SendGrid. Here's my issue, if I open up the email via Gmail, everything works great, the text of the email is there, and the attachment is attached. However, if I open it up using OSX's Mail.app or on my iPhone, I simply get the following:

This is a multi-part message in MIME format...

Anybody have any tips? I think I am following the Rails guides correctly.

Here is the call that I make Payments::LateNoticesMailer.notice(payment.id).deliver

Devan answered 9/9, 2012 at 13:6 Comment(4)
Have you tried in another client? Might be OS X problemLindner
Can you show the code for the actual delivery. This is most likely being caused by a legacy syntax error from the mail gemJobi
Legacy syntax error? I think the actual delivery is correct but I put it up there anyways.Devan
Does the issue resolve if you define pdf: "#{@payment.invoice_filename}.pdf"instead of pdf: @payment.invoice_filename (i.e. you're missing the .pdf when you render).Claymore
T
2

According to the api docs for ActionMailer::Base, if multiple template types are used, all of them are rendered and the mime-type is automatically set to multipart/alternative.

If you add an attachment, the attachment is placed inside a multipart/mixed container.

First question: Are you rendering other types such as text and html? I would not recommend sending out emails with just a pdf part. Even if the text and html parts simply instruct the recipient to open the attachment, they should be there. Ideally, there would be more information in the text/html parts.

Second, are you trying to view the pdf inline, and not as an attachment?

Can you take a look at the raw source of the email and update your post with the structure you're seeing? There will be an initial mime type set in the header. it will look something like this:

Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="--==_mimepart_50596418be947_c7223fec9d834d3874256";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

This says the parts to follow are not alternative versions of the same information, but instead instruct the email client to display them distinctly.

Later on in the email, your text and html parts should proceeded by something like:

----==_mimepart_50596418be947_c7223fec9d834d3874256
Date: Wed, 19 Sep 2012 06:20:12 +0000
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_50596418be468_c7223fec9d834d38741a5";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

And finally, the encoded pdf part should have a mime header like:

----==_mimepart_50596418be947_c7223fec9d834d3874256
Date: Wed, 19 Sep 2012 06:20:12 +0000
Mime-Version: 1.0
Content-Type: application/pdf;
 charset=UTF-8;
 filename=terms.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=terms.pdf

With a simple test email I just sent to myself with text, html parts, and a large pdf, I can view the email on my iphone. It shows the html part and an icon that lets me download the pdf.

Troll answered 19/9, 2012 at 6:36 Comment(1)
What from this answer would have answered the question?Folly
P
1

Some e-mail clients may require an e-mail to have a plain text part in order to display it correctly.

Psychological answered 19/9, 2012 at 5:58 Comment(0)
H
0

I just ran into this message while converting a Rails app from 2.3 to 3.2. I thought I converted my mailer correctly, but the old version specified content_type: "multipart/mixed" in the options. However, when I removed that, I received the attachments and the HTML and plain-text rendered correctly. I think that having that setting in there overrode whatever Rails does to put in different types of content, which was not what I wanted. Thanks to the original answer for leading me in that direction.

Huysmans answered 26/7, 2013 at 22:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.