【问题标题】:Rails 3 mailer not working and not logging any errorsRails 3 邮件程序不工作并且没有记录任何错误
【发布时间】:2012-03-20 07:14:13
【问题描述】:

我尝试了各种配置,但仍然无法在我的开发环境中从 rails 发送电子邮件。

我安装了 mailutils 以从命令行尝试此操作,它成功了,我收到了电子邮件(当然是垃圾邮件):echo test | mail -s Subject user@example.com

这是我的配置:

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true # still no logs about emails

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true # I can't believe I have to add this option. Does it even exist? I found it on google.
config.action_mailer.smtp_settings = {
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "gmail.com",
  :authentication => :login,
  :user_name => "some_user@gmail.com",
  :password => "abc123",
}

这是邮件中的代码:

class UserMailer < ActionMailer::Base
  default :from => "root@ubuntu"

  def test_email
    Rails.logger.debug 'test_email'
    mail(:to => 'user@example.com', :subject => "testing rails")
  end
end

控制器:

class PagesController < ApplicationController
  def home
    UserMailer.test_email
  end
end

开发日志:

[2012-03-01 18:26:45.859] DEBUG  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] test_email
[2012-03-01 18:26:45.888]  INFO  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/]   Rendered user_mailer/test_email (1.6ms)
[2012-03-01 18:26:45.898]  INFO  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/]   Rendered pages/home.html.erb within layouts/application (1.1ms)
[2012-03-01 18:26:46.815]  INFO  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Completed 200 OK in 455ms (Views: 112.4ms)

我也尝试过使用控制台:

root@ubuntu:/srv/www/myapp# rails c
Loading development environment (Rails 3.2.1)
irb(main):001:0> UserMailer.test_email
=> #<Mail::Message:32110400, Multipart: false, Headers: <To: user@example.com>, <Subject: testing rails>, <Mime-Version: 1.0>, <Content-Type: text/html>>

【问题讨论】:

  • guides.rubyonrails.org/action_mailer_basics.html 有一个 GMail 配置示例。它使用'plain'作为身份验证属性,你试过了吗?
  • 是的,我试过了,但什么也没发生。没有任何日志,这是行不通的。请求中的其他所有内容均已正确记录
  • 调用邮件程序的代码是什么样的?
  • @FrederickCheung 我将控制器添加到我的帖子中

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


【解决方案1】:
  UserMailer.test_email

只创建一个Mail::Message 对象。要实际发送电子邮件,您需要这样做

  UserMailer.test_email.deliver

(或从 rails 4.2 deliver_now / deliver_later 开始)

【讨论】:

  • 好吧,这很尴尬..谢谢
  • 天哪!我花了3个小时,完全忘记了这个方法!谢谢
  • Rails 5 的注意事项:弃用警告:#deliver 已弃用,将在 Rails 5 中删除。使用 #deliver_now
【解决方案2】:

关于日志记录错误:

刚刚通过修补发现存在用于交付的 bang 方法对应物,这会引发异常。检查您是否只是拼错了您的用户名或密码,或者您只是有一些错误的设置,这非常有用。

UserMailer.test_email.deliver!

【讨论】:

    【解决方案3】:

    Rails 4.2 的更新答案是:

    UserMailer.test_email.deliver_now!
    

    感叹号是在有错误时引发异常。

    【讨论】:

    【解决方案4】:

    如果您怀疑这是您的设置,请尝试取出 :domain。不久前它对我有用。(Net::SMTPAuthenticationError in rails 3.1.0.rc5 when connecting to gmail

    但我在 mail 函数中没有看到 :body 选项。也许这就是问题所在。尝试从 rails 控制台发送它,看看会发生什么。 http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多