邮件gem在升级到Rails 5.0.6后不发送电子邮件

时间:2018-02-17 23:56:44

标签: ruby-on-rails ruby actionmailer mail-gem

我将我的Rails应用从4.2.5升级到5.0.6。现在,Mail gem尚未发送电子邮件。我似乎没有得到错误。我将Mail gem更新为最新版本但没有运气。我不知道还有什么可以尝试的。

我正在跑步:

Rails 5.0.6

Ruby 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时,它给了我一条错误消息,帮助我跟踪它。

1 个答案:

答案 0 :(得分:3)

嗯,在这里疯狂猜测,但试试这个:

import random
import os
directory = "/Users/Iamawesome/Desktop/test/"
random_image = random.choice(os.listdir(directory))
print directory + random_image

如果您的Rails版本低于4.2.1,则邮件对象上没有必要to call deliver才能传递它。从那时起,您可以在发送之前对邮件对象进行操作,can be now using  EmailMailer.sample_email(UserEmail.first).deliver_now or later, using .deliver_now,与ActiveJob或其他排队库一起使用。