5
votes

I have built a contact us form and I nicely get the emails in my gApps inbox. However, the received emails show the default :from value of the site. I would like them to appear sent from the email users type in the email field of them form. So, when I hit 'reply' it takes the user address as the address the email has to be sent.

I have tried this, but it does not work. Any idea why?

notifications_mailer.rb

class NotificationsMailer < ActionMailer::Base
  default to: "[email protected]"

  def new_contact(message)
      @message = message
      mail(:subject => "[hiKultura.com] #{message.subject}",
        :from => message.email)
  end
end

ContactController

class ContactController < ApplicationController

  def new
    @message = Contactmessage.new
  end

  def create
    @message = Contactmessage.new(params[:contactmessage])

    if @message.valid?
      NotificationsMailer.new_contact(@message).deliver
      redirect_to(root_path, :notice => "Message was successfully sent.")
    else
      flash.now.alert = "Please fill all fields."
      render :new
    end
  end

end

UPDATE

I followed similar cases I found in SO regarding :reply_to and GMail. But, still shows the :to email address when I hit reply. What am I missing???

notifications_mailer.rb

class NotificationsMailer < ActionMailer::Base

   default :from => '[email protected]'

  def new_contact(message)
      @message = message
      mail(:reply_to => "[email protected]",
      :subject => "[mydomain.com] #{message.subject}", 
      :to => "[email protected]"
      )
  end
end
1
What does message.email return?John Naegle
How can I know that? I assumed it should work because 'message.subject' content gets added to the subject line.Sergio Nekora
Where are you calling it from? What are you passing in?John Naegle
added the controller.. that might make things clearerSergio Nekora
well.... reading on the net just discover that gmail does not let you override the :from value.... shite!Sergio Nekora

1 Answers

5
votes

Most SMTP providers (Gmail, etc.) won't let you send an email from an address other than the address associated with the account that you are logging in to the SMTP server with, which in your case is probably the default from address configured in your app. If you think about it, that makes a lot of sense for security/SPAM protection. Would you want anyone to be able to send an email that showed your email address as the from address? I sure wouldn't. An alternative is to use :reply_to. When you hit reply in your email program it should use this if it's present.

  mail(:subject => "[hiKultura.com] #{message.subject}",
    :reply_to => message.email)