簡體   English   中英

Rails 4、如何正確配置smtp設置(gmail)

[英]Rails 4, how to correctly configure smtp settings (gmail)

我正在嘗試在 Rails 4 中創建一個聯系表單。我在這里進行了一些挖掘,並且能夠使大部分內容正常工作。 (遵循@sethfri 在此處的工作,Rails 4 中的聯系表格郵件程序

現在我可以填寫我的表單並點擊發送。 在我的 rails 服務器中,它說郵件已出站到我的電子郵件地址,但我的 gmail 郵箱中沒有收到任何內容,所以我認為我的 smtp 設置不正確。 我的 smtp 設置是:

...配置/環境/development.rb

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "mydomain.net",
    :user_name => "mygmailusername@gmail.com",
    :password => "myGmailPassword",
    :authentication => "plain",
    :enable_starttls_auto => true
  } 

我還添加了.../config/initializers/smtp_settings.rb

ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "mydomain.net",
    :user_name => "gmailuser@gmail.com",
    :password => "gmailPassword",
    :authentication => "plain",
    :enable_starttls_auto => true
}

我錯過了什么/做錯了什么? 我嘗試了幾件事(將 default_url 更改為端口 1025,將 :port => "587" 更改為 :port => 587)但沒有成功。

謝謝您的幫助!

您必須正確設置域。 目前在代碼中發布了它的“mydomain.net”。 如果您想通過 gmail 發送,請將其更改為gmail.com

config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            'xyz@gmail.com',
  password:             'yourpassword',
  authentication:       :plain,
  enable_starttls_auto: true
}

如果您在使用 Gmail 發送電子郵件(常見於 Google Apps 帳戶)時遇到 Net::SMTPAuthenticationError 等錯誤,請訪問您的Gmail 設置並啟用安全性較低的應用程序以使應用程序正常工作。

經過幾個小時的搜索,如何讓它為我工作,我找到了一種讓它工作的方法。 對於我自己,我需要進行兩步驗證並使用 Gmail 應用程序密碼

當您啟用兩步驗證(也稱為兩步驗證)時,您可以為您的帳戶增加一層額外的安全保護。 您使用您知道的信息(您的密碼)和您擁有的信息(發送到您手機的代碼)登錄。

設置兩步驗證

  1. 轉至兩步驗證頁面。 您可能需要登錄您的 Google 帳戶。
  2. 在右側的“兩步驗證”框中,選擇開始設置。
  3. 按照分步設置過程進行操作。

應用密碼是一個 16 位密碼,可讓應用或設備訪問您的 Google 帳戶。 如果您使用兩步驗證並在嘗試訪問您的 Google 帳戶時看到“密碼不正確”錯誤,應用密碼可能會解決問題。 大多數情況下,您只需為每個應用程序或設備輸入一次應用程序密碼,因此不必擔心記住它

如何生成應用密碼

  1. 訪問您的應用密碼頁面。 系統可能會要求您登錄自己的 Google 帳戶。
  2. 在底部,單擊選擇應用程序並選擇您正在使用的應用程序。
  3. 單擊選擇設備並選擇您正在使用的設備。
  4. 選擇生成。
  5. 按照說明在您的設備上輸入應用程序密碼(黃色欄中的 16 個字符代碼)。
  6. 選擇完成

2020 年,Rails 6 更新了答案:

  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_caching = false
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: "blabla@gmail.com",
    password: "blabla", 
    domain: "smtp.gmail.com",
    openssl_verify_mode: "none",
  }

Google 建議在登錄過程中使用 OAuth 2.0。 這種配置對谷歌來說“不太安全”,但他們可以容忍。 您必須在 Google 帳戶設置中允許“安全性較低的連接”或使用 OAuth 方式。 https://developers.google.com/identity/protocols/OAuth2

他們的 ruby​​ 庫仍然是 alpha 版本。 似乎有一些 gems 為 OAuth 擴展了 ActionMailer,但我從未使用過它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM