Home > Mobile >  Adding an attachment breaks mail content rails
Adding an attachment breaks mail content rails

Time:09-16

I work with Rails 4.2 application and I need to attach a PDF version of the mail to the mail itself. I found a way to render and convert rendered view into PDF, but adding an attachment to the mail causes not sending its content. The code for sending with attachment:

    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

The resulted mail:

    Date: Wed, 08 Sep 2021 19:13:16  0400
    From: GlassFrog Billing 
    To: [email protected]
    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

The code without sending an attachment:

    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

The resulted mail:

    Date: Wed, 08 Sep 2021 19:22:18  0400
    From: GlassFrog Billing 
    To: [email protected]
    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
    $72.00
    Total
    $72.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
$72.00 Total $72.00 ref. org/15570897 HolacracyOne, LLC 12333 Sowden Rd. Ste B #33583, Houston, Texas 77080-2059 ----==_mimepart_6138d52a674b5_75fc116ac69560--

P.S. Writing to file is for debugging purposes

CodePudding user response:

The problem was in base class, where there was the line:

  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

Adding check for attachments fixed the problem:

  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
  • Related