Wasabi Developer
Wasabi Developer

Reputation: 3553

Rails mailer Net::SMTPAuthenticationError (530-5.5.1 Authentication Required) with Gmail using ENV[USERNAME] and ENV[PASSWORD]

I'm getting an error when sending out mail from the Production environment, specifically:

Net::SMTPAuthenticationError (530-5.5.1 Authentication Required)

On my local machine and development environment I'm declaring the passwords using ENV[GMAIL_USERNAME] and ENV[GMAIL_PASSWORD] and it's working fine, where the declared ENV is stored in my .bash_profile

  # Development Environment for Action Mailer config
  config.action_mailer.default_url_options = { host: '0.0.0.0:3000' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default charset: 'utf-8'
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'mydomain.com',
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"]
  }

  # Production Environment for Action Mailer config
  config.action_mailer.default_url_options = { host: 'mydomain.com' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default charset: 'utf-8'
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "mydomain.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"]
  }

Unfortunately, in Production I get an error when sending mail. However, if I hardcode the username and password in environments/production.rb it works. The ENV["GMAIL_USERNAME"] and `ENV["GMAIL_PASSWORD"] is set in the .bash_profile:

export GMAIL_USERNAME="[email protected]"
export GMAIL_PASSWORD="mypassword"

I thought it would it was the app unable to call ENV["GMAIL_USERNAME"] and ENV["GMAIL_PASSWORD"] but when I jump into the production rails console and puts ENV["GMAIL_USERNAME"] it outputs the correct credentials.

I have restarted Apache and restarted my app multiple times but I'm puzzled what to do next.

Any help is much appreciated.

Thank you in advanced.

Upvotes: 2

Views: 4757

Answers (1)

Doon
Doon

Reputation: 20232

Whose bash profile? Your users? Apache won't read those profiles on startup as it doesn't run bash to start the server.. Your profile gets sourced when you login to the server. Which is why it works when you test.

You need to make sure that environment variables are defined in the script or the environment that apache runs under. Depending upon your server this could be in /etc/apache2/envvars or added to the startup script for apache.

Upvotes: 3

Related Questions