ActionMailer将不会发送邮件?

5
我正在尝试在我的开发环境中设置一个简单的联系我们页面,用户可以使用我创建的表单发送询问。我已经设置好了ActiveMailer和Contact模型/控制器/视图,但似乎没有正常工作。有什么想法吗?我的日志显示电子邮件正在发送。
邮件发送程序(ActionMailer)
class ContactConfirmation < ActionMailer::Base
  default from: "from@example.com"

  def receipt(contact)
    @contact = contact

    mail to: 'myname@example.com',
      subject: contact.subject
  end
end

收据

<%= @contact.first_name %> <%= @contact.last_name %>

Writes:

<%= @contact.description %>

联系人控制器

class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    if @contact.submit_contact_info
      redirect_to users_path, notice: 'Submission successful. Somebody will get back to you shortly.'
    else
      render :new
    end
  end

  protected

  def contact_params
    params.require(:contact).permit(:first_name, :last_name, :email, :subject, :description)
  end
end

联系模型

class Contact < ActiveRecord::Base
  validates_presence_of :email
  validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
  validates_presence_of :subject
  validates_presence_of :description
  validates_presence_of :first_name
  validates_presence_of :last_name

  def submit_contact_info
    if save
      ContactConfirmation.receipt(self).deliver
      return true
    end
  end
end

联系表单在contacts/new.html.erb文件中呈现

<%= simple_form_for @contact do |f| %>
  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.input :email %>
  <%= f.input :subject %>
  <%= f.input :description, as: :text %>
  <%= f.submit 'Submit Contact Form' %>
<% end %>

在初始化文件夹中,我有一个smtp.rb文件:

if Rails.env.development?
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    address: "localhost",
    port: 1025
  }
end

在更改我的配置后,现在在ContactConfirmation.receipt(self).deliver上出现以下错误:

在ContactsController#create中出现Errno::ECONNREFUSED错误 连接被拒绝 - connect(2)

def submit_contact_info
    if save
      ContactConfirmation.receipt(self).deliver
      return true
    end
  end

开发环境下默认不发送邮件,你可以在配置文件中添加以下代码:config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliveries = true,然后重启应用程序并再次检查。 - zrl3dx
你是否已经设置好了SMTP详细信息? - Richard Peck
@oddone:所以你可以看到,你的设置有误,你的邮件服务器是否在1025端口上工作?默认值(至少在postfix中)是25。你能否粘贴telnet localhost 1025的输出? - zrl3dx
嗯,我认为邮件捕获测试可能在1025端口上。我需要更改设置吗?输出结果是:telnet: 连接到地址127.0.0.1时拒绝连接 尝试::1... telnet: 连接到地址::1时拒绝连接 尝试fe80::1... telnet: 连接到地址fe80::1时拒绝连接 telnet: 无法连接到远程主机 - John
@zrl3dx 当我现在再次尝试发送邮件时,我收到了这个消息 ==> SMTP:从'from@example.com'(296字节)接收到的消息。这是否意味着它正常工作? - John
显示剩余5条评论
1个回答

5

让我们从头开始:

在开发模式下,默认情况下不会发送电子邮件并且不会引发传递错误,这就是为什么您收不到错误和电子邮件的原因。要更改此设置,请添加以下内容到您的配置文件 并重新启动服务器

config/environments/development.rb

config.action_mailer.raise_delivery_errors = true 
config.action_mailer.perform_deliveries = true

现在您应该会得到一些错误信息或者接收到一封电子邮件。根据您在评论中提到的信息,您收到了“连接拒绝”的错误,这意味着您的邮件代理没有运行。您正在使用mailcatcher(因此是1025端口),所以安装之后只需要简单地运行mailcatcher即可。现在您应该不会遇到任何错误,并且可以通过浏览localhost:1080来查看您的电子邮件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接