问题
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: "info@hikultura.com"
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 => 'no-reply@mydomain.com'
def new_contact(message)
@message = message
mail(:reply_to => "mierda@hotmail.com",
:subject => "[mydomain.com] #{message.subject}",
:to => "admin@mydomain.com"
)
end
end
回答1:
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)
来源:https://stackoverflow.com/questions/15508994/rails-3-2-how-to-change-from-value-in-a-mailer-instead-default-gmail