【问题标题】:Mailer stopped working after switching emailsMailer 切换电子邮件后停止工作
【发布时间】:2015-01-11 23:28:06
【问题描述】:

几周前,我创建了一个邮件程序对象,用于在创建新“事物”时发送电子邮件。它工作顺利,但我随后注释掉了相关行并且有一段时间没有处理它。今天我又试了一次,没有任何反应。我发现我使用的电子邮件帐户由于某种原因已断开连接,因此我尝试使用另一个电子邮件帐户。依然没有。有没有人猜测它为什么没有做任何事情,或者我如何调试它?我确定用户名/密码/域是准确的。

config/environments/development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.gmail.com',
  port: 587,
  domain: 'gmail.com',
  user_name: 'username',
  password: 'password',
  authentication: 'plain',
  enable_starttls_auto: true
}

app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: 'username@gmail.com'

  def notify(user)
    @user = user
    mail(to: @user.email,subject: "Notification")
  end
end

app/views/user_mailer/notify.html.erb

<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Heading</h1>
    <p>First paragraph</p>
    <p>Second paragraph</p>
  </body>
</html>

app/controllers/users_controller.rb

def create
  #...
  UserMailer.notify(@user).deliver
end

【问题讨论】:

  • 您是否有测试/规范来验证电子邮件是否已发送且内容/属性是否符合预期?这是验证邮件功能是否正常工作的更好方法;比尝试手动触发电子邮件更好。可能是电子邮件仍在发送,但本地的一些 smtp 问题正在抑制它。另外,邮件功能是否适用于生产环境?
  • 也许您必须在电子邮件配置中启用对您应用程序的访问权限,您为什么不试试这个? stackoverflow.com/a/26374624/3851249
  • 为delivery添加爆炸符号,并告诉我们,错误是什么?并且邮件不会进入垃圾箱对吧..?
  • @PrakashMurthy 我做了一个按钮,当我按下它时应该会发送一封电子邮件。我还没有部署我的应用,所以我不知道它是否可以在生产环境中运行。
  • @DemiMagus 我启用了对不太安全的应用程序的访问,但它仍然没有做任何事情。

标签: ruby-on-rails ruby ruby-on-rails-4 smtp mailer


【解决方案1】:

试试这个:

config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host:'localhost', port: '3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
  address: 'smtp.gmail.com',
  port: 587,
  domain: 'localhost:3000',
  user_name: 'username@gmail.com',
  password: 'password',
  authentication: 'plain',
  enable_starttls_auto: true
}

【讨论】:

  • 它有效,谢谢!但是我不明白在那之前我的代码是如何工作的,如果我错误地填写了域和用户名字段......
【解决方案2】:

如果您在开发环境中进行测试,请添加以下行并检查
config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true

【讨论】:

  • 我可以在环境/development.rb 中的任何位置添加这些行吗?
  • 是的,将这些行添加到您有权访问(或访问配置的范围)变量的位置。
  • 我应该检查什么?
  • 触发邮件并检查它是否被发送。您也可以从 Rails 控制台触发邮件方法,如 UserMailer.notify(@user)。
  • 好吧,现在我真的很困惑。我在控制台中尝试了它,它返回“用户名和密码不被接受。了解更多信息”......然后句子中断了。我绝对,绝对写了正确的用户名和密码。您知道“在...了解更多”之后会发生什么吗?
猜你喜欢
  • 2017-06-15
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多