【问题标题】:ActionMailer will not send mail?ActionMailer 不会发送邮件?
【发布时间】:2014-01-12 12:45:43
【问题描述】:

我正在尝试在我的开发环境中设置一个简单的contact_us 页面,用户可以使用我创建的表单发送查询。我已经设置了 ActiveMailer 和联系人模型/控制器/视图,但它似乎无法正常工作。有任何想法吗?我的日志似乎显示正在发送邮件。

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 %>

在 Initializers 文件夹中,我有一个 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添加到您的配置中,重新启动并再次检查?
  • 您的 SMTP 详细信息设置完毕了吗?
  • @zrl3dx 现在出现错误:连接被拒绝 - connect(2) def submit_contact_info if save ContactConfirmation.receipt(self).deliver return true end end
  • @RichPeck 我将我的 smtp 设置添加到上述问题中
  • @oddone: 如您所见,您的设置有误,您的邮件服务器是否在端口 1025 上工作?默认(至少在后缀中)是 25。你能粘贴 telnet localhost 1025 的输出吗?

标签: ruby-on-rails email actionmailer


【解决方案1】:

那么让我们从头开始吧:

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

config/environments/development.rb

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

现在您应该得到一些错误或收到一封电子邮件。正如您在 cmets 中所写,您收到了 Connection refused 错误,因此这意味着您的邮件程序守护程序没有运行。您正在使用mailcatcher(因此是 1025 端口),因此安装后您应该通过简单的mailcatcher 运行它。现在您应该不会收到任何错误,并且您应该能够在浏览到 localhost:1080 后看到您的电子邮件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 2013-02-07
    • 2014-12-31
    • 2011-12-31
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多