2011-07-27 18 views
2

我有一個要求輸入人名和電子郵件地址的輸入表單。我將該電子郵件地址保存到會話中,以便在提交表單後訪問它。然後我使用Pony向提交表單的人發送感謝/通知電子郵件。但是,儘管它沒有問題發送到MobileMe地址,但它不會發送到Gmail地址。我使用的線路是:小馬不發送郵件到gmail地址?

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

@email變量在處理程序中定義並從會話中獲取值。

任何想法?

回答

6

這裏的輔助方法,我使用了一個小馬使用sendmail在我的Mac開發時或通過sendgridHeroku在生產時發送電子郵件。這工作可靠,我的所有測試電子郵件都發送到我的各種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 
相關問題