0

I am currently reading through http://learn-rails.com/content/online#send-mail in order to configure a transactional email. I got to the end of the chapter and when testing, I received the following error:

"An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address."

I have the "secrets.yml" and ".bash_profile" configure appropriately so that the to: recipient can be called.

user_mailer.rb

class UserMailer < ApplicationMailer

      default from: "do-not-reply@example.com"

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

contacts_conrtoller.rb

class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(secure_params)
    if @contact.valid?
      UserMailer.contact_email(@contact).deliver_now
      flash[:notice] = "Message sent from #{@contact.name}."
      redirect_to root_path
    else
      render :new
    end
  end

  private

  def secure_params
    params.require(:contact).permit(:name, :email, :content)
  end

end 

contact.rb

class Contact

  include ActiveModel::Model
  attr_accessor :name, :string
  attr_accessor :email, :string
  attr_accessor :content, :string

  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :content
  validates_format_of :email, 
    with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
  validates_length_of :content, :maximum => 500

end
nomad
  • 45
  • 9
  • My guess is `Rails.application.secrets.owner_email` is `nil`, If needed, give it a try with a hard-coded value first. – Wand Maker Dec 31 '15 at 18:39
  • Thanks for the reply, I ran the Rails Console, and yes you were right 'owner_email' is set to 'nil'. So your answer was helpful, however I am not sure why this is. I am thinking for some reason my '.bash_profile' is not being identified. – nomad Dec 31 '15 at 19:00
  • I don't know about your use-case, shouldn't you be using `to: @contact.email` - and `from: Rails.application.secrets.owner_email` ? – Wand Maker Dec 31 '15 at 19:18
  • *Send Contact Message* As the owner of the website I want to receive email messages with a visitor's name, email address, and some text In order to communicate with visitors – nomad Dec 31 '15 at 19:24
  • I tried switching them around, no luck. I feel strongly it has something to do with the communication between, config/secrets.yml and .bachrc. – nomad Dec 31 '15 at 19:25
  • Okay, I misunderstood your requirement. – Wand Maker Dec 31 '15 at 19:47

1 Answers1

1

I was able to identify a solution to this problem. I set up a new gem file - https://github.com/bkeepers/dotenv so that I could add my env variables to a .env file rather than .bashrc or .bash_profile. This fixed the issue.

nomad
  • 45
  • 9