我将我的 Rails 应用程序从 4.2.5 升级到 5.0.6。现在Mail gem 不会发送电子邮件。我似乎没有收到错误。我没有运气将 Mail gem 更新到最新版本。我不确定还能尝试什么。
我在跑步:
导轨 5.0.6
红宝石 2.3.0
邮件 2.7.0
控制器/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 类似的设置。
配置/环境/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
它时,它给了我一条错误消息,帮助我追踪它。