仅发送纯文本电子邮件,正文中包含 Rails mime 部分
Posted
技术标签:
【中文标题】仅发送纯文本电子邮件,正文中包含 Rails mime 部分【英文标题】:Sending ONLY plain text e-mail, Rails mime part included in body 【发布时间】:2012-10-31 05:17:02 【问题描述】:我有一个发送许多通知电子邮件的 Rails 应用程序。我们希望让用户选择纯文本与 html。
最初我们的愿望总是多部分的,所以我们没有设置内容类型,而只是同时拥有 email.html.erb 和 email.text.erb 视图,它们都将通过发送并创建一个多部分。
但是现在,当我们根据用户偏好指定 content_type 时,它确实将自己正确设置为多部分或纯文本,但 html 模板作为纯文本包含在纯文本电子邮件的底部。
代码如下:
邮件操作:
def client_creation(client) # When client checks in for the first time
@client = client
content_type = client.company.html_emails ? nil : 'text/plain'
email_prefix = client.company.brand ? client.company.brand.email_subject_prefix : 'MonitoringClient'
mail(to: @client.company.administrative_reporting_address, subject: "[#email_prefix][Initial Check-in] #@client.machine_name.html_safe of #@client.clientgroup.name", content_type: content_type)
end
上面的代码检查客户公司是否打开了 html_emails,如果是,则将 content_type 设置为纯文本/文本。结果如下:
IRB 输出:
#<Mail::Message:70269523266340, Multipart: false, Headers: <Date: Sun, 11 Nov 2012 09:09:43 -0600>, <From: notifications@monitoringclient.com>, <To: ommitted>, <Message-ID: <509fbfb7e0c4e_11e113fe8e4435b4084183@Jonathan.local.mail>>, <Subject: [Initial Check-in] Jonathan of hivemind labs>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
到目前为止,Multipart 是错误的 content_type 是 text/plain 但这是生成的电子邮件正文:
----==_mimepart_509fbfb7ddb8b_11e113fe8e4435b4083853
Date: Sun, 11 Nov 2012 09:09:43 -0600
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <509fbfb7de92a_11e113fe8e4435b408399f@Jonathan.local.mail>
First check-in notification for Jonathan.
=========================== CLIENT DETAILS ===========================
MACHINE INFORMATION:
MacBookAir4,1
REPORT TIME:
Nov 11, 2012 8:17am
CLIENT GROUP:
Hivemind Labs
MACHINE NAME:
Jonathan
LAST USER:
Jonathan
SERIAL NUMBER:
ommited
MACHINE SPECIFICATIONS:
RAM:
4 GB
PROCESSOR INFO:
1x
1.8 GHz,
2 core
Intel Core i7
CLIENT VERSION:
5.0.5
UPTIME AT LAST CHECKIN:
4 days, 21 hours, 11 mins
----==_mimepart_509fbfb7ddb8b_11e113fe8e4435b4083853
Date: Sun, 11 Nov 2012 09:09:43 -0600
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <509fbfb7e0496_11e113fe8e4435b40840ce@Jonathan.local.mail>
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body leftmargin="0" margin topmargin="0" margin offset="0">
<table style="box-shadow: 0px 0px 28px -10px #222; margin: 10px auto 20px" border="0" cellpadding="0" cellspacing="0" id="templateContainer" >
<tr>
<td align="center" valign="top">
<table border="0" cellpadding="0" cellspacing="0" id="templateHeader">
<tr>
<td class="headerContent">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left" valign="top" style="width:600px">
<table border="0" cellpadding="10" cellspacing="0" id="templateBody">
<tr>
<td valign="top" class="bodyContent">
<table border="0" cellpadding="0" cellspacing="0" >
<tr>
<td valign="top">
<div class="mainContent">
<style type="text/css" media="screen">
table.info tr td:first-child
width:40%;
color: #ffffff;
text-shadow:1px 1px 1px #555;
background-color: #2C2C2C;
background-image: -moz-linear-gradient(top, #888, #777);
background-image: -ms-linear-gradient(top, #888, #777);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#888), to(#777));
background-image: -webkit-linear-gradient(top, #888, #777);
background-image: -o-linear-gradient(top, #888, #777);
background-image: linear-gradient(top, #888, #777);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#888', endColorstr='#777', GradientType=0);
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25),inset 0 -1px 0 rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25),inset 0 -1px 0 rgba(0, 0, 0, 0.1);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25)
legend + .page-actions.page-actions-small
margin-top: -49px;
</style>
<legend>Client Details</legend>
<div id="" class="page-actions page-actions-small pull-right">
<a href="#" class="btn btn-mini">Complete Overview</a>
</div> <!-- close: page-actions -->
<table style="" class="table table-bordered table-striped table-condensed info">
<tbody>
<tr><td>Client Group</td><td>Hivemind Labs</td></tr>
<tr><td>Serial Number</td><td></td></tr>
<tr><td>Machine Name</td><td> Jonathan</td></tr>
<tr><td>Last User</td><td>Jonathan</td></tr>
<tr><td>Operating system</td><td>OS X 10.8.2 (12C60)</td></tr>
<tr><td>Installed RAM</td><td>4 GB</td></tr>
<tr><td>Model Details</td><td>MacBookAir4,1</td></tr>
<tr><td>Uptime</td><td>4 days, 21 hours, 11 mins</td></tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" valign="top">
<table border="0" cellpadding="5" cellspacing="0" >
<tr>
<td style="padding: 0 0 20px 20px">
<small class="copyright"> All Rights Reserved.</small>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
----==_mimepart_509fbfb7ddb8b_11e113fe8e4435b4083853--
为什么所有的 mime 部分数据,为什么 html 内容显示为文本?我想要一个纯文本的电子邮件。这不是如何实现的吗?
我注意到的一件事是电子邮件的 html 部分显示 content_type: 'text/html'。也许这就是它被包括在内的原因?可以预防吗?
这个问题似乎很接近,但似乎这个人只是想弄清楚如何发送多部分,这不是我们的问题: Rails mailer mimepart visible as text in message body
我也尝试将我们的文本模板更改为 email.text.plain.erb,但在一些地方看到了它,但也没有用。
如果有人好奇,这里是电子邮件标题:
Delivered-To: jon@hivemindlabs.com
Received: by 10.60.124.225 with SMTP id ml1csp294562oeb;
Sun, 11 Nov 2012 07:09:46 -0800 (PST)
Received: by 10.236.122.143 with SMTP id t15mr16902572yhh.26.1352646586007;
Sun, 11 Nov 2012 07:09:46 -0800 (PST)
Return-Path: <info@hivemindlabs.com>
Received: from mail-gh0-f181.google.com (mail-gh0-f181.google.com [209.85.160.181])
by mx.google.com with ESMTPS id p20si1615988ano.75.2012.11.11.07.09.45
(version=TLSv1/SSLv3 cipher=OTHER);
Sun, 11 Nov 2012 07:09:45 -0800 (PST)
Received-SPF: pass (google.com: domain of info@hivemindlabs.com designates 209.85.160.181 as permitted sender) client-ip=209.85.160.181;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of info@hivemindlabs.com designates 209.85.160.181 as permitted sender) smtp.mail=info@hivemindlabs.com
Received: by mail-gh0-f181.google.com with SMTP id z22so1332210ghb.26
for <jon@hivemindlabs.com>; Sun, 11 Nov 2012 07:09:45 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=google.com; s=20120113;
h=date:from:to:message-id:subject:mime-version:content-type
:content-transfer-encoding:x-gm-message-state;
bh=OFH7ScRSXxkeW0Ig+UdGXMRcNoKwEcIPDFaTVrwnnRs=;
b=i+Oq1YtnJVh3hM6bB5JgXbZGUS1KnvIsyX2zU2UCoCGtGd1DrvifYQGTPSkqbjlqg0
vArkYg9ZOApVkueLh8fU3lXocCgXy1cSQ7pnTDo5aj2YKyNj8XcshGU5PN24nKmNkJNn
AOHD75ounew3lrfL/dPGx5rfD6ROIgOORIHioPUSs9sFPUbbfu1OsPDBlAAv4zPWbBuG
tbjR1JL7DntD3sPzcHiZJ9XyajhhuYQPZ/j5f/++Bdyfqq1s8uxlZvapQlndai58KtqU
EjmDb91+C/uIc9FwcT2YE0vv3pdrIHnlGYmfyX/sB0ZDDET+dTFcCOmbqpbaAwxavRb+
GAUg==
Received: by 10.236.83.103 with SMTP id p67mr16870674yhe.78.1352646585386;
Sun, 11 Nov 2012 07:09:45 -0800 (PST)
Return-Path: <info@hivemindlabs.com>
Received: from gmail.com (ip-216-115-131-150.static.eatel.net. [216.115.131.150])
by mx.google.com with ESMTPS id u11sm3665339ane.11.2012.11.11.07.09.44
(version=TLSv1/SSLv3 cipher=OTHER);
Sun, 11 Nov 2012 07:09:45 -0800 (PST)
Date: Sun, 11 Nov 2012 09:09:43 -0600
From: info@hivemindlabs.com
To: jon@hivemindlabs.com
Message-ID: <509fbfb7e0c4e_11e113fe8e4435b4084183@Jonathan.local.mail>
Subject: [Initial Check-in] Jonathan of hivemind labs
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Gm-Message-State: ALoCoQlSNX58YFYU6Idzq84zch66CnL12QD/WrT3R8kcu0wT2jV9oppA1clUl9pHY2hta0vIMqJx
【问题讨论】:
【参考方案1】:mail(to: @client.company.administrative_reporting_address,
subject: "[#email_prefix][Initial Check-in] \
#@client.machine_name.html_safe of #@client.clientgroup.name",
content_type: content_type) do |format|
format.html if client.company.html_emails
format.text
end
由于邮件操作被称为client_creation
,这将为 HTML 部分呈现模板“client_creation.html.erb”(如果条件为真)并为文本部分呈现模板“client_creation.text.erb” . html_emails
是公司设置的标志,用于启用/禁用 html 电子邮件。更多信息参考http://guides.rubyonrails.org/action_mailer_basics.html
【讨论】:
我实际上是通过不同的方法自己解决了这个问题,但这足够相似,我相信它应该可以工作。 一点解释都没有?! @konyak以上是关于仅发送纯文本电子邮件,正文中包含 Rails mime 部分的主要内容,如果未能解决你的问题,请参考以下文章
PhpMailer 发送的电子邮件内容仅显示纯文本,不包含 HTML 样式
使用 mailkit 发送的多部分电子邮件的文本/纯文本版本未正确接收