为什么我的Rails 2 ActionMailer会间歇性地失败?

时间:2019-03-06 17:58:53

标签: ruby-on-rails actionmailer ruby-on-rails-2

我相对缺乏经验,试图维护使用Rails 2.3.X和Ruby 1.8.7构建的Web应用程序。使用该应用程序创建帐户的用户应该会收到带有“激活”链接的自动电子邮件,这是他们确认自己实际上有权访问要注册的电子邮件地址的方式。

我发现我的一些(但不是全部)用户正在收到此激活电子邮件。如果他们的收件箱中没有收到电子邮件,则该电子邮件也不会显示在垃圾邮件文件夹中。

我怀疑该问题与电子邮件的格式有关;也许某些电子邮件提供商不喜欢这种格式并自动对其进行过滤。

我不确定需要提供什么信息来调查原因,但是这里有一些我认为有意义的地方:

网络表单(用户可以请求新的激活链接)

<h3>Resend activation link?</h3>
<form action="/send_activation" method="post">
    <input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>" />
    <p class="field">
    <label for="user_email">Email Address</label>
    <input class="login" id="user_email" name="user[email]" size="45" type="text" />
    </p>
    <div class="clear"></div>  
    <input name="commit" type="submit" value="submit" />
</form>

sessions_controller.rb

def send_activation
  return unless request.post?
  @title = "Send Activation Link"
  @user = User.find_by_email(params[:user][:email])
  if @user && @user.activated_at.blank?
      UserMailer.deliver_signup_notification(@user)
      redirect_to login_url
      flash[:notice] = "Activation link sent." 
  else
      flash[:notice] = "Could not find your email address. Try another." 
  end
end

user_mailer.rb

class UserMailer < ActionMailer::Base

def activation(user)
setup_email(user)
@subject    += 'Your account has been activated!'
@body[:url]  = "http://MYWEBSITE/"
end

integral_mailer.rb

module IntegralMailer
  def perform_delivery_integral_mailer(mail)
    destinations = mail.destinations
    mail.ready_to_send

    helo = smtp_settings[:helo] || "localhost.localdomain"

    ActionMailer::Base::INTEGRAL_MAILER_SERVER.send_mail(helo, mail.from, destinations, mail.encoded)
  end
end

integral_mailer_server.rb

require 'net/smtp'
require 'logger'
require 'fileutils'
require 'mx_lookup'

class IntegralMailerServer
  include FileUtils

  def self.reloadable?; false; end

  def initialize(opts={})
    @times_to_try_busy_host = opts[:times_to_try_busy_host] || 2
    initialize_log( (opts[:age_log_file] || 'daily'), opts[:log_path] )
  end

  def send_mail(helo_domain, from_email, destination_emails, data)
    t = Thread.new do
      destination_emails.each do |dest_email|

        mx_records = MXLookup.for_email(dest_email)

        mx_records.each do |mx_record|
          begin
            do_delivery(mx_record[:host], helo_domain, data, from_email, dest_email) and break
          rescue Net::SMTPFatalError => e
            log_fatal_delivery(dest_email, mx_record[:host], e.message) and break
          rescue Net::SMTPServerBusy => e
            log_delayed_delivery(dest_email, mx_record[:host], e.message)
            # there has to be a better method to use for this
            unless (mx_records.find_all { |r| r == mx_record }).size >= @times_to_try_busy_host
              mx_records << mx_record
            else
              log_fatal_delivery(dest_email, mx_record[:host], e.message)
            end
          rescue => e
            if mx_records.last == mx_record
              log_fatal_delivery(dest_email, mx_record[:host], e.message)
            else
              log_delayed_delivery(dest_email, mx_record[:host], e.message)
            end
          end
        end # mx_records.each

      end # destination_emails.each
    end # Thread.new 
  end # send_mail

  private

  def do_delivery(host, helo, data, from_email, dest_email)
    Net::SMTP.start(host, 25, helo) do |smtp|
      smtp.send_message data, from_email, dest_email
      log_successful_delivery(dest_email, host)
    end; true
  end

  def log_delayed_delivery(email, host, reason)
    msg =  "Message NOT sent to #{email} via #{host}\n  --> #{reason.strip}\n" +
           "  --> However, I'm not giving up just yet."
    @log.warn(msg); msg
  end

  def log_fatal_delivery(email, host, reason)
    msg = "Message NOT sent to #{email} via #{host}\n  --> #{reason.strip}\n" +
          "  --> I'm giving up delivery of this message."
    @log.fatal(msg); msg
  end

  def log_successful_delivery(email, host)
    msg = "Message sent to #{email} via #{host}"
    @log.info(msg); msg
  end

  def initialize_log(age_log_file='daily', log_path=nil)
    if log_path
      @log = Logger.new(log_path, age_log_file)
    else
      if defined?(RAILS_ROOT)
        @log = Logger.new("#{RAILS_ROOT}/log/integral_mailer.log", age_log_file) rescue Logger.new(STDOUT)
      else
        @log = Logger.new(STDOUT)
      end
    end
    @log.info "Initializing"
  end
end

1 个答案:

答案 0 :(得分:1)

最初,我将确认每次都发送电子邮件。您是否有显示发送成功的日志副本,被用户报告为失败?

因此,将某种形式的成功计数在发送命令到变量之后,也将电子邮件抄送给您自己/虚拟帐户以进行检查。为了隔离收据问题并进行追溯检查。

将过程分为几个步骤,您可以确认而不是发送电子邮件,也可以向服务器发送电子邮件,然后将文件写入服务器并检查其内容和格式。

您需要成功的例子和失败的例子,测试电子邮件充其量是困难的,除非您拥有“丢失的”电子邮件的副本。

我已经成功地将ActionMailer与外部分配代理一起使用,考虑迁移到此吗?设置代理后,它更简单,更灵活。

您是否也记录了电子邮件的内容?