ruby-on-railsruby-on-rails-4learn-ruby-on-rails

Why do I get: An SMTP To address is required to send a message. "Set the message smtp_envelope_to, to, cc, or bcc address."?


I am following the learn Ruby on Rails book by Daniel Kehoe and when I try to send mail, I get the following error.

ArgumentError at /contacts An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.

The better errors gem points to UserMailer.contact_email(@contact).deliver in the Contacts Controller.

I have done some research and prior to the error had followed the book's instruction on setting the environmental variables in my .bash_profile file.

Any help is greatly appreciated.

Update

As per the responses, thank you. I had already set the mail (to: )

def contact_email(contact)
  @contact = contact
  mail(to: Rails.application.secrets.owner_email, from: @contact.email, :subject => "Website Contact")
end

And I set the smtp settings:

config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: Rails.application.secrets.domain_name,
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: Rails.application.secrets.email_provider_username,
  password: Rails.application.secrets.email_provider_password
}

Update 2

Thanks. Find secrets below :) I excluded the secret key base here

development:
  email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
  email_provider_password: <%= ENV["GMAIL_PASSWORD"] %>
  domain_name: example.com
  mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
  mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
  owner_email: <%= ENV["OWNER_EMAIL"] %>
  secret_key_base: 'my secret key_base'


test:
  secret_key_base: 'my secret key_base'

production:
  email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
  email_provider_password: <%= ENV["GMAIL_PASSWORD"] %>
  domain_name: <%= ENV["DOMAIN_NAME"] %>
  mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
  mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
  owner_email: <%= ENV["OWNER_EMAIL"] %>
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

Solution

  • Through some detective workI stumbled on a solution that only required me to restart terminal

    The problem is as described as above. Earlier I had filled out the following environmental variables in my .bash_profile file.

    export GMAIL_USERNAME="my_email@gmail.com"
    export GMAIL_PASSWORD="my_password"
    export MAILCHIMP_API_KEY="my mail_chimp_api_key"
    export MAILCHIMP_LIST_ID="my mail_chimp_list_id"
    export OWNER_EMAIL="my_email@gmail.com"
    

    But it app wasn't able to access these variables somehow.

    I came across and typed the following command in terminal

    > env
    

    It lists all the environmental variables in your pc environment. (I think)

    Anyway I saw the variables above that I had set which meant that my variables were properly set and it was just the app that was unable to access the variables.

    After some more digging, I came across this post that said something about restarting terminal.

    I tried that and it worked.

    The lesson learned is that when I alter environmental variables, I should restart my terminal.

    I hope this helps others.