Rails and Gmail - Reply-To function not working as hoped
In my rails app I have a contact form that lets users send an email to my gmail account.
Problem 1
I'm using the same gmail account to send the emails as I'm using to receive them, and thus when I click the 'reply' button, gmail sets-up a new email to myself....
My mailer looks like this:
class Notification < ActionMailer::Base
default :to => ENV['GMAIL_USER']
def hello(contact)
mail(
:from => contact[:email],
:return_path => contact[:email],
:subject => "[My Website] << #{contact[:subject]}",
:body => contact[:body] )
end
end
I was under the impression that setting :return_path => contact开发者_如何学Python[:email]
would ensure that gmail would know who to send the reply to.... Obviously I'm wrong there. Anyone know how to fix this?
Problem 2
Most action_mailer tutorials out there would have me set up my mail method using :from => contact.email
as opposed to :from => contact[:email]
like so:
def hello(contact)
@contact = contact
mail(
:from => contact.email,
:return_path => contact.email,
:subject => "[My Website] << #{contact.subject}",
:body => contact.body
)
end
But when I do it this way, I get the following error message:
undefined method `email' for #<ActiveSupport::HashWithIndifferentAccess:0xdde82f0>
Anyone know what the recommended approach isn't working for me.
Appendage
FWIW, in-case it helps, my contact model extends a tabless active_record model as I don't want to use the database but do want to have valiations so it looks like this:
#contact.rb
class Contact < Tabless
column :name, :string
column :email, :string
column :body, :text
column :subject, :string
# validations go here
end
# tabless.rb
class Tabless < ActiveRecord::Base
# create an array of columns
def self.columns()
@columns ||= [];
end
# add new column to columns array
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s,
default,
sql_type.to_s,
null)
end
# override the save method to prevent exceptions
def save(validate = true)
validate ? valid? : true
end
end
Maybe I'm missing something but can't you just use :reply_to
?
mail(
:from => contact[:email],
:reply_to => contact[:email],
:subject => "[My Website] << #{contact[:subject]}",
:body => contact[:body] )
And any way you can get the email as a string should be fine. .email
isn't a valid method for a Hash so it won't work in your example. The other examples you've seen are probably assuming you're working with a contact model. If contact[:email] gets you an email address then it shouldn't be a problem.
I've managed to fix this by creating another gmail account and using that one to send the emails. Now when I hit the reply button, it sets-up an email to the actual sender
This link explains helped me understand it
The symbol for the e-mail Reply-To header is :'reply-to'. To the original question, this should work:
mail(
:from => contact[:email],
:'reply-to'=> contact[:email],
:subject => "[My Website] << #{contact[:subject]}",
:body => contact[:body] )
I ran into this situation recently myself with Gmail rewriting the email's From header. I blogged about the solution today at http://blog.rietta.com/2012/01/rails-gmail-reply-to-on-contact-form.html.
For me it is easy to respond to an e-mail either with Apple Mail or Google Apps itself. So the reply-header seems to be transmitted correctly by Google's SMTP server.
I hope this helps someone avoid this little catch :-)
精彩评论