【问题标题】:Unable to send mail from Ruby on Rails 4.2 task无法从 Ruby on Rails 4.2 任务发送邮件
【发布时间】:2015-09-23 15:26:13
【问题描述】:

我可以从 rails 控制台发送邮件,但不会从 rake 任务发送相同的邮件

这是我的environments/development.rb

  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { host: 'localhost:3000' }
  config.action_mailer.smtp_settings = {
    address: 'my.mail.server',
    port: 465,
    user_name: "my-account",
    password: "my-password",
    athentication: :plain,
    ssl: true
  }

邮寄者:

class ScriptAlertMailer < ApplicationMailer

  def alert_email(to)
    mail(to: to, subject: 'This is an alert').deliver
  end  
end

任务:

task :inspect_js => :environment do
  puts "running inspect_js"
  ScriptAlertMailer.alert_email "mymail@domain.com"
end

邮件正在rails console 中发送。无论是在开发环境还是生产环境中加载:

ScriptAlertMailer.alert_email "an-account@domain.com"

在 log/[environment].log 中输出呈现的邮件视图,然后发送。

但是当我使用rake inspect_js 执行此操作时,不会将任何内容附加到日志并且不会发送

我试过 deliver, deliver!, deliver_later and deliver_now 没有运气

有什么想法吗?

【问题讨论】:

  • 我从未在邮件中看到deliver。我会尝试ScriptAlertMailer.alert_email("mymail@domain.com").deliver_now,将邮件从邮件中取出。可能是错的,但你需要做 logger.info "running inspect_js" 而不是 puts

标签: ruby-on-rails email ruby-on-rails-4 rake


【解决方案1】:

不应在邮件程序本身内调用deliver 方法。在 rails 控制台中,您会看到 Mail::Message 对象,但我认为没有发送任何实际的电子邮件。下面的更改应该可以让您的任务发挥作用。

在邮件中,更改:

mail(to: to, subject: 'This is an alert').deliver

收件人:

mail(to: to, subject: 'This is an alert')

在rake任务中,改变:

ScriptAlertMailer.alert_email "mymail@domain.com"

收件人:

ScriptAlertMailer.alert_email("mymail@domain.com").deliver_now

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2012-05-04
    • 2016-01-30
    • 2013-06-08
    • 1970-01-01
    • 2010-09-19
    相关资源
    最近更新 更多