2010-09-06 48 views
7

我在我的Notifier模型(20多封電子郵件)中對所有電子郵件使用了一種佈局......但是有時候我只是想發送一個沒有佈局或html的純文本電子郵件。我似乎無法弄清楚如何?如果我嘗試發送純文本電子郵件,我仍然可以獲得佈局以及電子郵件中的所有HTML。不同佈局的軌道郵件

我正在使用Rails 2.3.8。

我在這裏看到這個猴子補丁...但它似乎表明一個更新版本的軌道已經結束了嗎?如果我可以避免猴子補丁,我並不是真的想要猴子補丁。

Rails - setting multiple layouts for a multipart email with mailer templates

layout "email" # use email.text.(html|plain).erb as the layout 


    def welcome_email(property) 
    subject 'New Signup' 
    recipients property.email 
    from  '[email protected]' 
    body  :property => property 
    content_type "text/html" 
    end 

    def send_inquiry(inquire) 
    subject "#{inquire.the_subject}" 
    recipients inquire.ob.email 
    from  "Test on behalf of #{inquire.name} <#{inquire.email}>" 
    body  :inquire => inquire 
    content_type "text/plain" 

    end 

我也有2個文件。

email.text.html.erb 
email.text.plain.erb 

它總是使用text.html.erb ......即使是CONTENT_TYPE 「text/plain的」

回答

5

OK,不知道這工作,但似乎默認CONTENT_TYPE爲text/plain ,所以你只需要設置內容類型,如果你想要的東西以外的文本/平原。只有

layout "email", :except => [:send_inquiry] 

我就用上面的,因爲純文本電子郵件似乎並不有一個「佈局」,:

試試這個:

def send_inquiry(inquire) 
    subject "#{inquire.the_subject}" 
    recipients inquire.ob.email 
    from  "Test on behalf of #{inquire.name} <#{inquire.email}>" 
    body  :inquire => inquire 
end 

我還是覺得你應該考慮這您想要發送的實際內容。

+0

這可能是一個快速修復,但我仍然喜歡它,如果我能弄清楚爲什麼據說內置的功能不起作用。 IE瀏覽器,當我聲明一個content_type它應該尋找適當的文件? – holden 2010-09-10 16:14:52

+0

我更新了我的答案..不確定,但值得一試。祝你好運。 – Mischa 2010-09-10 17:06:06

9

編輯:想通了,佈局遵循不同的電子郵件模板的命名方案。

layout.text.html.erb => layout.html.erb 
layout.text.plain.erb => layout.text.erb 

我也做的手工定義零件的錯誤,如果你使用這個:正如如下命名它們

part :content_type => 'text/plain', 
    :body => render_message('my_template') 

然後Rails的無法確定的content_type您的一部分,它假定它是HTML。

當我改變了這兩件事情後,它爲我工作!

原答覆如下..


我掙扎着這個問題很多次,在過去,通常與某種非乾燥快速和骯髒的解決方案結束了。我一直認爲我是唯一一個遇到這個問題的人,因爲Google在這個問題上變得毫無用處。

這次我決定深入研究Rails,但目前爲止沒有取得太大的成功,但也許我的發現將幫助其他人弄清楚這一點。

我發現在ActionMailer :: Base中,#render_message方法的任務是確定正確的content_type,並將其分配給@current_template_content_type。 #default_template_format然後返回佈局的適當的MIME類型,或者,如果未設置@current_template_content_type,它將默認爲:html。

這是什麼的ActionMailer :: Base的#render_message看起來像我的應用程序(2.3.5)

def render_message(method_name, body) 
    if method_name.respond_to?(:content_type) 
     @current_template_content_type = method_name.content_type 
    end 
    render :file => method_name, :body => body 
    ensure 
    @current_template_content_type = nil 
    end 

麻煩的是,METHOD_NAME似乎是一個字符串(局部視圖的名稱,在我case「new_password.text.html」)和字符串當然不會響應#content_type,這意味着@current_template_content_type將始終保持爲零,因此#default_template_format將始終默認爲:html。我知道,沒有太多接近實際的解決方案。 ActionMailer內部對我來說太不透明瞭。

+0

嗯,有趣。與你所說的相反,根據API文檔,默認的content_type是text/plain ... – Mischa 2010-09-14 14:15:32

+0

這對我來說也會更有意義,但是在ActionMailer :: Base中尋找#default_template_format,這是迄今爲止的罪魁禍首因爲我可以解決。我會繼續尋找和更新,因爲我發現更多。 – 2010-09-14 15:39:20

+0

我想我已經明白了,或多或少。我的問題是我用「part:body => render_message()」手動定義了部件,如果你這樣做的話,ActionMailer會忽略content_type並且默認爲html,就像我上面描述的那樣。 但是,如果讓Rails自動確定部件,render_message會獲取一個ActionView :: ReloadableTemplate,而不是以「method_name」(說出誤導變量名稱)和content_type檢測的方式傳遞的字符串。 – 2010-09-14 16:17:32