如何用Rails创建带有CSS和图像的电子邮件? [英] How do I create email with css and images from Rails?

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

问题描述

如何创建和发送Rails应用程序的电子邮件,其中包含图像和正确的格式?就像你从Facebook那里得到的一样。

解决方案

假设你知道如何使用ActionMailer从Rails发送普通的纯文本电子邮件,要获得 HTML 电子邮件,您需要为电子邮件设置内容类型。



例如,你的notifer可能看起来像这样:

  class MyMailer< ActionMailer :: Base 
def signup_notification(收件人)
收件人recipient.email_address_with_name
主题新帐户信息
来自system@example.com
正文:用户=>收件人
content_typetext / html
end
end

注意 content_typetext / html行。这告诉ActionMailer发送内容类型为 text / html 的电子邮件,而不是默认的 text / plain 。 / p>

接下来,您必须使您的邮件视图输出 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-Typecontent =text / html; charset = utf-8>
< style type =text / cssmedia =screen>
h3 {color:#f00; }
ul {list-style:none; }
< / style>
< / head>
< body>
< h3>您的帐户注册详情位于< / h3>之下
< ul>
< li>名称:<%= @ user.name%>< / li>
< li>登录:<%= @ user.login%>< / li>
< li>电子邮件:<%= @ user.email_address%>< / li>
< / ul>
< / body>
< / html>

您可以看到 HTML 视图可以包括< style> 标签来定义基本样式。不是所有的 HTML CSS ,特别是在所有邮件客户端,但是你应该有足够的格式控制文本样式。



如果您打算显示附加的电子邮件,嵌入图像有点棘手。如果您只是包含来自外部网站的电子邮件,则可以像 HTML < img /> 标签$ C>。但是,许多邮件客户端将阻止这些图像显示,直到用户授权。如果您需要显示附加的图像,则Rails插件内联附件可能值得一看



有关Rails邮件支持的更多信息, ActionMailer文档是一个很棒的资源


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.

解决方案

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.

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         "system@example.com"
    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.

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:

<!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.

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 Attachments might be worth a look.

For more information on Rails mailing support, the ActionMailer documentation is a great resource

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

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