Rails 3 电子邮件中的 CSS 图像
我正在尝试使用 Rails 3 和 Action Mailer 发送一封电子邮件。电子邮件发送得很好,但我希望它采用 HTML 格式,并带有一些基本样式,其中包括背景图像。我知道图像可能会被阻止,直到用户允许显示它们,但我仍然认为最好链接到我的网络服务器上的图像。
名为 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 链接以包含完整主机以便背景将显示在电子邮件中的最佳方法是什么?
I'm trying to send out an email with Rails 3 and Action Mailer. The email goes out fine, but I want it to be HTML formatted with some basic styling which includes background images. I understand that the images might get blocked until the user allows them to be shown, but I still think it would be best to link to the images on my web server.
The email template called registration_confirmation.html.erb starts out like this:
<!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;
}
What is the best way to get the url link for the background image to have the full host included so that the background will show up in the email?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在回复我的另一个答案时,@Kevin 写道:
答案是肯定的。所有 Rails url 构造助手都应该使用这些 defualt_url_options。除了设置
:host
之外,您还应该通过设置此选项强制它使用绝对网址:另外,像这样设置资产主机:
然后只需使用
image_path
helper 而不是手写 url,如下所示:注意:直接设置 default_url_options 已被弃用。这是新的方法:
In response to my other answer, @Kevin wrote:
The answer is yes. All rails url-construction helpers should use these defualt_url_options. In addition to setting the
:host
, you should also force it to use absolute urls by settings this option:Also, set the asset host like this:
Then just use the
image_path
helper instead of hand-writing the url, like this:NOTE: Setting default_url_options directly like that is deprecated. Here's the new way to do it:
将您的请求主机作为参数传递给邮件程序方法,然后将其从该方法传递给视图。因此,例如,您的邮件程序方法可能如下所示(示例从rails文档中提取并在此处进行修改):
您可以这样调用它:
然后在您看来,您只需使用@host:
这都是假设图像服务器与运行请求的服务器相同。如果图像服务器托管在其他地方,则必须在此处输出常量。您可以在 lib/settings.rb 中放置类似的内容:
然后在您看来,您只需在那里输出常量,如下所示:
Pass your request host as a parameter to the mailer method, and then pass it from the method to the view. So, for example, your mailer method might look like this (example lifted from rails docs and modified here):
You would call it like this:
Then in your view, you would just use the @host:
This is all assuming the image server is the same as the server running the request. If the image server is hosted elsewhere, you have to output a constant here. You could put something like this in lib/settings.rb:
Then in your view, you'd just output the constant there, like this:
如果您不关心性能,roadie gem 可以为您处理样式表中的网址。
If you do not care about performance, the roadie gem can handle urls in the stylesheets for you.