通过Gmail从Ruby发送电子邮件

时间:2015-12-12 09:23:36

标签: ruby-on-rails ruby email

此代码无法发送电子邮件(根本没有任何反应 - 也没有错误)。我做错了什么?

/app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: "sample@gmail.com"

  def email_test()
    @user = 'recipient@gmail.com'
    mail(to: @user, subject: 'Welcome to My Awesome Site')
  end
end

/config/development.rb:

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply@example.com'}

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "gmail.com",
  :authentication => :login,
  :user_name => "sample@gmail.com",
  :password => "password123",
}

我调用文件夹/ app / mailers /中的代码。我在这里跑:

rails runner UserMailer.email_test

是因为我在developer.rb中设置了gmail smtp设置,但是在命令行中运行此代码?

我已经开启了访问不太安全的应用程序的权限。在我的Gmail帐户中。如此困惑,请指教谢谢!

1 个答案:

答案 0 :(得分:1)

对于Rails 3:

rails runner "UserMailer.email_test.deliver"

对于Rails 4:

bin/rails runner "UserMailer.email_test.deliver_now"