如何使用 Gmail 从 Rails 发送电子邮件?
How do I send an email from Rails using Gmail?
我正在使用 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"
知道可能出了什么问题,或者我该如何进一步解决这个问题?
我相信 Rails 5,正确的语法是
UserNotifier.send_notification(user_notification, price).deliver_now
...并使用完整的电子邮件作为用户名。
我正在使用 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"
知道可能出了什么问题,或者我该如何进一步解决这个问题?
我相信 Rails 5,正确的语法是
UserNotifier.send_notification(user_notification, price).deliver_now
...并使用完整的电子邮件作为用户名。