简体   繁体   中英

Rails Environment Variable Not Getting Set on Production on Heroku

I am trying to setup ActionMail using Mailgun and DelayedJob on Heroku and I've got everything working to the point where DelayedJob is initiating the request with Mailgun, but I'm getting a 400 Bad Request: 'from' parameter is missing from Mailgun.

My mailer is app/mailers/example_mailer.rb :

class ExampleMailer < ApplicationMailer

def sample_email(user)
    @user = user
    mailgun_client = Mailgun::Client.new ENV['MAILGUN_API_KEY']
    message_params = {:from     => ENV['GMAIL_USERNAME'],
                      :to       => @user.email,
                      :subject  => 'Sample Mail Using Mailgun API',
                      :text     => 'Testing...'}

    mailgun_client.send_message ENV['MAILGUN_DOMAIN'], message_params

  end
end

My config/application.yml :

MAILGUN_API_KEY:  '[REDACTED]'
MAILGUN_ADDRESS:  'smtp.mailgun.org'
MAILGUN_PORT:     587
MAILGUN_DOMAIN:   '[REDACTED]'
MAILGUN_USERNAME: '[REDACTED]'
MAILGUN_PASSWORD: '[REDACTED]'
GMAIL_USERNAME:   '[REDACTED]'

So since the error is that the from field isn't being set properly I logged into my heroku console and tested my ENV variables and lo and behold all of them are present except ENV['GMAIL_USERNAME'] . Is there any reason why this variable would be missing in my production environment? I'm not sure how to debug this as I've never come across a problem with environment variables like this before.

I'm an idiot, I setup figaro to handle my environment variables and forgot to publish them to heroku:

figaro heroku:set -e production

Also, you need to wrap all variables in application.yml in strings, so I also had to update the MAILGUN_PORT to '587'.

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