【问题标题】:Action Mailer Configuration for GmailGmail 的操作邮件程序配置
【发布时间】:2018-01-16 16:34:50
【问题描述】:

我正在尝试将使用 Gmail SMTP 的电子邮件递送添加到我的应用程序。我之前已经做过“不太安全的应用程序”的方法,但我不想在这个项目中使用这个选项。

我尝试查看 Google 的文档或一些 gem 以使其正常工作,但无济于事。每个人都只是发送一些代码(如下所示,这通常与我的相同)或告诉我尝试“不太安全的应用程序”。

我当前在 production.rb 上的 action mailer 配置是:

    config.action_mailer.perform_caching = false
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.default_url_options = { :host => ENV['DOMAIN_NAME'] }
    config.action_mailer.asset_host = ENV['DOMAIN_NAME']

    config.action_mailer.smtp_settings = {
      :address              => 'smtp.gmail.com',
      :port                 => 587,
      :authentication       => :plain,
      :user_name            => ENV['USERNAME'],
      :password             => ENV['PASSWORD'],
      :enable_starttls_auto => true
    }

有人说我需要“:domain => 'gmail.com'”,但使用“不太安全的应用程序”选项它可以工作,所以我的猜测是问题并不那么简单。此外,人们谈论将 'authentication: :plain' 更改为 :login。

另外,我意识到在官方 Rails 文档中它说:

注意:自 2014 年 7 月 15 日起,Google 增加了安全措施,现在阻止来自它认为不太安全的应用程序的尝试。您可以在此处更改您的 gmail 设置以允许尝试。如果您的 Gmail 帐户启用了 2 因素身份验证,那么您将需要设置应用程序密码并使用该密码而不是您的常规密码。或者,您可以使用另一个 ESP 发送电子邮件,方法是将上面的“smtp.gmail.com”替换为您的提供商的地址。

(来自http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail

但我不确定此解决方案是否仍需要启用“不太安全的应用程序”选项,这不是我需要的。 有没有人在不使用“不太安全的应用”的情况下解决了这个问题?

提前致谢!

【问题讨论】:

    标签: ruby-on-rails email gmail actionmailer


    【解决方案1】:

    好吧,过了一段时间我终于做到了。

    我要做的是:

    1) 对我的 gmail 帐户进行两步验证,您可以在此处启用:https://myaccount.google.com/security

    2) 在此处创建应用专用密码:https://support.google.com/accounts/answer/185833

    它是一个字符串,格式为 16 个小写字母。它们以 4 个一组的形式出现,但它们都在同一个字符串中。您所要做的就是在块内的密码字段中添加此应用密码。

        ...
        :user_name => 'my@gmail.com',
        :password  => 'abcdefghijklmnop',
        ...
    

    所有其他设置均未更改。

    【讨论】:

      【解决方案2】:
      config.read_encrypted_secrets = true
      config.action_mailer.default_url_options = { :host => 
      "domain.com" }
      config.action_mailer.perform_deliveries = true
      config.action_mailer.delivery_method = :smtp
      
      config.action_mailer.smtp_settings = {
       :address => "smtp.gmail.com",
       :port => 587,
       :user_name => "username",
       :password => "password",
       :enable_starttls_auto => true
      }
      

      试试这个配置。

      【讨论】:

        猜你喜欢
        • 2015-01-30
        • 2012-04-22
        • 1970-01-01
        • 2013-12-10
        • 2013-08-10
        • 2019-07-19
        • 2013-01-10
        • 2014-09-14
        • 2014-07-21
        相关资源
        最近更新 更多