2011-09-22 61 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