小马不发送电子邮件到Gmail地址?

时间:2011-07-27 18:59:26

标签: ruby sinatra pony

我有一个报名表,要求提供人名和电子邮件地址。我将该电子邮件地址保存到会话中,以便在表单提交后我可以访问它。然后我使用Pony向提交表单的人发送感谢/通知电子邮件。但是,虽然它没有问题发送到MobileMe地址,但它不会发送到Gmail地址。我用来发送的行是:

Pony.mail(:to => "#{@email}", :from => 'from@email.com', :subject => "Thanks for entering!", 
:body => "Thank you!")

@email变量在处理程序中定义,并从会话中获取值。

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

以下是我使用的辅助方法,它使用Pony在我的Mac上进行开发时使用sendmail发送电子邮件,或者在生产时使用sendgrid上的Heroku发送电子邮件。这可靠地工作,我的所有测试电子邮件都会发送到我的各种Gmail地址。

可能您的问题是,您的from地址无效,Google正在将其标记为垃圾邮件。另请注意,您没有设置Content-Type标头,在我的情况下通常为text/html

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