2

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

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

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

有任何想法吗?

4

1 回答 1

6

sendmail这是我使用的辅助方法,它使用 Pony在我的 Mac 上开发时使用或在生产时使用sendgridon发送电子邮件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
于 2011-12-13T21:55:22.877 回答