Skip to content

Having troubles with attachments #114

@exocode

Description

@exocode

Hi there,
I've troubles sending with attachments. The first run raises an error, when I do the exact same command again, it works:

Complete output

InvoiceMailer.with(user: User.last, invoice: Stripe::Event.construct_from(WebhookEvent.last.data['data']['object'])).order_failed_email.deliver_now
  User Load (1.7ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1  [["LIMIT", 1]]
  WebhookEvent Load (2.2ms)  SELECT "webhook_events".* FROM "webhook_events" ORDER BY "webhook_events"."id" DESC LIMIT $1  [["LIMIT", 1]]
InvoiceMailer#order_failed_email: processed outbound mail in 10943.9ms
Traceback (most recent call last):
        2: from (irb):1
        1: from app/mailers/invoice_mailer.rb:16:in `order_failed_email'
RuntimeError (Can't add attachments after `mail` was called.)
Make sure to use `attachments[]=` before calling `mail`.
>> InvoiceMailer.with(user: User.last, invoice: Stripe::Event.construct_from(WebhookEvent.last.data['data']['object'])).order_failed_email.deliver_now
  User Load (2.6ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1  [["LIMIT", 1]]
  WebhookEvent Load (2.9ms)  SELECT "webhook_events".* FROM "webhook_events" ORDER BY "webhook_events"."id" DESC LIMIT $1  [["LIMIT", 1]]
InvoiceMailer#order_failed_email: processed outbound mail in 9208.0ms
Delivered mail 6284a23f4edf5_5b8924683512c@JJ-MacBook-Pro.local.mail (908.1ms)
Date: Wed, 18 May 2022 09:37:35 +0200
From: noreply@fromme.at
To: some@email.com
Message-ID: <6284a23f4edf5_5b8924683512c@JJ-MacBook-Pro.local.mail>
Subject: =?UTF-8?Q?money grabbing failed
Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="--==_mimepart_6284a23f4d766_5b8924683502d";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_6284a23f4d766_5b8924683502d
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Some comlainings....

----==_mimepart_blah_blah
Content-Type: application/pdf;
 filename=invoice.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=invoice.pdf
Content-ID: <blah_blah@JJ-MacBook-Pro.local.mail>

JVBERi0xLjQKMSovQ3JlYXRvciAo/v8A
dwBrAGgAdABtAGwAAuADEAMgAuADUpCi9Qcm9kdWNl
ciAo/v8AUQB0ACXRpb25EYXRlIChEOjIwMjIw
NTE4MDczNzMxWikKPj4KZW5kb2JqCjMgMCBvYmoKPDwKL1R5cGUgL0V4dEdT
....
...
..
.

Complete mailer class

class InvoiceMailer < ApplicationMailer
  def order_failed_email
    raise "InvoiceMailer.order_failed_email: no User found" if params[:user].blank?

    # grabbing remote PDF and attaching it
    attachments['invoice.pdf'] = URI.open(params[:invoice][:invoice_pdf]).read if params[:invoice][:invoice_pdf].present?
    sending mail
    mail(
      to: params[:user][:email],
      subject: 'money grabbing failed',
      body: "Some comlainings...."
    )

  end
end

# ApplicationMailer
class ApplicationMailer < ActionMailer::Base
  default from: ENV['EMAIL_MAILER']
  layout 'mailer'
end

Anything more I've to concern? Did I miss something?

Gemfile:

gem 'sendgrid-ruby'
gem 'sendgrid-actionmailer'

development.rb

  config.action_mailer.default_url_options = { host: 'localhost', port: ENV.fetch("APP_PORT") }
  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = true
  config.action_mailer.delivery_method = :sendgrid_actionmailer
  config.action_mailer.sendgrid_actionmailer_settings = {
    api_key: ENV['SENDGRID_API_KEY'],
    raise_delivery_errors: true
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions