Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2, how to change :from value in a mailer instead default (GMail)

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
like image 795
Sergio Nekora Avatar asked Mar 19 '13 19:03

Sergio Nekora


People also ask

How does Rails action mailer work?

Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.

What is ActionMailer?

Action Mailer is the Rails component that enables applications to send and receive emails. In this chapter, we will see how to send an email using Rails. Let's start creating an emails project using the following command. tp> rails new mailtest. This will create the required framework to proceed.


1 Answers

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)
like image 60
portlandrock Avatar answered Sep 23 '22 04:09

portlandrock