I've got an entry form that asks for a persons name and email address. I save that email address to the session so I can access it after the form has been submitted. Then I use Pony to send a thank you/notification email to the person who submitted the form. However, while it sends without issue to a MobileMe address, it won't to a gmail address. The line I'm us开发者_运维百科ing to send is:
Pony.mail(:to => "#{@email}", :from => 'from@email.com', :subject => "Thanks for entering!",
:body => "Thank you!")
The @email variable is defined in the handler and gets the value from the session.
Any ideas?
Here's the helper method I use that uses Pony to send email using sendmail
when in development on my Mac or via sendgrid
on Heroku
when in production. This works reliably and all of my test emails get sent to my various gmail addresses.
Possibly your problem is that your from
address is invalid and Google is flagging that as spam. Also I note you are not setting the Content-Type
header, which is typically text/html
in my case.
def send_email(a_to_address, a_from_address , a_subject, a_type, a_message)
begin
case settings.environment
when :development # assumed to be on your local machine
Pony.mail :to => a_to_address, :via =>:sendmail,
:from => a_from_address, :subject => a_subject,
:headers => { 'Content-Type' => a_type }, :body => a_message
when :production # assumed to be Heroku
Pony.mail :to => a_to_address, :from => a_from_address, :subject => a_subject,
:headers => { 'Content-Type' => a_type }, :body => a_message, :via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => 25,
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN'] }
when :test
# don't send any email but log a message instead.
logger.debug "TESTING: Email would now be sent to #{to} from #{from} with subject #{subject}."
end
rescue StandardError => error
logger.error "Error sending email: #{error.message}"
end
end
精彩评论