我需要阅读每封邮件的正文来找到一个具体的数字。
我代码:
imap.search(["ON","20-Jan-2021"]).each do |message_id|
body = imap.fetch(message_id, "BODY[]")[0].attr["BODY[]"]
mail = Mail.new(body)
pp mail.body
end
的结果类似于
"L0Rlc3RbOCAwIFIvRml0SCA4MzJdL1BhcmVudCAxNiAwIFIvVGl0bGUoKT4+CmVuZG9iagoxOCAwrn" +
"IG9iajw8L1R5cGUvQ2F0YWxvZy9QYWdlcyAxMyAwIFIvT3V0bGluZXMgMTYgMCBSPj4KZW5kb2Jqrn" +
"CjE5IDAgb2JqPDwvQ3JlYXRpb25EYXRlKEQ6MjAyMTAxMjAxMDQ2NDUrMDInMDAnKS9Qcm9kdWNlrn" +
我已经尝试使用:
mail.body.decoded # same result like above.
mail.text_part.decoded # error: undefined method `decoded' for nil:NilClass
mail.text_part.body.to_s # error: undefined method `body' for nil:NilClass
我会检查正文的内容是否使用Content-Transfer-Encoding,这很可能是base64。Ruby提供了一个base64模块,使其易于解码:
require "base64"
# IMAP authentication and whatnot
imap.search(["ON","20-Jan-2021"]).each do |message_id|
body = imap.fetch(message_id, "BODY[]")[0].attr["BODY[]"]
body = Base64.decode64(body)
mail = Mail.new(body)
pp mail.body
end
一般情况下,您提供的文本生成
/Dest[8 0 R/FitH 832]/Parent 16 0 R/Title()>>
endobj
18 0 obj<</Type/Catalog/Pages 13 0 R/Outlines 16 0 R>>
endobj
19 0 obj<</CreationDate(D:20210120104645+02'00')/Produce
从base64解码。