Sending email with attachments

I have an email program that looks like this:

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" < no-reply@rentingsmart.com >', cc: @payment.landlord_email, subject: "*** Your rent payment of #{@payment.amount_due} is overdue ***" end end 

which I am sending using SendGrid. Here is my problem, if I open the letter through Gmail, everything works fine, the email text is there, and the application is attached. However, if I open it using OSX Mail.app or on my iPhone, I just get the following:

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

Does anyone have any clues? I think I follow Rails correctly.

Here is the call I'm making Payments::LateNoticesMailer.notice(payment.id).deliver

+4
source share
3 answers

According to the api docs for ActionMailer :: Base , if several types of templates are used, all of them are displayed, and the mime type is automatically set to multipart / alternative.

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

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

Secondly, are you trying to view a pdf file, not as an attachment?

Can you take a look at the original source of the email message and update the message with the structure you see? The header will set the initial mime type. 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 suggests that the parts to be observed are not alternative versions of the same information, but instead instruct the mail client to display them clearly.

Later in the email, your texts and html parts should be done as follows:

 ----==_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, for example:

 ----==_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 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 allows you to download a pdf file.

+2
source

Some email clients may require that the email contain a text part in order to display it correctly.

+1
source

I just came across this post when converting a Rails application from 2.3 to 3.2. I thought I converted my mailing list correctly, but the old version indicated content_type: "multipart/mixed" in the parameters. However, when I deleted this, I received the correct attachments and HTML and text text. I think having this setting there turned upside down everything Rails does to accommodate different types of content, which I didn't want. Thanks to the original answer for leading me in this direction.

0
source

Source: https://habr.com/ru/post/1433223/


All Articles