Using Ruby 3.2.2 and Rails 6.1.7.8 I'm getting an ArgumentError attempting to send an email. This previously worked using Ruby 2.7.8 and Rails 6.1.7.7.
RSpec tests that were previously passing are now failing and trials in a Rails Console are also failing.
Error: ArgumentError: wrong number of arguments (given 1, expected 0; required keywords: email, message)
Mailer Class:
class AlertMailer < ApplicationMailer
def pmp_throttling_event(email:, message:)
message = message.with_indifferent_access
pmp_name = message[:pmp_name]
email_body = translate_for_erb(message[:email_body])
email_subject = translate_for_erb(message[:email_subject])
body = ERB.new(email_body).result(binding)
subject = ERB.new(email_subject).result(binding)
if body.blank? || subject.blank?
logger.error 'email body and subject must be provided'
return
end
mail(to: email, subject: subject) do |format|
format.html { render plain: body }
format.text { render plain: ActionController::Base.helpers.strip_tags(body) }
end
end
end
Attempt 1:
AlertMailer.pmp_throttling_event(email: 'test@example.com', message: { email_body: 'test', email_subject: 'test' }).deliver_now
Attempt 2 (using an argument hash):
args = {email: 'mknittel@bamboohealth.com', message: { email_body: 'test', email_subject: 'test' }}
AlertMailer.pmp_throttling_event(**args).deliver_now
All attempts, including the RSpec tests result in the following error:
[2024-08-13T11:57:13.261031#49822] DEBUG -- [rails] [reqId:|usr:] AlertMailer#pmp_throttling_event: processed outbound mail in 0.6ms
ArgumentError: wrong number of arguments (given 1, expected 0; required keywords: email, message)
from /Users/<username>/Projects/<application_name>/app/mailers/alert_mailer.rb:4:in `pmp_throttling_event'
Expected Result (from prior to ruby upgrade):
[2] pry(main)> AlertMailer.pmp_throttling_event(email: 'test@example.com', message: { email_body: 'test', email_subject: 'test' }).deliver_now
[2024-08-13T09:43:26.200544 #93395] DEBUG -- [rails] [reqId:|usr:] AlertMailer#pmp_throttling_event: processed outbound mail in 92.4ms
[2024-08-13T09:43:26.221136 #93395] INFO -- [rails] [reqId:|usr:] Delivered mail 66bb62fe31d3a_16cd316a8-35c@mknittel-mac-J7R2QYG4RR.mail (20.5ms)
[2024-08-13T09:43:26.221172 #93395] DEBUG -- [rails] [reqId:|usr:] Date: Tue, 13 Aug 2024 09:43:26 -0400¶From: <Application Name><NoReply@<application_name>.net>>¶To: test@example.com¶Message-ID: <66bb62fe31d3a_16cd316a8-35c@<username>.mail>¶Subject: test¶Mime-Version: 1.0¶Content-Type: multipart/alternative;¶ boundary="--==_mimepart_66bb62fe30d98_16cd316a8-46b";¶ charset=UTF-8¶Content-Transfer-Encoding: 7bit¶¶¶----==_mimepart_66bb62fe30d98_16cd316a8-46b¶Content-Type: text/plain;¶ charset=UTF-8¶Content-Transfer-Encoding: 7bit¶¶test¶----==_mimepart_66bb62fe30d98_16cd316a8-46b¶Content-Type: text/html;¶ charset=UTF-8¶Content-Transfer-Encoding: 7bit¶¶test¶----==_mimepart_66bb62fe30d98_16cd316a8-46b--
=> #<Mail::Message:22800, Multipart: true, Headers: <Date: Tue, 13 Aug 2024 09:43:26 -0400>, <From: <Application Name><NoReply@<application_name>.net>>, <To: test@example.com>, <Message-ID: <66bb62fe31d3a_16cd316a8-35c@<username>.mail>>, <Subject: test>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; charset=UTF-8; boundary="--==_mimepart_66bb62fe30d98_16cd316a8-46b">, <Content-Transfer-Encoding: 7bit>>
I've tried to drop Rails back down to 6.1.7.7 and bump to 7.x (but this caused more upgrade issues than I have time to work through now). I've also tried bumping the Ruby version to 3.2.5 using Rails 6.1.7.8. Starting to think there's a compatibility issue between Ruby 3.2.x and Rails 6.1.7.x. Any ideas on a resolution to this issue?
Did you try this
class AlertMailer < ApplicationMailer
def pmp_throttling_event(args={})
args[:email]
args[:message]
...
end
end
AlertMailer.pmp_throttling_event(
email: 'test@example.com',
message: { email_body: 'test', email_subject: 'test' }
).deliver_now