升级至Rails 5.0.6后Mail gem无法发送邮件求助
Hey there, let's troubleshoot this email sending issue you're hitting after upgrading your Rails app from 4.2.5 to 5.0.6. I’ve run into similar head-scratchers before, so here are targeted steps to get to the bottom of it:
Rails 5.x introduced subtle changes to Action Mailer behavior, so let’s verify your settings in config/environments/[your-environment].rb:
- Confirm
config.action_mailer.perform_deliveriesis set totrue—it’s easy for this to get toggled accidentally during upgrades, and it’ll make the console show "sent" without actually delivering. - Review your
smtp_settings: Ensureaddress,port,user_name, andpasswordare correct. For third-party services like SendGrid or Gmail, double-check if you need app-specific passwords or if IP whitelisting is enabled. Also, verifyenable_starttls_autois present (most modern SMTP servers require this). - Make sure
config.action_mailer.delivery_methodis set to:smtp(the default, but worth confirming if you made config tweaks).
Since you’re not seeing error messages, let’s force Rails to surface them:
Add these lines to your environment config file:
config.action_mailer.raise_delivery_errors = true config.action_mailer.logger = Logger.new(STDOUT)
Restart your server, then try sending an email again. This will dump detailed SMTP connection logs and any delivery failures directly to your console—super helpful for spotting issues like authentication failures or connection timeouts.
You can also test directly from the Rails console to bypass controller code:
ActionMailer::Base.mail( from: 'your-sender@example.com', to: 'your-recipient@example.com', subject: 'Test Delivery', body: 'Hello from Rails 5!' ).deliver_now
Even though you updated to Mail 2.7.0, newer gem versions can sometimes clash with older Rails releases. Rails 5.0.6’s default dependency is Mail 2.6.6—try downgrading to that version to rule out compatibility issues:
In your Gemfile:
gem 'mail', '2.6.6'
Run bundle install, restart your app, and test email delivery again.
Check your send_email.rb controller and associated mailer classes:
- If you’re using
deliver_later, ensure your Active Job backend (like Sidekiq or Delayed Job) is running. In development, if you haven’t started the worker, emails will sit in the queue indefinitely. Switch todeliver_nowfor testing to eliminate this variable. - Confirm your mailer class has a valid
default from:setting, and that your email templates exist in the correctapp/views/[mailer-name]directory. Missing templates can sometimes cause silent failures.
To rule out Rails-specific issues, test your SMTP server directly with raw Ruby:
require 'net/smtp' begin smtp = Net::SMTP.new('your-smtp-host.com', 587) smtp.enable_starttls_auto smtp.start('your-domain.com', 'smtp-username', 'smtp-password', :login) do |conn| conn.send_message( "Subject: Direct SMTP Test\n\nThis email was sent via raw Ruby SMTP", 'sender@example.com', 'recipient@example.com' ) end puts "Email sent successfully!" rescue StandardError => e puts "SMTP Error: #{e.message}" end
If this fails, the problem is with your SMTP server/credentials (firewall blocks, account restrictions, etc.). If it works, the issue is isolated to your Rails setup.
内容的提问来源于stack exchange,提问作者MrAutoIt

