Sinatra with Pony always has the same email for from

133 Views Asked by At

I'm working through a contact form from Sinatra Jump Start and trying to figure out why the "From" is always the same gmail account I'm using as the :user_name. I have an Heroku version and it's also always the same :from. My goal is to have the little contact form where there is Name, Email and Message send to the receiving email and the "From" would be the sender's Name and Email.

I thought maybe this is because of local version, but it's the same result on Heroku. The setting for :user_name ends up as the "From" I'm doing something wrong, any ideas?

Is the :from => params[:name] + "<" + params[:email] + ">" that's no longer correct, the book I'm working from isn't too old.

Here's what my contact.slim form looks like:

p You can contact  me by filling in the form below:
form class="contact" action="/contact" method="POST"
  p
    label for="name" Name:
    input type="text" name="name"
  p
    label for="email" Email:
    input type="text" name="email"
  p
    label for="message" Your Message:
    textarea name="message"
  input type="submit" value="Send Message"

Here's my send_message:

 def send_message
    Pony.mail(
      # this always comes back as the same gmail.account I'm using for the :user_name
      :from => params[:name] + "<" + params[:email] + ">", 
      :to => '[email protected]',
      :subject => params[:name] + " from #{params[:email]} has contacted you ",
      :body => params[:message],
      :via => :smtp,
      :via_options => {
        :address              => 'smtp.gmail.com',
        :port                 => '587',
        :enable_starttls_auto => true,
        :user_name            => '[email protected]',
        :password             => 'mypassword',
        :authentication       => 'plain',
        :domain               => 'localhost.localdomain'
      })
  end

And my post route handler from my main.rb

post '/contact' do
  send_message
  flash[:notice] = "Thank you for your message. We'll be in touch soon."
  redirect to("/")
end

Then when I tried on Heroku for I have this in the main.rb:

configure :development do
  # for the local MySQL db
  DataMapper.setup(:default, 'mysql://root:admin@localhost/sinatra_jumpstart')
  set :email_address      => 'smtp.gmail.com',
      :email_user_name    => '[email protected]',
      :email_password     => 'mypassword',
      :email_domain       => 'localhost.localdomain'
end

configure :production do
  # for Heroku
  DataMapper.setup(:default, ENV[ 'DATABASE_URL' ])
  set :email_address      => 'smtp.sendgrid.net',
      :email_user_name    => ENV['SENDGRID_USERNAME'],
      :email_password     => ENV['SENDGRID_PASSWORD'],
      :email_domain       => 'heroku.com'
end

Update: It seems like if my goal is to be able to reply-to, there's a :reply_to option that I can pass the params[:email] to, but that still leaves me wondering why the 'From' show the name (coming from params[:name]) correctly, but ignores the params[:email] for the 'From' email address and instead shows my :user_name email from the :via_options instead.

1

There are 1 best solutions below

0
On

This answer came from a sitepoint discussion.

You are sending the form from your webpage and so the sender is yourself. If you were opening the senders email software ( using the html mail function? ) and the user was sending from that it would have their email address.

This happens a lot in forums when the user gets an email from another user and uses "reply". The email is then sent to the forum admin. There is usually a note on the message saying something like "Do not reply to this message but do so in the forum messaging section".