【问题标题】:why is devise not sending email via gmail smtp?为什么设计不通过 gmail smtp 发送电子邮件?
【发布时间】:2016-12-29 09:28:46
【问题描述】:

我正在使用设备进行身份验证。它提供了一个忘记密码的链接。当我发送电子邮件时,电子邮件不会发送。以下是我使用的设置。你能告诉我为什么gmail不发送电子邮件吗?我还打开了“允许不太安全的应用程序发送电子邮件”,并且我还在 gmail 设置中启用了 imap。

application.rb 有以下设置。

ActionMailer::Base.smtp_settings = {


  :address => 'smtp.gmail.com',
  :domain => 'mail.google.com',
  :port => 587,
  :user_name => 'validemail@gmail.com',
  :password => 'validpassword',
  :authentication => 'login',
  :enable_starttls_auto => true


}

development.rb 有

  config.action_mailer.default_url_options = { host: '127.0.0.1'}


  config.action_mailer.delivery_method = :smtp

发送电子邮件后,我在控制台中收到以下文本。

Devise::Mailer#reset_password_instructions: processed outbound mail in 215.2ms
Sent mail to validemail@gmail.com (1097.6ms)
Date: Thu, 29 Dec 2016 09:50:41 +0000
From: please-change-me-at-config-initializers-devise@example.com
Reply-To: please-change-me-at-config-initializers-devise@example.com
To: validemail@gmail.com
Message-ID: <5864dc7161acb_173921a07788707d@kofhearts-rubyonrails-3267120.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Hello validemail@gmail.com!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><a href="http://127.0.0.1/users/password/edit?reset_password_token=WQxYad91mPghMxaurYA5">Change my password</a></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>

Redirected to https://rubyonrails-kofhearts.c9users.io/users/sign_in
Completed 302 Found in 1965ms (ActiveRecord: 14.7ms)

更新:

我只是在学习本教程。

https://www.youtube.com/watch?v=ZEk0Jp2dThc

发送电子邮件不适用于此视频中指定的设置。

【问题讨论】:

  • 我知道这不是您问题的答案,但我放弃了通过 gmail 使用 smtp。我现在使用sendinblue,他们有一个 smpt 服务,每天大约 300 封电子邮件是免费的。也是一个很好的红宝石,所以它很容易使用。

标签: ruby-on-rails devise


【解决方案1】:

在我的帐户设置中启用不太安全的应用对我有用。

Google 帐户设置 > 登录 Google > 向下滚动到底部

开启允许不太安全的应用。再次尝试发送电子邮件。

【讨论】:

    【解决方案2】:

    应该可以的。 也许你也可以打开config.action_mailer.raise_delivery_errors = trueconfig/environments/development.rb。 更容易调试。

    也许您忘记在config/environments/development.rb 中设置config.action_mailer.perform_deliveries = true

    【讨论】:

    • 谢谢,但即使添加了 perform_deliveries 设置和 raise_delivery_errors 标志,电子邮件也不会发送。我已经用我点击“向我发送重置密码说明”后打印的控制台消息更新了我上面的帖子,该消息应该向提供的电子邮件发送电子邮件。
    【解决方案3】:

    你试过了吗?

    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
    ActionMailer::Base.smtp_settings = {  
      :address              => "smtp.gmail.com",  
      :port                 => 587,  
      :domain               => "gmail.com",  
      :user_name            => "myinfo@gmail.com",  
      :password             => "secret",  
      :authentication       => "plain"
      # :enable_starttls_auto => true # I don't have this, but it should work anyway 
    } 
    

    它已发送,可能由于垃圾邮件过滤器而您没有收到它,首先要检查:

    class UserMailer < ActionMailer::Base
      default :from => "myinfo@gmail.com"
      # ...
    end
    

    您还可以在您的 gmail 中查看您的 Forwarding and POP/IMAP..

    在您的development.rb 中包含这些代码行

    config.action_mailer.default_url_options = {host: 'your server' } # ex. localhost:3000
    config.action_mailer.raise_delivery_errors = true # to raise error if smtp has error on setup
    config.action_mailer.default :charset => "utf-8"
    

    【讨论】:

    • 感谢我添加了,但也没有用。我还检查了垃圾邮件,邮件不存在。
    • @user734861 好的.. 现在你必须启用“转发和 POP/IMAP”..
    • 我已启用 imap。我也应该启用pop吗?
    【解决方案4】:
    1. 如果您使用的是 Gmail,您应该激活权限 外部应用通过 SMTP 发送电子邮件。
    2. 在开发模式下 Rails 不发送电子邮件,您应该在生产环境中运行 Rails rails s -e production 或打开 config.action_mailer.raise_delivery_errors = true
    3. 不要忘记使用 mail().deliver 或 Rails 5+ mail().delivery_now

    你看,这是我的 SMTP 配置

      config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
        :address              => ENV['mailer_address'],
        :port                 => ENV['mailer_port'],
        :user_name            => ENV['mailer_username'],
        :password             => ENV['mailer_password'],
        :authentication       => "plain",
        :enable_starttls_auto => true
      }
    

    这是我的 Class Mailer

    def send_email
     mail(to: "example@email.com", subject: 'not reply this message').deliver
    end
    

    我从我的控制器调用该函数。如果您遇到任何错误,您应该打印它以检查问题所在。

    begin
      myMailer.send_email
    rescue  Exception => e
      flash[:error] = "Imposible sent email, #{e.inspect}"
    end
    

    【讨论】:

    • 感谢 jeff,但我启用了 pop、imap 并启用了不太安全的应用程序来访问 gmail 权限。仍然使用上述设置,电子邮件未送达。
    • 您能否创建一个邮件程序来测试它是否正常工作?您使用的是哪个 Rails 版本?
    猜你喜欢
    • 2015-10-09
    • 2017-05-12
    • 2020-02-21
    • 1970-01-01
    • 2014-01-04
    • 2016-01-16
    • 2011-08-11
    • 2022-08-14
    • 2014-10-19
    相关资源
    最近更新 更多