【问题标题】:How do I send an email from Rails using Gmail?如何使用 Gmail 从 Rails 发送电子邮件?
【发布时间】:2017-09-15 04:39:25
【问题描述】:

我正在使用 Rails 5 并尝试使用 Gmail 作为中继从我的开发机器发送一些电子邮件。我有这个邮件文件,app/mailers/user_notifier.rb

class UserNotifier < ActionMailer::Base
  default from: RAILS_FROM_EMAIL

  # send notification email to user about the price
  def send_notification(user_notification, crypto_price)
    puts "user notification: #{user_notification.id}"
    @user = user_notification.user
    @crypto_price = crypto_price
    threshhold = user_notification.buy ? 'above' : 'below'
    puts "user: #{@user.email} currency: #{@user.currency}"
    mail( :to => @user.email,
    :subject => sprintf(Constants::USER_NOTIFICATION_SUBJECT, crypto_price.crypto_currency.name, threshhold, PriceHelper.format_price(user_notification.price, @user.currency) )  )
  end

然后我像这样从 Sidekiq 工作人员发送电子邮件

UserNotifier.send_notification(user_notification, price).deliver

虽然我在日志中没有看到任何错误,但电子邮件从未送达(我已检查我的垃圾邮件文件夹以验证这一点)。下面是我的 config/environments/development.rb 文件。

  # ActionMailer Config
  config.action_mailer.smtp_settings = {
     address:              'smtp.gmail.com',
     port:                 587,
     domain:               'mybox.devbox.com',
     user_name:            'myusertest1',
     password:             'myuser99999',
     authentication:       'plain',
     enable_starttls_auto: true
  }
  config.action_mailer.delivery_method = :smtp
  # change to true to allow email to be sent during development
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default :charset => "utf-8"

有什么想法可能出了什么问题,或者我可以如何进一步解决这个问题?

【问题讨论】:

    标签: email smtp gmail ruby-on-rails-5 mailer


    【解决方案1】:

    我相信 Rails 5,正确的语法应该是

    UserNotifier.send_notification(user_notification, price).deliver_now

    ...并使用完整的电子邮件作为用户名。

    【讨论】:

    • 试一试,但什么也没做。仍然没有收到任何电子邮件。
    • 添加了一个编辑。我使用 gmail 作为中继,我使用完整的电子邮件作为用户名。值得一试。
    • 谢谢,我没想到。我仍然没有收到电子邮件(即使是垃圾邮件)——有没有办法可以追踪正在发生的事情?日志文件似乎没有透露太多信息。
    • 尝试在deliver_now(例如UserMailer.test_email.deliver_now!)的末尾添加一个砰('!'),如果某些事情不太正确,这应该会给你一个运行时错误。我的来源:stackoverflow.com/a/16410194
    • @Dave 所以我构建了一个新应用程序和一个测试邮件。我将更改用户名和密码的设置复制到我的帐户(我只尝试了用户名,而不是完整的电子邮件)。我打开了 rails 控制台,并使用 Deliver_now 直接调用了邮件程序!它通过了。能否在控制台中测试并返回结果?
    猜你喜欢
    • 2011-06-28
    • 2013-10-17
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多