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

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

问题描述

如何从Rails应用程序创建和发送包含图像和正确格式的电子邮件?

解决方案

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



例如,您的notifer可能如下所示:

  class MyMailer< ActionMailer :: Base 
def signup_notification(收件人)
收件人recipient.email_address_with_name
主题新帐户信息
来自system@example.com
主体:user =>收件者
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 是支持的,特别是在所有邮件客户端,但你应该肯定有足够的格式化控制文本



如果您计划显示附加的电子邮件,嵌入图片有点麻烦。如果您只是包含来自外部网站的电子邮件,您可以使用< img /> 标记,通常在 HTML 。但是,许多邮件客户端将阻止这些图像显示,直到用户授权它。如果您需要显示附加的图片,则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

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

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