如何使用 Rails 中的 css 和图像创建电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/472450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create email with css and images from Rails?
提问by Akshay
How do you create and send emails from Rails application, that contain images and proper formatting? like the ones you get from facebook and like.
您如何从 Rails 应用程序创建和发送包含图像和正确格式的电子邮件?就像你从 facebook 上得到的一样。
回答by Bo Jeanes
Assuming you know how to send normal plain-text emails from Rails using ActionMailer, to get HTML
emails working you need to set a content type for your email.
假设您知道如何使用 ActionMailer 从 Rails 发送普通的纯文本电子邮件,要使HTML
电子邮件正常工作,您需要为电子邮件设置内容类型。
For example, your notifer might look like this:
例如,您的通知程序可能如下所示:
class MyMailer < ActionMailer::Base
def signup_notification(recipient)
recipients recipient.email_address_with_name
subject "New account information"
from "[email protected]"
body :user => recipient
content_type "text/html"
end
end
Note the content_type "text/html"
line. This tells ActionMailer to send an email with a content type of text/html
instead of the default text/plain
.
注意content_type "text/html"
行。这告诉 ActionMailer 发送一封内容类型为 的电子邮件,text/html
而不是默认的text/plain
。
Next you have to make your mailer views output HTML
. For example, your view file app/views/my_mailer/signup_notification.html.erb
might look like the following:
接下来,您必须使您的邮件程序视图输出HTML
。例如,您的视图文件app/views/my_mailer/signup_notification.html.erb
可能如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" media="screen">
h3 { color: #f00; }
ul { list-style: none; }
</style>
</head>
<body>
<h3>Your account signup details are below</h3>
<ul>
<li>Name: <%= @user.name %></li>
<li>Login: <%= @user.login %></li>
<li>E-mail: <%= @user.email_address %></li>
</ul>
</body>
</html>
As you can see the HTML
view can include a <style>
tag to define basic styles. Not all HTML
and CSS
is supported, especially across all mail clients, but you should definitely have sufficient formatting control over text styles.
如您所见,HTML
视图可以包含一个<style>
标签来定义基本样式。并非所有HTML
并且CSS
都受支持,尤其是在所有邮件客户端中,但您绝对应该对文本样式有足够的格式控制。
Embedding images is a bit tricker if you plan to display attached emails. If you are simply including emails from external sites, you can use an <img />
tag as you usually would in HTML
. However, many mail clients will block these images from being displayed until the user authorises it. If you need to display attached images, the Rails plug-in Inline Attachmentsmight be worth a look.
如果您打算显示附加的电子邮件,则嵌入图像有点棘手。如果您只是包含来自外部站点的电子邮件,则可以<img />
像通常在HTML
. 但是,许多邮件客户端会阻止显示这些图像,直到用户授权为止。如果您需要显示附加的图像,Rails 插件Inline Attachments可能值得一看。
For more information on Rails mailing support, the ActionMailer documentationis a great resource
有关 Rails 邮件支持的更多信息,ActionMailer 文档是一个很好的资源
回答by Mika
For the images you can just use the normal image_tag
helper after you have defined the ActionMailer::Base.asset_host = 'http://www.your-domain.com'
对于图像,您可以image_tag
在定义后使用普通助手ActionMailer::Base.asset_host = 'http://www.your-domain.com'
I use Paperclip to store my images so in my case I can add an image to an email using this.
我使用 Paperclip 来存储我的图像,所以在我的情况下,我可以使用它向电子邮件添加图像。
image_tag result.photo.url(:small)