Devise无法通过ActionMailer发送邮件问题排查求助
Hey there, let's troubleshoot why your Devise password reset emails aren't sending even though your contact form works perfectly with Mailgun. Here are targeted steps to get to the bottom of this:
First, confirm Devise is using your ActionMailer setup correctly. Head to config/initializers/devise.rb and verify these settings:
# Make sure the sender matches your verified Mailgun domain config.mailer_sender = 'no-reply@your-verified-domain.com' # Ensure Devise uses the right mailer (default or your custom one) config.mailer = 'Devise::Mailer' # Or your custom mailer class if you've built one
Also, check that Devise's default email templates don't have hardcoded invalid addresses or broken formatting.
Since your contact form works, your global Mailgun setup is solid—but Devise's requests might have subtle issues. Log into your Mailgun dashboard and check the Logs tab for failed deliveries related to Devise emails. Look for errors like:
- Invalid recipient formatting
- Domain verification mismatches
- Spam flagging from the recipient's email provider
Mailgun's logs will give you specific error messages to act on.
raise_delivery_errors for Detailed Exceptions You already enabled raise_delivery_errors = true in config/environments/production.rb—now trigger a password reset request and check your production logs (log/production.log). Look for explicit exceptions like:
- SMTP authentication failures
- Timeouts connecting to Mailgun
- API errors returned by Mailgun
To get even more context, you can temporarily add logging to Devise's mailer (create a custom mailer if you haven't already):
# app/mailers/custom_devise_mailer.rb class CustomDeviseMailer < Devise::Mailer def reset_password_instructions(record, token, opts={}) logger.debug "Sending reset email to: #{record.email}" logger.debug "Mail options: #{opts.inspect}" super end end
Update config/initializers/devise.rb to use this custom mailer, then check the logs for any red flags in the email parameters.
It's easy to accidentally use development environment values in production. Double-check your config/environments/production.rb ActionMailer settings:
config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.mailgun.org', port: 587, domain: 'your-verified-domain.com', user_name: 'postmaster@your-verified-domain.com', password: 'your-mailgun-api-key', authentication: 'plain', enable_starttls_auto: true } config.action_mailer.raise_delivery_errors = true
Ensure all values match exactly what's in your Mailgun dashboard—no typos or leftover dev credentials.
Even if Mailgun accepts the email, recipient providers might flag Devise's default templates as spam. Try these fixes:
- Check the recipient's spam folder for the reset email
- Verify your domain's SPF and DKIM records are correctly configured in Mailgun
- Customize Devise's email subject line (e.g., change "Reset password instructions" to "Reset your [Your App Name] password") to avoid generic spam triggers
If the default Devise mailer is still giving you trouble, build a fully custom one to eliminate hidden configuration issues:
# app/mailers/custom_devise_mailer.rb class CustomDeviseMailer < ApplicationMailer default from: 'no-reply@your-verified-domain.com' def reset_password_instructions(record, token) @token = token @resource = record mail(to: record.email, subject: 'Reset Your Password') end end
Update config/initializers/devise.rb to use this mailer, then test the password reset flow again.
内容的提问来源于stack exchange,提问作者Christopher Warrington

