Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version 1.3.x breaks sending simple text/csv attachments #340

Open
jlw opened this issue Jan 31, 2025 · 5 comments
Open

version 1.3.x breaks sending simple text/csv attachments #340

jlw opened this issue Jan 31, 2025 · 5 comments
Assignees

Comments

@jlw
Copy link

jlw commented Jan 31, 2025

I have a Rails 7 app where one of the mailers includes a few CSV attachments. This mailer has been working as-is for several years. Yesterday I upgraded mailgun-ruby 1.2.16 -> 1.3.2 (along with several other gems) and this morning the email did not include any attachments. I reverted just the mailgun-ruby gem to 1.2.16 and now the emails include the attachments. Here is the affected mailer code that has been working fine with the older versions of this gem:

class AnalyticsReportMailer < ApplicationMailer
  default from: '[email protected]'

  def nightly_user_count
    formatted_time
    attachments[csv_file_name('User counts')] = csv_report(UsersCreatedReport)
    attachments[csv_file_name('User sessions')] = csv_report(UserSessionsReport)
    attachments[csv_file_name('Lead counts')] = csv_report(LeadsCreatedReport)

    mail(
      to: Settings.mailer.deliver_user_counts_to,
      subject: "User Metrics as of #{@formatted_time}"
    )
  end

  private

  def columns
    @columns ||= %i[
      last_24_hours
      last_week
      this_month_to_date
      last_month_to_date
      percent_change
    ]
  end

  def csv_file_name(name)
    "#{name} as of #{@formatted_time}.csv"
  end

  def csv_report(report_class)
    report_class.new.to_csv(columns).join
  end

  def formatted_time
    Time.use_zone('Eastern Time (US & Canada)') do
      now = 1.day.ago.end_of_day
      @formatted_time = now.strftime('%B %-d at %-l:%M %p %Z')
    end
  end
end

As soon as I upgrade to mailgun-ruby v1.3.2 again, the email no longer contains attachments again. I've done this a few times now with consistent results. Here's the only change in my working directory:

diff --git a/Gemfile.lock b/Gemfile.lock
index 90e5cddbf..f706162ae 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -383,8 +383,9 @@ GEM
       net-imap
       net-pop
       net-smtp
-    mailgun-ruby (1.2.16)
-      rest-client (>= 2.0.2)
+    mailgun-ruby (1.3.2)
+      faraday (~> 2.1)
+      mime-types
     marcel (1.0.4)
     matrix (0.4.2)
     method_source (1.1.0)

Obviously I can't provide the full source of the generated email here. Is there any other info you need to resolve this bug?

@jlw
Copy link
Author

jlw commented Jan 31, 2025

One additional note, we have a unit test that did not fail with this upgrade, so whatever is causing this breakage appears to be triggered in the actual delivery process and not in the compilation of the email object that fires in the test environment:

RSpec.describe AnalyticsReportMailer, type: :mailer do
  let(:mail) { AnalyticsReportMailer.nightly_user_count }

  before do
    Time.zone = 'UTC'
    Timecop.freeze(Time.zone.parse('2017-10-11 03:00:00'))
  end
  after do
    Timecop.return
  end

  describe 'nightly_user_count' do
    it 'renders the headers' do
      ...
    end

    it 'renders the html text body' do
      ...
    end

    it 'renders the appropriate attachments' do
      str_date = 'October 9'
      title1 = "User counts as of #{str_date} at 11:59 PM EDT.csv"
      title2 = "User sessions as of #{str_date} at 11:59 PM EDT.csv"
      title3 = "Lead counts as of #{str_date} at 11:59 PM EDT.csv"

      expect(mail.attachments.count).to eq 3
      attachment1 = mail.attachments.first
      expect(attachment1.content_type).to start_with('text/csv')
      expect(attachment1.filename).to eq title1
      attachment2 = mail.attachments.second
      expect(attachment2.content_type).to start_with('text/csv')
      expect(attachment2.filename).to eq title2
      attachment3 = mail.attachments.last
      expect(attachment3.content_type).to start_with('text/csv')
      expect(attachment3.filename).to eq title3
    end
  end
end

@Uaitt
Copy link

Uaitt commented Feb 3, 2025

I faced the same issue after the upgrade to 1.3.2 😅.

Unit tests for my mailers were also passing too 🤷🏻.

@alex-leb alex-leb self-assigned this Feb 3, 2025
@minter
Copy link

minter commented Feb 7, 2025

Confirming this breaks ActionMailer on my Rails 7 app as well

@doutatsu
Copy link

doutatsu commented Feb 9, 2025

Can confirm it broke here on Rails 7.2.2.1. Rolling back to 2.1.16 makes everything work again as normal

@oliwoodsuk
Copy link

Yep, same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants