首页 > 解决方案 > Rails 5.2 ActionMailer 邮件不会发送。在服务器中看不到任何错误

问题描述

尝试使用 Gmail、Sendgrid 或其他任何东西通过 SMTP 发送志愿表格,但是我已经设置了 Action Mailer,我想当我查看电子邮件时我应该真的看到电子邮件通过了。我可以在下面看到此错误,但根本看不到任何错误或收到任何电子邮件。我已经尝试在我的创建操作中编写 puts,但我也没有看到这些,所以希望这是一个简单的修复,因为我遗漏了一些东西或者有些东西不遵循通常的 Rails 约定。

我正在使用导轨 5.2

我在服务器上看到的:

Started GET "/volunteer?utf8=%E2%9C%93&authenticity_token=AZ%2FAflVf%2BlqRUYm45Jo82wAMpq%2B%2BY%2F93piLbeRXdK5n%2FQIWhuaUL3Oe2%2FSwzR%2FCLvj%2FAKW%2BBgD8dPd8vJYRDBA%3D%3D&volunteer_message%5Bname%5D=test+name&volunteer_message%5Bemail%5D=email%40test.com&volunteer_message%5Bphone_number%5D=88888888&volunteer_message%5Bbody%5D=test+email&commit=Send" for 127.0.0.1 at 2018-10-10 17:29:42 +0100

Processing by PagesController#volunteer as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"AZ/AflVf+lqRUYm45Jo82wAMpq++Y/93piLbeRXdK5n/QIWhuaUL3Oe2/SwzR/CLvj/AKW+BgD8dPd8vJYRDBA==", "volunteer_message"=>{"name"=>"test name", "email"=>"email@test.com", "phone_number"=>"88888888", "body"=>"test email"}, "commit"=>"Send"}

好的,这是我的代码以及它的全部布局方式。

PagesController.rb 包含一个名为志愿者的页面,其中包含部分(称为 _new.html.erb),这是它自己的文件夹中名为志愿者消息的表单

意见/volunteer_messages/_new.html.erb

 <%= simple_form_for @volunteer_message, url: create_volunteer_message_url do |f| %>
  <%= @volunteer_message.errors.full_messages.join(', ') %>

  <%= f.input :name, placeholder: 'name' %>
  <%= f.input :email, placeholder: 'email' %>
  <%= f.input :phone_number, placeholder: 'phone number' %>
  <%= f.input :body, placeholder: 'body' %>
  <%= f.submit 'Send' %>
<% end %>

创建的路线

   # is this causing a problem do you think? 
   get '/volunteer' => 'pages#volunteer'

   # volunteer message routes
   get '/volunteer', to: 'volunteer_message#new', as: 'new_volunteer_message'
   post '/volunteer', to: 'volunteer_message#create', as: 'create_volunteer_message'

模型/volunteer_message.rb

class VolunteerMessage
    include ActiveModel::Model
    attr_accessor :name, :email, :phone_number, :body
#   validates :name, :email, :phone_number, :body, presence: true
end

mailers/volunteer_message_mailer.rb

class VolunteerMessageMailer < ApplicationMailer

      # Subject can be set in your I18n file at config/locales/en.yml
      # with the following lookup:
      #
      #   en.volunteer_message_mailer.volunteer.subject
      #
      def volunteer(volunteer_message)
            @body = volunteer_message.body
            @subject = "Volunteer Request sent from www.myapp.com"
        mail to: "myemail@example.com", from: volunteer_message.email
      end
    end

志愿者消息控制器.rb

class VolunteerMessagesController < ApplicationController

    def new
    @volunteer_message = VolunteerMessage.new
  end

    def create
    @volunteer_message = VolunteerMessage.new volunteer_message_params
    if @volunteer_message.valid?
      VolunteerMessageMailer.volunteer(@volunteer_message).deliver_now
      redirect_to new_volunteer_message_url
      flash.now[:notice] = "We have received your volunteer request and will be in touch soon!"
    else
      flash.now[:notice] = "There was an error sending your volunteer request. Please try again."
      render :new
    end
  end

    private

    def volunteer_message_params
    params.require(:volunteer_message).permit(:name, :email, :phone_number, :body)
  end
end

pages_controller.rb

class PagesController < ApplicationController
  def volunteer
    @lead = Lead.new
    @volunteer_message = VolunteerMessage.new
  end
end

发展.rb

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

    config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }


    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
            address: 'smtp.gmail.com',
            port: 587,
            domain: 'gmail.com',
            enable_starttls_auto: true,
            user_name: 'me@gmail.com',
            password: 'gmailapppassword',
            authentication: 'plain'
    }

不知道我还能在这里添加什么。我认为我什至不需要他 stmp_settings 来查看动作邮件程序实际上显示正在发送的电子邮件的模板吗?

另外我猜这与表单和路由有关,因为我添加了将消息放入志愿者消息控制器上的创建操作中,并且在我的服务器日志中看不到它们。

按提交时我没有收到任何错误。我确实注意到参数进入了 url,但就是这样。它停留在那里,刷新时停留在同一屏幕上。

例子:

http://localhost:3000/volunteer?utf8=%E2%9C%93&authenticity_token=AZ%2FAflVf%2BlqRUYm45Jo82wAMpq%2B%2BY%2F93piLbeRXdK5n%2FQIWhuaUL3Oe2%2FSwzR%2FCLvj%2FAKW%2BBgD8dPd8vJYRDBA%3D%3D&volunteer_message%5Bname%5D=test+name&volunteer_message%5Bemail%5D=email%40test.com&volunteer_message%5Bphone_number%5D=88888888&volunteer_message%5Bbody%5D=test+email&commit=Send

是图像中服务器文本中的线索,它显示了进入页面控制器的参数。我真的不想要那个。只希望部分存在,但要在志愿者消息控制器中执行创建操作的操作。

任何帮助将不胜感激。谢谢。

标签: rubyruby-on-rails-5actionmailermailchimptransactional-email

解决方案


推荐阅读