2011-09-22 18 views
4

我試圖用Rails 3和Action Mailer發送一封電子郵件。電子郵件沒問題,但我希望它是HTML格式的一些基本的樣式,其中包括背景圖像。我知道圖像可能會被阻止,直到用戶允許它們顯示,但我仍然認爲最好鏈接到我的Web服務器上的圖像。電子郵件中的CSS圖像使用Rails 3

稱爲registration_confirmation.html.erb電子郵件模板開始時是這樣的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
body { 
    background: url(/images/mainbg_repeat.jpg) top repeat-x #cfcfcf; 
    margin: 0px 0px 0px 0px; 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 12px; 
    color: #565656; 
} 

什麼是獲取URL鏈接,背景圖像具有完整的主機包括在內,以便背景將最好的辦法在電子郵件中顯示?

回答

16

在回答我的其他答案,@Kevin寫道:

感謝您的回答,我沒想到做這樣的事情,但我 不認爲這可能與我有它設置的方式。郵件 調用正在模型中的after_create調用中發生,而不是在 控制器中,所以我不認爲我有權訪問請求對象,因爲您提到(或者我錯了) 。我確實在我的郵件程序初始化程序中有這個: ActionMailer :: Base.default_url_options [:host] =「localhost:3000」我可以用 在我的郵件程序中使用hos參數來使其工作嗎?

答案是肯定的。所有的rails url-construction helper都應該使用這些defualt_url_options。除了設置:host,你也應該迫使它通過設置使用絕對URL此選項:

ActionMailer::Base.default_url_options[:only_path] = false 

而且,設置資產主機這樣的:

config.action_mailer.asset_host = 'http://localhost:3000' 

然後,只需使用image_path幫手而不是手寫的網址,例如:

<style type="text/css"> 
body { 
    background: url(<%= image_path('mainbg_repeat.jpg') %>) top repeat-x #cfcfcf; 
} 
</style> 

注:設置default_url_options直接喜歡就是deprec ated。這裏是新的方式來做到這一點:

config.action_mailer.default_url_options = { 
    :host => 'localhost:3000', 
    :only_path => false 
} 
+0

感謝您的額外答案,我嘗試配置東西既depricated和新的方式,仍然無法與主機得到完整的路徑,我會做一些更多的研究,看看我能找到。還有一件事,當我按照建議使用image_tag時,我的電子郵件代碼如下所示:'background:url(Mainbg_repeat)top repeat-x #cfcfcf;'image_tag是否是正確的幫手?我不想在我的css中有我不認爲? – Kevin

+0

@Kevin,'image_tag'是我的一個錯字。意思是寫'image_path'。修正了。 –

+0

@Kevin,試着在image_path中明確地設置':only_path => false',看看會發生什麼。 'image_path('mainbg_repeat.jpg',:only_path => false)' –

2

將您的請求主機作爲參數傳遞給郵件程序方法,然後將其從方法傳遞給視圖。因此,舉例來說,您的郵件的方法可能是這樣的(例如,從軌道文檔取消,此修改):

class UserMailer < ActionMailer::Base 
    default :from => "[email protected]" 

    def registration_confirmation(user, host) 
    @user = user 
    @host = host 
    mail(:to => user.email, :subject => "Welcome to My Awesome Site") 
    end 
end 

你會這樣稱呼它:

def some_action 
    UserMailer.registration_confirmation(@user, request.host).deliver 
end 

那麼在你看來,你將只使用@host:

<style type="text/css"> 
body { 
    background: url(http://<%= @host %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf; 
} 
</style> 

這是假設圖像服務器與運行請求的服務器相同。如果圖像服務器託管在別處,則必須在此處輸出常量。你可以把這樣的事情在LIB/settings.rb:

module Settings 
    IMAGE_HOST = 'superawesome.images.com' 
end 

然後在你看來,你只是輸出的不斷出現,像這樣:

<style type="text/css"> 
body { 
    background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf; 
} 
</style> 
+0

感謝您的回答,我確實想過做這樣的事情,但我認爲這是不可能的,因爲我有它的設置。郵件調用發生在模型中的after_create調用中,而不是控制器中,所以我不認爲我有權訪問請求對象(或者我錯了)。 我確實在我的郵件程序初始化程序中有這個: 'ActionMailer :: Base.default_url_options [:host] =「localhost:3000」' 我可以以某種方式在我的郵件程序中使用該hos參數使其工作? – Kevin