【发布时间】: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