添加附件会破坏邮件内容 rails

Adding an attachment breaks mail content rails

我使用 Rails 4.2 应用程序,我需要将邮件的 PDF 版本附加到邮件本身。我找到了一种渲染视图并将渲染视图转换为 PDF 的方法,但是向邮件添加附件会导致无法发送其内容。 附件发送代码:


    def failed_charge(bill)
      @monthly_bill = bill
      @organization = bill.organization
      @edit_account_url = edit_account_url(organization)
      I18n.with_locale(organization.locale) do
        @date = I18n.l Time.now.utc.to_date, format: :medium
        @display_period = [bill.period_start, bill.period_end]
          .map { |t| I18n.l(t.to_date, format: :medium).strip }
          .join(' – ')
    
        invoice_content = WickedPdf.new.pdf_from_string(
          render_to_string(template: 'billing_mailer/failed_charge'),
          {
            margin: {top: 0, bottom: 0, left: 0, right: 0}
          }
        )
    
        File.open('invoice.pdf', 'wb') do |file|
          file.write invoice_content
        end
    
        attachments['invoice.pdf'] = invoice_content
    
        mail(
          to: bill.billing_email,
          bcc: [INVOICES_EMAIL_FULL, SUPPORT_EMAIL_FULL],
          subject: I18n.t('billing_mailer.subject_failed_charge'),
        )
      end
    end

结果邮件:


    Date: Wed, 08 Sep 2021 19:13:16 +0400
    From: GlassFrog Billing 
    To: sally@fictional.com
    Message-ID: 
    Subject: There was a problem charging your credit card for GlassFrog
    Mime-Version: 1.0
    Content-Type: multipart/mixed;
     boundary="--==_mimepart_6138d30c9a0db_7215116ac5527d";
     charset=UTF-8
     Content-Transfer-Encoding: 7bit

不发送附件的代码:


    def failed_charge(bill)
      @monthly_bill = bill
      @organization = bill.organization
      @edit_account_url = edit_account_url(organization)
      I18n.with_locale(organization.locale) do
        @date = I18n.l Time.now.utc.to_date, format: :medium
        @display_period = [bill.period_start, bill.period_end]
          .map { |t| I18n.l(t.to_date, format: :medium).strip }
          .join(' – ')
    
        invoice_content = WickedPdf.new.pdf_from_string(
          render_to_string(template: 'billing_mailer/failed_charge'),
          {
            margin: {top: 0, bottom: 0, left: 0, right: 0}
          }
        )
    
        File.open('invoice.pdf', 'wb') do |file|
          file.write invoice_content
        end
    
        mail(
          to: bill.billing_email,
          bcc: [INVOICES_EMAIL_FULL, SUPPORT_EMAIL_FULL],
          subject: I18n.t('billing_mailer.subject_failed_charge'),
        )
      end
    end

结果邮件:


    Date: Wed, 08 Sep 2021 19:22:18 +0400
    From: GlassFrog Billing 
    To: sally@fictional.com
    Message-ID: 
    Subject: There was a problem charging your credit card for GlassFrog
    Mime-Version: 1.0
    Content-Type: multipart/alternative;
     boundary="--==_mimepart_6138d52a674b5_75fc116ac69560";
     charset=UTF-8
    Content-Transfer-Encoding: 7bit
    ----==_mimepart_6138d52a674b5_75fc116ac69560
    Content-Type: text/plain;
     charset=UTF-8
    Content-Transfer-Encoding: quoted-printable
    GlassFrog Logo
    Sep 8, 2021
    ***********************************************************
    There was a problem charging your credit card for GlassFrog
    ***********************************************************
    Dear Sally Payer,
    There was a problem charging your credit card for your GlassFrog account for last month (Feb 1, 2015 =E2=80=93 Feb 28, 2015). Please
    update your credit card information in GlassFrog by visiting your
    organization's Billing & Plans Administration Page 
    ( http://app.glassfrog.local:16124/accounts/15570897/edit ). We
    will make a second attempt to charge your card on the 10th of the
    month.
    Note that accounts that are 10 days past due will be downgraded
    from Premium to Free, with limited features, until payment is
    current.
    If you have any questions or need any help, please don't hesitate
    to reply to this email or contact us 
    ( http://glassfrog.com/contact ).
    Regards,
    GlassFrog Billing
    To:
    Billing Shell
    From:
    HolacracyOne, LLC
    12333 Sowden Rd.
    Ste B #33583
    Houston, Texas 77080-2059
    USA
    -------
    Details
    -------
    Invoice #
    104275561
    Date
    Description
    Amount
    Feb 1, 2015 -
    Feb 28, 2015
    Premium Plan 8 users @9/Month each
    .00
    Total
    .00
    ref. org/15570897
    HolacracyOne, LLC
    12333 Sowden Rd. Ste B #33583, Houston, Texas 77080-2059=
    ----==_mimepart_6138d52a674b5_75fc116ac69560
    Content-Type: text/html;
     charset=UTF-8
    Content-Transfer-Encoding: quoted-printable
    
    =
    
    =
    
    GlassFrog
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    Sep 8, 2021
    
    
    There was a problem charging your credit card for GlassFrog
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    Dear Sally Payer,
    
    
    
    
    There was a problem charging your credit card for your GlassFrog account =
    for last month (Feb 1, 2015 =E2=80=93 Feb 28, 2015). Please update your c=
    redit card information in GlassFrog by visiting your organization's B=
    illing & Plans Administration Page. We will make a second attempt=
     to charge your card on the 10th of the month.
    
    
    
    
    Note that accounts that are 10 days past due will be downgraded from Prem=
    ium to Free, with limited features, until payment is current.
    
    
    
    
    If you have any questions or need any help, please don't hesitate to repl=
    y to this email or cont=
    act us.
    
    
    
    
    Regards,
    
GlassFrog Billing = To: Billing Shell

From: HolacracyOne, LLC
12333 Sowden Rd.
Ste B #33583
Houston, Texas 77080-2059
USA

Details

Invoice # 104275561 Date Description Amount Feb 1, 2015 -= Feb 28, 2015= Premium Plan 8 users @9/Month each
.00 Total .00 ref. org/15570897 HolacracyOne, LLC 12333 Sowden Rd. Ste B #33583, Houston, Texas 77080-2059 ----==_mimepart_6138d52a674b5_75fc116ac69560--

P.S。写入文件用于调试目的

问题出在基础 class,那里有一行:

  after_action :convert_ul_to_table

  def convert_ul_to_table
    body = message.body.to_s
    message.body = to_table(Rinku.auto_link(body))
  end

添加附件检查解决了问题:

  def convert_ul_to_table
    return nil unless message.attachments.empty?

    body = message.body.to_s
    message.body = to_table(Rinku.auto_link(body))
  end