发送带有附件的电子邮件 [英] Sending email with attachments

查看:144
本文介绍了发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的邮件程序:

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

使用SendGrid发送。这是我的问题,如果我通过Gmail打开电子邮件,则一切正常,电子邮件文本在那里,并且附件已附上。但是,如果我使用OSX的Mail.app或在iPhone上打开它,则会得到以下信息:

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:


部分邮件为MIME格式...

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

有人有什么建议吗?我认为我正确地遵循了Rails指南。

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

这是我拨打 Payments :: LateNoticesMailer.notice(payment.id)的电话。 Delivery

推荐答案

根据 ActionMailer :: Base ,如果使用了多种模板类型,则将全部呈现,并且mime-type将自动设置为multipart /

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.

第一个问题:您是否正在渲染其他类型,例如文本和html?我不建议仅以pdf格式发送电子邮件。即使text和html部分只是指示收件人打开附件,它们也应该在那里。理想情况下,在文本/ html部分中会包含更多信息。

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.

第二,您是否尝试以内联方式查看pdf,而不是作为附件查看?

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

您可以看看电子邮件的原始来源,并使用所看到的结构更新您的帖子吗?标头中将设置一个初始的mime类型。

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.

稍后在电子邮件,您的文本和html中零件应该以类似以下内容的方式进行处理:

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

最后,编码的pdf部分应该具有mime标题,例如:

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

通过简单的测试电子邮件,我以文字发送给自己,html部件和较大的pdf,我可以在iPhone上查看电子邮件。它显示了html部分和一个图标,可让我下载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.

这篇关于发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆