Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email with attachments

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

like image 317
TheDelChop Avatar asked Sep 09 '12 13:09

TheDelChop


2 Answers

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.

like image 152
edk750 Avatar answered Nov 11 '22 08:11

edk750


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

like image 1
Just Thomas Misund Avatar answered Nov 11 '22 07:11

Just Thomas Misund