我有一个Rails应用程序,需要联系表格。 我正在使用mail_form gem,可以从Rails控制台发送邮件,但不能从表单发送邮件。 跟随了一些视频和教程,但似乎都没有解决这个问题。
控制器:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contacts_params)
@contact.request = request
if @contact.deliver
flash.now[:notice] = 'Thank you for your message. We will contact you soon!'
else
flash.now[:alert] = 'Cannot send message'
render :new
end
end
protected
def contacts_params
params.require(:contact).permit(:name, :email, :message)
end
end
contact.rb
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :file, :attachment => true
attribute :message
attribute :nickname, :captcha => true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "My Contact Form",
:to => "tcunha_lp@hotmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
服务器日志显示:
Started POST "/contacts" for 127.0.0.1 at 2019-02-19 11:40:45 +0000
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yLolqR/aOQ1T75HJr4f+DlGwbw6e67FwVoLuWNr/MIs2PdFbdx/626SUqTjw0xbb+9xmy4zg2mzZKYt9XXYNdA==", "contact"=>{"name"=>"", "email"=>"", "message"=>""}, "commit"=>"Send"}
Rendering contacts/new.html.erb within layouts/application
Rendered contacts/new.html.erb within layouts/application (4.6ms)
Rendered layouts/_navbar.html.erb (0.7ms)
Rendered layouts/_header.html.erb (53.4ms)
Rendered layouts/_navbar.html.erb (0.2ms)
Rendered layouts/_footer.html.erb (51.8ms)
Completed 200 OK in 767ms (Views: 761.6ms | ActiveRecord: 0.0ms)
env配置应该正常,因为我可以从控制台发送邮件。
有什么想法吗?