2013-03-19 92 views
4

我建立了一個聯繫我們表單,我很好地將電子郵件發送到我的gApps收件箱中。但是,收到的電子郵件會顯示默認值:來自網站的值。我希望他們出現在電子郵件用戶的電子郵件字段中輸入。所以,當我點擊'回覆'時,它將用戶地址作爲電子郵件必須發送的地址。Rails 3.2,如何更改:從郵件中的值改爲默認值(GMail)

我試過這個,但它不起作用。任何想法爲什麼?

notifications_mailer.rb

class NotificationsMailer < ActionMailer::Base 
    default to: "[email protected]" 

    def new_contact(message) 
     @message = message 
     mail(:subject => "[hiKultura.com] #{message.subject}", 
     :from => message.email) 
    end 
end 

的ContactController

class ContactController < ApplicationController 

    def new 
    @message = Contactmessage.new 
    end 

    def create 
    @message = Contactmessage.new(params[:contactmessage]) 

    if @message.valid? 
     NotificationsMailer.new_contact(@message).deliver 
     redirect_to(root_path, :notice => "Message was successfully sent.") 
    else 
     flash.now.alert = "Please fill all fields." 
     render :new 
    end 
    end 

end 

UPDATE

我跟着類似的情況下,我在SO就發現:REPLY_TO和Gmail。但是,當我點擊回覆時仍然顯示:給電子郵件地址。我錯過了什麼?

notifications_mailer.rb

class NotificationsMailer < ActionMailer::Base 

    default :from => '[email protected]' 

    def new_contact(message) 
     @message = message 
     mail(:reply_to => "[email protected]", 
     :subject => "[mydomain.com] #{message.subject}", 
     :to => "[email protected]" 
    ) 
    end 
end 
+0

message.email返回什麼? – 2013-03-19 19:49:15

+0

我怎麼知道?我認爲它應該工作,因爲'message.subject'內容被添加到主題行。 – 2013-03-19 19:52:21

+0

你從哪裏來的?你在傳什麼? – 2013-03-19 19:53:40

回答

4

大多數SMTP提供商(Gmail等)不會讓您比您登錄到SMTP帳戶關聯的地址以外的地址發送一封電子郵件服務器,您的情況可能是您的應用中配置的默認地址。如果你仔細想想,這對於安全/垃圾郵件保護有很大的意義。您是否希望任何人都能夠發送顯示您的電子郵件地址爲發件人地址的電子郵件?我當然不會。另一種方法是使用:reply_to。當你在你的電子郵件程序中回覆時,如果它存在,應該使用它。

mail(:subject => "[hiKultura.com] #{message.subject}", 
    :reply_to => message.email) 
+0

:rep​​ly_to不適合我... – 2013-03-20 08:17:23

+0

我同意背後的邏輯。但是,我無法得到:reply_to working。我更新了那一點代碼。謝謝 – 2013-03-20 10:25:54

+0

您也可以使用API​​文檔中描述的'from'參數。但是,您必須確保您的SMTP服務器配置正確,以允許中繼,可能使用經過身份驗證的SMTP。 http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail – 2013-03-20 10:30:27