Rails 3 电子邮件中的 CSS 图像

发布于 2024-12-06 07:26:03 字数 813 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美胚控场 2024-12-13 07:26:03

在回复我的另一个答案时,@Kevin 写道:

谢谢你的回答,我确实想过做类似的事情,但我
不认为以我的设置方式是可能的。邮寄者
调用发生在模型中的 after_create 调用中,而不是在
控制器,所以我认为我无法像您一样访问请求对象
提到过(或者我错了)。我的邮件初始化程序中确实有这个:
ActionMailer::Base.default_url_options[:host] = "localhost:3000" 我可以吗
以某种方式在我的邮件程序中使用该 hos 参数使其工作?

答案是肯定的。所有 Rails url 构造助手都应该使用这些 defualt_url_options。除了设置 :host 之外,您还应该通过设置此选项强制它使用绝对网址:

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

另外,像这样设置资产主机:

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

然后只需使用 image_path helper 而不是手写 url,如下所示:

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

注意:直接设置 default_url_options 已被弃用。这是新的方法:

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

In response to my other answer, @Kevin wrote:

Thanks for the answer, I did think of doing something like that but I
don't think it's possible with the way I have it setup. The mailer
call is happening in a after_create call in a model rather than in a
controller so I don't think I have access to the request object as you
mentioned (or am I mistaken). I do have this in my mailer initializer:
ActionMailer::Base.default_url_options[:host] = "localhost:3000" Can I
somehow use that hos parameter in my mailer to make it work?

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:

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

Also, set the asset host like this:

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

Then just use the image_path helper instead of hand-writing the url, like this:

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

NOTE: Setting default_url_options directly like that is deprecated. Here's the new way to do it:

config.action_mailer.default_url_options = {
    :host => 'localhost:3000',
    :only_path => false
}
各自安好 2024-12-13 07:26:03

将您的请求主机作为参数传递给邮件程序方法,然后将其从该方法传递给视图。因此,例如,您的邮件程序方法可能如下所示(示例从rails文档中提取并在此处进行修改):

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>

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):

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

You would call it like this:

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

Then in your view, you would just use the @host:

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

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:

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

Then in your view, you'd just output the constant there, like this:

<style type="text/css">
body {
    background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf;
}
</style>
少钕鈤記 2024-12-13 07:26:03

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

If you do not care about performance, the roadie gem can handle urls in the stylesheets for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文