在处理电子邮件回复时,如何忽略任何电子邮件客户端细节和历史记录?

tsd*_*own 6 ruby email parsing ruby-on-rails

我有一个rails应用程序,它通过IMAP处理传入的电子邮件.目前,使用一种方法在TMail对象的部分中搜索给定的content_type:

def self.search_parts_for_content_type(parts, content_type = 'text/html')
    parts.each do |part|
      if part.content_type == content_type
        return part.body
      else
        if part.multipart?
          if body = self.search_parts_for_content_type(part.parts, content_type)
            return body
          end
        end
      end
    end

    return false
 end
Run Code Online (Sandbox Code Playgroud)

这些电子邮件通常是对它首先发送的html电子邮件的回应.(原始出站电子邮件永远不会相同.)上述方法返回的正文文本包含电子邮件的完整历史记录,我想解析回复文本.

  1. 我想知道在37信号应用程序中看到的是否在邮件顶部放置一些"---请在这行以上回复---"文本是否合理.

  2. 除了为每个邮件客户端编写大量正则表达式(我还没有尝试过)之外,还有其他方法可以忽略客户端特定的电子邮件添加吗?他们似乎都在自己的位置上发表任何回复.

Kei*_*oot 9

我必须对我正在进行的项目进行电子邮件回复解析.我最终使用模式匹配来识别响应部分,因此用户不必担心在何处插入他们的回复.

好消息是实施真的不太难.困难的部分只是测试您想要支持的所有不同的电子邮件客户端和服务,并弄清楚如何识别每个.通常,您可以使用消息ID或X-Mailer或Return-Path标头来确定传入电子邮件的来源.

这是一个获取TMail对象并提取消息的响应部分并将其与发送的电子邮件客户端/服务一起返回的方法.它假定你有原始消息的From:名称和地址在常量FROM_NAMEFROM_ADDRESS.

def find_reply(email)
  message_id = email.message_id('')
  x_mailer = email.header_string('x-mailer')

  # For optimization, this list could be sorted from most popular to least popular email client/service
  rules = [
    [ 'Gmail', lambda { message_id =~ /.+gmail\.com>\z/}, /^.*#{FROM_NAME}\s+<#{FROM_ADDRESS}>\s*wrote:.*$/ ],
    [ 'Yahoo! Mail', lambda { message_id =~ /.+yahoo\.com>\z/}, /^_+\nFrom: #{FROM_NAME} <#{FROM_ADDRESS}>$/ ],
    [ 'Microsoft Live Mail/Hotmail', lambda { email.header_string('return-path') =~ /<.+@(hotmail|live).com>/}, /^Date:.+\nSubject:.+\nFrom: #{FROM_ADDRESS}$/ ],
    [ 'Outlook Express', lambda { x_mailer =~ /Microsoft Outlook Express/ }, /^----- Original Message -----$/ ],
    [ 'Outlook', lambda { x_mailer =~ /Microsoft Office Outlook/ }, /^\s*_+\s*\nFrom: #{FROM_NAME}.*$/ ],

    # TODO: other email clients/services

    # Generic fallback
    [ nil, lambda { true }, /^.*#{FROM_ADDRESS}.*$/ ]
  ]

  # Default to using the whole body as the reply (maybe the user deleted the original message when they replied?)
  notes = email.body
  source = nil

  # Try to detect which email service/client sent this message
  rules.find do |r|
    if r[1].call
      # Try to extract the reply.  If we find it, save it and cancel the search.
      reply_match = email.body.match(r[2])
      if reply_match
        notes = email.body[0, reply_match.begin(0)]
        source = r[0]
        next true
      end
    end
  end

  [notes.strip, source]
end
Run Code Online (Sandbox Code Playgroud)