如何使用css创建电子邮件,以及如何从Rails创建图像?

时间:2021-10-01 07:13:04

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上看到的那样。

3 个解决方案

#1


31  

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

注意content_type“text/html”行。这告诉ActionMailer发送一个包含文本/html内容类型的电子邮件,而不是默认的文本/纯文本。

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:

接下来,您必须使您的mailer视图输出HTML。例如,您的视图文件应用程序/视图/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视图可以包含一个

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.

如果你打算显示附加的电子邮件,嵌入图片会有点麻烦。如果您仅仅包括外部站点的电子邮件,您可以使用如何使用css创建电子邮件,以及如何从Rails创建图像?标记,就像您通常在HTML中使用的那样。然而,许多邮件客户端将阻止这些图像显示,直到用户授权它。如果您需要显示附加的图像,那么Rails插件内联附件可能值得一看。

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

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

#2


1  

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'

对于图像,您可以在定义ActionMailer::Base之后使用普通的image_tag helper。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.

我用回形针来存储我的图片,这样我就可以在邮件中添加一个图片。

image_tag result.photo.url(:small)

#3


0  

Read How to Create Great HTML Emails with CSS , it's a good start. All emails need to follow HTML 3.0!

阅读如何用CSS创建好的HTML电子邮件,这是一个好的开始。所有的邮件都需要遵循HTML 3.0!

#1


31  

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

注意content_type“text/html”行。这告诉ActionMailer发送一个包含文本/html内容类型的电子邮件,而不是默认的文本/纯文本。

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:

接下来,您必须使您的mailer视图输出HTML。例如,您的视图文件应用程序/视图/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视图可以包含一个

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.

如果你打算显示附加的电子邮件,嵌入图片会有点麻烦。如果您仅仅包括外部站点的电子邮件,您可以使用如何使用css创建电子邮件,以及如何从Rails创建图像?标记,就像您通常在HTML中使用的那样。然而,许多邮件客户端将阻止这些图像显示,直到用户授权它。如果您需要显示附加的图像,那么Rails插件内联附件可能值得一看。

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

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

#2


1  

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'

对于图像,您可以在定义ActionMailer::Base之后使用普通的image_tag helper。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.

我用回形针来存储我的图片,这样我就可以在邮件中添加一个图片。

image_tag result.photo.url(:small)

#3


0  

Read How to Create Great HTML Emails with CSS , it's a good start. All emails need to follow HTML 3.0!

阅读如何用CSS创建好的HTML电子邮件,这是一个好的开始。所有的邮件都需要遵循HTML 3.0!