没有收到ActionMailer,没有错误

时间:2018-10-18 20:13:53

标签: ruby-on-rails

我目前正尝试使用以下邮件发送电子邮件:

class ContactMailer < ApplicationMailer
  default from: ENV['GMAIL_USERNAME']

  def contact_email(params)
    @email = params['email']

    mail(to: ENV['GMAIL_USERNAME'], from: @email, subject: "Contact email from #{@email}")
  end
end

然后我尝试使用以下控制器发送此电子邮件:

class MailerController < ApplicationController
  def contact
    ContactMailer.contact_email(email_params).deliver_now

    render json: { message: 'Email was sent!', email: params['email'] }, status: :ok
  end

  private

  def email_params
    params.permit(
      :name,
      :email,
      :phone,
      :services,
      :message
    )
  end
end

当我发布到该路线时,ENV [“ GMAIL_USERNAME”]存在且有效,我的email_params将被填写,并且我将收到该电子邮件的布局。

但是,当我检查接收电子邮件帐户时,我没有新的电子邮件。

这是./config/environments/development.rb中邮件程序的配置:

# action mailer
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :user_name            => ENV['GMAIL_USERNAME'],
  :password             => ENV['GMAIL_PASSWORD'],
  :authentication       => "plain",
  :enable_starttls_auto => true
}
config.action_mailer.perform_deliveries = true

有什么我想念的吗?

预先感谢

1 个答案:

答案 0 :(得分:0)

错误并非与ActionMailer有关,而是与我的请求有关,因为我在请求正文中提供了一封空电子邮件。 Rails会抛出错误,并且只有在将config.action_mailer.raise_delivery_errors = true添加到配置时,我才看到它。