【问题标题】:how to raise/rescue from ActionMailer deliver method如何从 ActionMailer 传递方法中引发/救援
【发布时间】:2010-08-04 04:57:23
【问题描述】:

我正在使用以下方法发送电子邮件:

class Communicate < ActionMailer::Base
  def message(sub,msg,people)
    subject    sub
    bcc        people
    from       'my_email.s@gmail.com'
    sent_on    Time.now

    body       :greeting => msg
  end
end

bcc 包含 4 或 5 个电子邮件地址。

在我的测试过程中,我注意到了两件事:

  • 即使其中一封电子邮件不是真正的电子邮件(例如fake_email_no_domain),它也不会向任何收件人发送电子邮件
  • 如果密件抄送列表有错误的电子邮件地址(例如nonexistent@gmail.com),它仍会向其他收件人发送电子邮件,但仍会向日志中抛出错误。

在这两种情况下,引发的错误是:

Redirected to http://localhost:3000/
Completed in 2601ms (DB: 1) | 302 Found [http://localhost/notifications]

    [2010-08-04 00:49:00] ERROR Errno::ECONNRESET: Connection reset by peer
        /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
        /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
        /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

问题:

  • 有什么方法可以捕捉到这个错误吗?我正在向用户显示flash[:notice],我想提一下发生了一些不好的事情
  • 如果我的密件抄送列表有 5 封电子邮件,但实际上只发送了 4 封电子邮件,因为第 5 封电子邮件不存在,那么在完成所有操作之后,我可以得到实际发送的电子邮件数量吗?我想在我的flash[:notice] 中显示该号码。我可以通过在迭代中实际调用传递方法而不是批量发送来获得这个数字,但如果没有发送一封电子邮件,我仍然不想增加计数。

【问题讨论】:

    标签: ruby-on-rails actionmailer


    【解决方案1】:

    config/environments/development.rb 中添加或取消注释以下行并重新启动您的服务器。

    # Don't care if the mailer can't send
    config.action_mailer.raise_delivery_errors = false
    

    我假设您在config/environments/production.rb 中检查developmentproduction 添加行

    【讨论】:

    • 我在 development.rb 中有那行,但删除它没有效果。仍然抛出同样的错误。
    • 对不起是我的错误Add or uncomment 那行不要删除也不要忘记重启你的服务器。
    • 我已经添加了它config.action_mailer.raise_delivery_errors = false,但现在它甚至不会向之前抛出的日志抛出错误......
    【解决方案2】:

    如果我正在编写代码以在控制器中发送电子邮件,我可以像这样编写来处理我的应用程序中的救援 我在控制器中使用这样的东西:

    if @user.save
      begin
      UserMailer.welcome_email(@user).deliver
      flash[:success] = "#{@user.name} created"
      rescue Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
      flash[:success] = "User #{@user.name} creating Problems sending mail"
      end
      redirect_to home_index_path
    end
    

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多