简体   繁体   中英

Rails 4 action mailer SMTP-AUTH error missing secret phrase?

I'm having trouble sending email using Rails 4 action mailer via gmail. It all should be so simple... yet it isn't working. Specifically, I get

ArgumentError (SMTP-AUTH requested but missing secret phrase):

I've read the other questions here regarding the situation, but most of them had simple fixes ("user_name" instead of "username", or ENV variable was wrong). I've double checked, and I'm certain my credentials are correct. Here is the relevant section of my development.rb:

config.action_mailer.delivery_method = :smtp                          
config.action_mailer.smtp_settings = {                    
  address: 'smtp.gmail.com',                                      
  port: 587,  
  domain:  'mydomain.com', 
  user_name:  ENV["GMAIL_USERNAME"],  
  password:  ENV["GMAIL_PXXWRD"],   
  authentication: 'plain',
  enable_starttls_auto: true                         
}  

I initially had the credentials in plaintext, but that I though maybe I should move them to env variables to solve the issue. It didn't change anything. As a note, my gmail is a company gmail, so it doesn't actually end in "@gmail.com". I'm not sure if that changes anything, but I thought I'd mention it. I've also tried with a standard gmail address, it changes nothing.

Also, does "missing secret phrase" refer to a bad password? Or is it not finding a password to begin with? What else could cause the error? Thanks for your time guys.

I just dealt with a very similar scenario in an app I'm working on. I was getting the exact same "SMTP-AUTH requested but missing secret phrase" error. What ultimately fixed it for me was two-fold:

1) Make sure that you're using an application-specific password for gmail. Instructions from Google here > How to generate an App password. Note that if you don't already have 2-factor authentication, you'll need to set that up first as well. You'll end up with an application password that is 16 characters long. You'll use that as the password that Rails uses via your SMPT settings.

2) Make sure that you have appropriately setup your local environment variables file so that it loads upon compiling of the Rails app, and is excluded from git tracking by putting the filename in your gitignore. I use a file called app_name/config/local_env.yml, but you could also use the standard secrets.yml file that comes with Rails 4.2, or the figaro gem is pretty popular for managing ENV variables. This RailsApps page gives a good overview of how to manage your ENV variables.

Ultimately, my SMTP settings ended up looking nearly identical to yours:

#GMAIL CONFIG
config.action_mailer.default_url_options = { :host =>'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}

Best of luck!

for me i had put the password in bashrc file and i forgot to restart the puma server. if you restart the puma server then this error is fixed.

Had this setup locally working fine and when deployed to prod it didn't work, I think Google may check the device it's comming from, or at least the ip, I generated a new application password for the production server and after a couple of minutes it started working.

So if Google SMTP AND you change device/ip without config changes try generating a new APP password.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM