提交表单后未发送电子邮件

Email not being sent after submitting form

我正在尝试完成一份申请表,一旦点击提交按钮,该申请表就会通过电子邮件发送给雇主。几天前我让它工作了,但我对我的代码做了很多小改动,现在表单无法发送。不过,我可以从控制台内部发送电子邮件。我看了很多不同的帖子和教程,但我无法弄明白。

控制器

    def new
       @form = Form.new
    end 

    def create
        @form = Form.new(params)
      if @form.deliver
        redirect_to careers_path, :notice => 'Thank you for applying.'
      else
        redirect_to new_form_path, :notice => 'Message could not be sent. Please try again.'
      end
   end

型号

class Form < MailForm::Base
  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :salary, :validate => true
  attribute :location, :validate => true
  attribute :message, :validate => true

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "Applicant",
      :to => "123@email.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end

查看

<%= simple_form_for new_form_path, method: :post do |f| %>
      <div>
          <%= f.label :name %><br/>
          <%= f.text_field  :name, required: true, class: "contact-form-text-area" %></br>
          </br>
          <%= f.label :email %><br/>
          <%= f.text_field :email, required: true, class: "contact-form-text-area" %></br>
          </br>
          <%= f.label :location %><br/>
          <%= f.text_field :location, required: true, class: "contact-form-text-area" %></br>
          </br>
          <%= f.label :salary %><br/>
          <%= f.text_field :salary, required: true, class: "contact-form-text-area" %></br>
          </br>
        <%= f.label :message %><br/>
        <%= f.text_area :message, rows: 8, cols: 40, required: true, class: "contact-form-text-area",
                          placeholder: "Inlcude extra details"%></br>

        <br/>
        <div align="center">
          <%= f.submit 'Submit', class: 'btn btn-primary' %>
        </div>
      </div>
    <% end %>

development.rb

config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors= true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'gmail.com',
    user_name:            ENV["GMAIL_EMAIL"],
    password:             ENV["GMAIL_PASSWORD"],
    authentication:       'plain',
    enable_starttls_auto: true  }

路线

resources :forms, only: [:new, :create]
    post '/forms/new', to: 'forms#create'

日志

Started POST "/forms/new" for ::1 at 2020-04-10 18:46:10 -0700
Processing by FormsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hwQ5eKNZptSK0w8E4C+UdvS+7TuUg5esYXyp8FMt/008s+RAiRrt/0oXpKVwGbJ5JbOpjtqbAqxTO/aoDWXZMA==", "/forms/new"=>{"name"=>"person", "email"=>"123@email.com", "location"=>"somewhere", "salary"=>"$$$", "message"=>"testing"}, "commit"=>"Submit"}
Redirected to http://localhost:3000/forms/new
Name can't be blank
Email can't be blank
Salary can't be blank
Location can't be blank
Message can't be blank
Completed 302 Found in 5ms (ActiveRecord: 0.0ms)


Started GET "/forms/new" for ::1 at 2020-04-10 18:46:10 -0700
Processing by FormsController#new as HTML
  Rendering forms/new.html.erb within layouts/application
  Rendered forms/new.html.erb within layouts/application (4.0ms)
Completed 200 OK in 197ms (Views: 194.7ms | ActiveRecord: 0.0ms)

错误是@form 对象中没有设置任何内容。

根据日志,正在传入参数...

Parameters: {"utf8"=>"✓", "authenticity_token"=>"token", 
"/forms/new"=>{"name"=>"person", "email"=>"123@email.com",
"location"=>"somewhere", "salary"=>"$$$", "message"=>"testing"}, 
"commit"=>"Submit"}

但没有被设置到对象中。所以,我敢打赌这两行

@form = Form.new(params[:form])
@form.request = request

应替换为

@form = Form.new(params)

这应该设置@form 对象的所有属性。