gpt4 book ai didi

ruby-on-rails - 在处理电子邮件回复时,我怎样才能忽略任何电子邮件客户端细节和历史记录?

转载 作者:数据小太阳 更新时间:2023-10-29 07:04:57 25 4
gpt4 key购买 nike

我有一个通过 IMAP 处理传入电子邮件的 Rails 应用程序。当前使用一种方法来搜索 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

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

  1. 我想知道像我在 37 signals 应用程序中看到的那样,在邮件顶部放置一些“---请在此行上方回复---”文本是否合理。

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

最佳答案

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

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

这是一个方法,它接受一个 TMail 对象并提取消息的响应部分并将其与发送它的电子邮件客户端/服务一起返回。它假定您在常量 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

关于ruby-on-rails - 在处理电子邮件回复时,我怎样才能忽略任何电子邮件客户端细节和历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/824205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com