我正在尝试在我的开发环境中设置一个简单的联系我们页面,用户可以使用我创建的表单发送询问。我已经设置好了ActiveMailer和Contact模型/控制器/视图,但似乎没有正常工作。有什么想法吗?我的日志显示电子邮件正在发送。
邮件发送程序(ActionMailer)
邮件发送程序(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
,然后重启应用程序并再次检查。 - zrl3dxtelnet localhost 1025
的输出? - zrl3dx