Ruby on rails 使用Rails 3的电子邮件中的CSS图像

Ruby on rails 使用Rails 3的电子邮件中的CSS图像,ruby-on-rails,actionmailer,html-email,Ruby On Rails,Actionmailer,Html Email,我正试图用Rails 3和Action Mailer发送一封电子邮件。电子邮件发出的很好,但我希望它是HTML格式的一些基本样式,其中包括背景图像。我知道在用户允许显示之前,图像可能会被阻止,但我仍然认为最好链接到我的web服务器上的图像 名为registration_confirmation.html.erb的电子邮件模板的开头如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3

我正试图用Rails 3和Action Mailer发送一封电子邮件。电子邮件发出的很好,但我希望它是HTML格式的一些基本样式,其中包括背景图像。我知道在用户允许显示之前,图像可能会被阻止,但我仍然认为最好链接到我的web服务器上的图像

名为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;
}
def some_action
    UserMailer.registration_confirmation(@user, request.host).deliver
end
<style type="text/css">
body {
    background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
config.action_mailer.asset_host = 'http://localhost:3000'
<style type="text/css">
body {
    background: url(<%= image_path('mainbg_repeat.jpg') %>) top repeat-x #cfcfcf;
}
</style>

无标题文件
身体{
背景:url(/images/mainbg_repeat.jpg)top repeat-x#cfcfcf;
保证金:0px 0px 0px 0px;
字体系列:Arial、Helvetica、无衬线字体;
字体大小:12px;
颜色:#5656;
}

获取背景图像的url链接以包含完整主机以便背景显示在电子邮件中的最佳方法是什么?

将请求主机作为参数传递给mailer方法,然后将其从方法传递给视图。例如,您的mailer方法可能如下所示(示例摘自rails文档并在此处修改):

在您看来,您只需使用@host:

<style type="text/css">
body {
    background: url(http://<%= @host %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
然后在您的视图中,您只需在那里输出常量,如下所示:

<!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;
}
def some_action
    UserMailer.registration_confirmation(@user, request.host).deliver
end
<style type="text/css">
body {
    background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
config.action_mailer.asset_host = 'http://localhost:3000'
<style type="text/css">
body {
    background: url(<%= image_path('mainbg_repeat.jpg') %>) top repeat-x #cfcfcf;
}
</style>

身体{
背景:url(http:///images/mainbg_repeat.jpg)顶部重复-x#cfcfcf;
}

作为对我另一个答案的回应,@Kevin写道:


谢谢你的回答,我确实想过做那样的事,但是我 不要认为我的设置方式是可能的。邮递员 调用发生在模型中的创建后调用中,而不是在 控制器,所以我认为我不能像您一样访问请求对象 提到(或者我错了)。我的邮件初始值设定项中有: ActionMailer::Base.default\u url\u options[:host]=“localhost:3000”我可以吗 在我的邮件中使用hos参数使其工作

答案是肯定的。所有rails url构造助手都应该使用这些default\u url\u选项。除了设置
:host
,还应通过以下设置强制其使用绝对URL:

ActionMailer::Base.default_url_options[:only_path] = false
另外,按如下方式设置资源主机:

<!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;
}
def some_action
    UserMailer.registration_confirmation(@user, request.host).deliver
end
<style type="text/css">
body {
    background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
config.action_mailer.asset_host = 'http://localhost:3000'
<style type="text/css">
body {
    background: url(<%= image_path('mainbg_repeat.jpg') %>) top repeat-x #cfcfcf;
}
</style>
然后只需使用
image\u路径
helper,而不是手工编写url,如下所示:

<!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;
}
def some_action
    UserMailer.registration_confirmation(@user, request.host).deliver
end
<style type="text/css">
body {
    background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
config.action_mailer.asset_host = 'http://localhost:3000'
<style type="text/css">
body {
    background: url(<%= image_path('mainbg_repeat.jpg') %>) top repeat-x #cfcfcf;
}
</style>

如果您不关心性能,可以为您处理样式表中的URL。

谢谢您的回答,我确实想过这样做,但我认为我的设置方式不可能。mailer调用发生在模型中的after_create调用中,而不是在控制器中,因此我认为我无法访问您提到的请求对象(或者我弄错了)。我的邮件初始化器中确实有这样一个选项:
ActionMailer::Base.default\u url\u options[:host]=“localhost:3000”
我可以在邮件中使用hos参数使其工作吗?感谢您提供的额外答案,我尝试过以非摩擦和新的方式配置内容,但仍然无法获得主机的完整路径,我会做更多的研究,看看能找到什么。还有一件事,当我按照建议使用image_标记时,我的电子邮件代码如下:
background:url()top repeat-x#cfcfcf图像标签是正确的辅助工具吗?我不想在我的css中加入我不认为?@Kevin,
image\u tag
是我的错别字。用于写入
图像\u路径
。修正了这个问题。@Kevin,尝试在图像路径中显式设置
:only_path=>false
,看看会发生什么
image\u path('mainbg\u repeat.jpg',:only\u path=>false)
它也不喜欢这样,
参数的数目错误(2对1)
并且出于某种原因,image\u path仍然没有给我完整的路径。除非有人有其他想法,否则我会把主机作为常量或其他东西传递进来。谢谢你在这方面的帮助!我还有一个主意。请尝试:
config.action\u mailer.asset\u host=http://localhost:3000“
在您的环境中。然后使用正常的图像路径。不确定这是否有效,但可能会。我更新了我的答案来说明这一点。