邮件 gem 升级到 Rails 5.0.6 后无法发送电子邮件

Mail gem not sending emails after upgrading to Rails 5.0.6

我将 Rails 应用程序从 4.2.5 升级到了 5.0.6。 Mail gem 现在不发送电子邮件了。我似乎没有收到错误。我将 Mail gem 更新到最新版本,但没有成功。我不确定还能尝试什么。

我是运行:

Rails 5.0.6

Ruby 2.3.0

邮件 2.7.0

controllers/send_email.rb

 class SendEmail < ApplicationController
    def initialize(to_address, from_address, email_pass, subject, body)

        begin
            options = {
                :address              => 'abc.prod.1234.secureserver.net',
                :port                 => '465',
                :domain               => 'mydomain.com',
                :user_name            => from_address,
                :password             => email_pass,
                :authentication       => :login,
                :ssl                  => true,
                :openssl_verify_mode  => 'none'
            }

            Mail.defaults do
                delivery_method :smtp, options
            end

            Mail.deliver do
                to to_address
                from from_address
                subject subject
                body body
            end

            puts("\nSent message. From: #{from_address} To: #{to_address} \nMessage body: \n#{body}")

            return true

        rescue Exception => e
            puts e.to_s
            return false
        end
    end
end

更新:

我也尝试用 Action Mailer 发送电子邮件。它说它已在控制台中发送,但从未送达。我对 Action Mailer 使用与 Mail gem.

类似的设置

config/environments/development.rb

  config.action_mailer.smtp_settings = {
    :address              => 'abc.prod.1234.secureserver.net',
    :port                 => '465',
    :domain               => 'mydomain.com',
    :user_name            => ENV['default_username'],
    :password             => ENV['default_password'],
    :authentication       => :login,
    :ssl                  => true,
    :openssl_verify_mode  => 'none'
}

我是 运行 来自控制台的这个:

EmailMailer.sample_email(UserEmail.first)

控制台输出:

   Rendering email_mailer/sample_email.html.erb within layouts/mailer
   Rendered email_mailer/sample_email.html.erb within layouts/mailer (0.1ms)
   Rendering email_mailer/sample_email.text.erb within layouts/mailer
   Rendered email_mailer/sample_email.text.erb within layouts/mailer (0.0ms)
   EmailMailer#sample_email: processed outbound mail in 9.4ms

解法:

我有一些代码可以在出现我需要查看的未知错误时发送电子邮件。当我更新 Rails 时,该代码陷入了一个非常快速地发送一堆电子邮件的循环中。这导致我的服务器提供商将电子邮件帐户标记为垃圾邮件帐户。我不确定为什么我的原始代码没有显示任何错误,但是当我 运行 EmailMailer.sample_email(UserEmail.first).deliver_now 它给了我一条错误消息,帮助我找到它。

好吧,这里是疯狂的猜测,但试试这个:

EmailMailer.sample_email(UserEmail.first).deliver_now

如果您的 Rails 版本低于 4.2.1,则没有必要 to call deliver on the mail object for it to be delivered. From that point on you can operate on the mail object before delivering it, which can be now using .deliver_now or later, using .deliver_later,与 ActiveJob 或其他队列库一起使用。