You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails开发环境中联系表单受SMTP设置干扰的问题求助

Fixing "Connection refused - connect(2) for 'localhost' port 1025" in Rails Contact Form

Hey there! Let's work through this email delivery error you're hitting in your Rails contact form. That port 1025 error happens because Rails is trying to connect to a local mail server (like Mailcatcher) that isn't running, and your SMTP config is conflicting with what you actually need—sending messages straight to a target email without fuss. Here's how to fix it:

This gem skips SMTP entirely and opens your test emails directly in a browser tab—perfect for development, no mail server required.

  • Add the gem to your Gemfile, restricted to the development group:
    gem 'letter_opener', group: :development
    
  • Run bundle install to install it.
  • Update your config/environments/development.rb to use this delivery method instead of SMTP:
    config.action_mailer.delivery_method = :letter_opener
    config.action_mailer.perform_deliveries = true
    

Now when you submit your contact form, a new browser tab will pop up showing your email—no more connection errors, and you can verify the content works as expected.

If you insist on sending actual emails to your target address during development, you'll need to set up valid SMTP credentials (not just remove the existing config). For example, using a Gmail account (note: you'll need an app-specific password if 2FA is enabled):

Update config/environments/development.rb with:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.gmail.com',
  port: 587,
  domain: 'your-app-domain.com',
  user_name: 'your-email@gmail.com',
  password: 'your-app-specific-password',
  authentication: 'plain',
  enable_starttls_auto: true
}

Warning: Sending real emails from development can trigger spam filters or expose sensitive credentials—stick to letter_opener unless you have a specific reason not to.

3. Double-Check Your Mailer Code

Make sure your contact mailer is set to send to your target email directly, even if you don't need a reply-to from the sender:

In your app/mailers/contact_mailer.rb:

class ContactMailer < ApplicationMailer
  # Set your target email here
  default to: 'your-target-email@example.com'

  def send_message(message)
    @message = message
    # Add a dummy from address if needed (most servers require this)
    mail(subject: "New Contact Message: #{@message.subject}", from: 'no-reply@your-app.com')
  end
end

That should resolve the connection error and get your contact form sending messages correctly in development.

内容的提问来源于stack exchange,提问作者Reynold Dmello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:02:59