如何使用Gmail从Rails发送电子邮件?

时间:2017-09-13 15:40:08

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

我正在使用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"

任何可能出现问题的想法或我如何进一步解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

UserNotifier.send_notification(user_notification, price).deliver_now

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