带Rails的电子邮件中的CSS图像3 [英] CSS Images in Email With Rails 3

查看:70
本文介绍了带Rails的电子邮件中的CSS图像3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

名为registration_confirmation.html.erb的电子邮件模板如下所示:

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;
}

获取URL链接以获得背景图像的最佳方法是什么

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?

推荐答案

为回应我的其他回答,@Kevin写道: :

In response to my other answer, @Kevin wrote:


谢谢您的回答,我确实考虑过要这样做,但是我
认为这不可能我设置的方式。邮件发件人
调用发生在模型中的after_create调用中,而不是在
控制器中,因此我认为您不能访问
中的请求对象(否则我会误认为)。我在邮件初始化程序中确实有此功能:
ActionMailer :: Base.default_url_options [:host] = localhost:3000我可以
以某种方式在我的邮件程序中使用该hos参数使它起作用吗?

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?

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

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'

然后只需使用 image_path 助手,而不是手动像这样写网址:

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>

注意:不建议像这样直接设置default_url_options。这是执行此操作的新方法:

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

这篇关于带Rails的电子邮件中的CSS图像3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆