I was following this tutorial right here https://gist.github.com/stevecondylios/16a53b73f22621e3cde2e17096dbf5ca
Which teaches you how to make a simple contact form with mail_form on Rails 6, I'm using Rails 7 and for the most part, the process is the same, it works well in development, as I've tested several times with MailCatcher and the mails get sent correctly, the only problem I'm stumbling upon is when I try to test the form on production.
The problems comes as a 500 Internal Server Error
from Mailgun, when I submit the form, which has me baffled as I have yet to find a similar issue on the internet, I've cross-referenced several tutorials and issues and I find several other error messages, and a couple with 500 Internal Server Error
but none of the answers to those helped.
I pretty much have the exact same configuration as in the tutorial, but here are my configurations, maybe there is something I missed, note that I have added the real "[email protected]" to the "Authorized Recipients" section in Mailgun and that I have a custom domain and is working properly with the rest of Heroku, I also have the right ENV vars set on Heroku:
Contact model:
class Contact < MailForm::Base
attribute :name, validate: true
attribute :email, validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :nickname, captcha: true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "Contact Form Inquiry",
:to => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
in production.rb
ActionMailer::Base.smtp_settings = {
:port => ENV['MAILGUN_SMTP_PORT'],
:address => ENV['MAILGUN_SMTP_SERVER'],
:user_name => ENV['MAILGUN_SMTP_LOGIN'],
:password => ENV['MAILGUN_SMTP_PASSWORD'],
:domain => 'https://www.example.com/',
:authentication => :plain,
}
ActionMailer::Base.delivery_method = :smtp
Heroku logs:
2022-04-28T20:35:32.118071+00:00 heroku[router]: at=info method=POST path="/contacts" host=www.pocket-witch.games request_id=b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b fwd="177.249.170.124" dyno=web.1 connect=0ms service=13ms status=500 bytes=1827 protocol=https
2022-04-28T20:35:32.106145+00:00 app[web.1]: I, [2022-04-28T20:35:32.106072 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Started POST "/contacts" for 177.249.170.124 at 2022-04-28 20:35:32 +0000
2022-04-28T20:35:32.106961+00:00 app[web.1]: I, [2022-04-28T20:35:32.106912 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Processing by ContactsController#create as TURBO_STREAM
2022-04-28T20:35:32.107080+00:00 app[web.1]: I, [2022-04-28T20:35:32.107015 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Parameters: {"authenticity_token"=>"[FILTERED]", "contact"=>{"name"=>"John", "email"=>"[email protected]", "message"=>"Example message", "nickname"=>""}, "commit"=>"Send Message"}
2022-04-28T20:35:32.109199+00:00 app[web.1]: I, [2022-04-28T20:35:32.109137 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Rendered contact_mailer/contact_mail.html.erb within layouts/mailer (Duration: 0.1ms | Allocations: 8)
2022-04-28T20:35:32.109367+00:00 app[web.1]: I, [2022-04-28T20:35:32.109331 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Rendered layout layouts/mailer.html.erb (Duration: 0.3ms | Allocations: 46)
2022-04-28T20:35:32.109834+00:00 app[web.1]: I, [2022-04-28T20:35:32.109777 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Rendered contact_mailer/contact_mail.text.erb within layouts/mailer (Duration: 0.1ms | Allocations: 4)
2022-04-28T20:35:32.109975+00:00 app[web.1]: I, [2022-04-28T20:35:32.109923 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Rendered layout layouts/mailer.text.erb (Duration: 0.2ms | Allocations: 41)
2022-04-28T20:35:32.115988+00:00 app[web.1]: I, [2022-04-28T20:35:32.115921 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Delivered mail 626afa941bd21_45348778b1@b96704a7-29a2-4902-83e1-099673240203.mail (2.8ms)
2022-04-28T20:35:32.116193+00:00 app[web.1]: I, [2022-04-28T20:35:32.116158 #4] INFO -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Completed 500 Internal Server Error in 9ms (Allocations: 3976)
2022-04-28T20:35:32.116915+00:00 app[web.1]: F, [2022-04-28T20:35:32.116874 #4] FATAL -- : [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b]
2022-04-28T20:35:32.116916+00:00 app[web.1]: [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Errno::ECONNREFUSED (Connection refused - connect(2) for 127.0.0.1:25):
2022-04-28T20:35:32.116917+00:00 app[web.1]: [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b]
2022-04-28T20:35:32.116917+00:00 app[web.1]: [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] app/controllers/contacts_controller.rb:11:in `block in create'
2022-04-28T20:35:32.116917+00:00 app[web.1]: [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] app/controllers/contacts_controller.rb:10:in `create'
Edit: I've added some of the Heroku ENV variables below, I've made double sure ENV vars like MAILGUN_DOMAIN
and such match with what I have on my mailgun account.
Some additional things that I've noticed is that error seems to stem from whet the @contact.deliver
action is done, I've tried making my own simple mailer in place of using mail_form, and using .deliver_now
triggers the same error, using deliver_later
does not, but the mail never gets sent, so I suspect the error comes from elsewhere.
Any help would be greatly appreciated, as I've reached an impasse regarding this situation
Thanks to the help of the comments, I was able to notice by the line in the heroku logs:
2022-04-28T20:35:32.116916+00:00 app[web.1]: [b1e694c5-cea3-427f-bbdd-aa3cc3a92d6b] Errno::ECONNREFUSED (Connection refused - connect(2) for 127.0.0.1:25):
The port being used was 25
instead of 587
meaning that the block
ActionMailer::Base.smtp_settings = {
:port => ENV['MAILGUN_SMTP_PORT'],
:address => ENV['MAILGUN_SMTP_SERVER'],
:user_name => ENV['MAILGUN_SMTP_LOGIN'],
:password => ENV['MAILGUN_SMTP_PASSWORD'],
:domain => 'https://www.example.com/',
:authentication => :plain,
}
Was being ignored, by instead encompassing those option inside a config.action_mailer.smtp_settings
I was able to resolve the issue.