如何使用 Ruby on Rails 仅解析电子邮件的正文?下面是我想出的。我已经深入到身体的主题,但我想要的只是身体。我试图在不使用 TMail 或基于电子邮件的库的情况下解析它。
节目
<p>Body: <%= @text.parse_body %></p>
控制器
def show
@text = Text.find(params[:id])
end
模型
class Text < ActiveRecord::Base
attr_accessible :email
def text_input
@raw_email = email.to_s
end
def parse_body
@parse_email = @raw_email.match(/(\r\n\r\n[\W\w\S\s\D\d\b]*\r\n)/i)
@parse_body = @parse_email.to_s.gsub(/<[\W\w\S\s\D\d\b]*?>/, "")
end
结果@parse_email
\r\n\r\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\r\n<HTML>\r\n<HEAD>\r\n<META
HTTP-EQUIV=3D\"Content-Type\" CONTENT=3D\"text/html; =\r\ncharset=3Diso-8859-1\">\r\n<META
NAME=3D\"Generator\" CONTENT=3D\"MS Exchange Server version
=\r\n6.5.7654.12\">\r\n<TITLE>Test for long subject</TITLE>\r\n</HEAD>\r\n<BODY>\r\n<!--
Converted from text/plain format -->\r\n\r\n<P><FONT
SIZE=3D2>Test<BR>\r\n<BR>\r\n</FONT>\r\n</P>\r\n\r\n</BODY>\r\n" 1:"\r\n\r\n<!DOCTYPE HTML
PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\r\n<HTML>\r\n<HEAD>\r\n<META HTTP-EQUIV=3D\"Content-
Type\" CONTENT=3D\"text/html; =\r\ncharset=3Diso-8859-1\">\r\n<META NAME=3D\"Generator\"
CONTENT=3D\"MS Exchange Server version =\r\n6.5.7654.12\">\r\n<TITLE>Test for long
subject</TITLE>\r\n</HEAD>\r\n<BODY>\r\n<!-- Converted from text/plain format --
>\r\n\r\n<P><FONT SIZE=3D2>Body<BR>\r\n<BR>\r\n</FONT>\r\n</P>\r\n\r\n</BODY>\r\n
@parse_body 的结果
Body: Test for long subject Body
结果我想得到
Body