我正在使用 IMAP api 访问我的邮箱并下载附件(.wav 音频文件)(如果有)。保存附件后,我希望它是一个可以播放的有效 .wav 文件,但它给了我无效的文件。
当我解码后保存附件时(mail.attachments.first.decoded),它有以下内容:
X-MCPBodyContent: ---
duration: 5
internal_codec: alaw
external_codec: wav
type: 1
filename: /9/04/04/04/05/m_88888_56b07809-1fe6-4cf7-8328-8e9bb0bd7716
$$$$$
如果我按原样保存附件(mail.attachments.first),它具有以下内容:
Date: Tue, 18 Oct 2011 09:06:07 -0400
Mime-Version: 1.0
Content-Type: audio/wav;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=m_88888_56b07809-1fe6-4cf7-8328-8e9bb0bd7716.wav
Content-ID: <4e9d79bfa830f_7efc3f9579013bfc3648e@vavcafeserver2.mail>
WC1NQ1BCb2R5Q29udGVudDogLS0tIApkdXJhdGlvbjogNQppbnRlcm5hbF9j
b2RlYzogYWxhdwpleHRlcm5hbF9jb2RlYzogd2F2CnR5cGU6IDEKZmlsZW5h
bWU6IC85LzA0LzA0LzA0LzA1L21fODg4ODhfNTZiMDc4MDktMWZlNi00Y2Y3
LTgzMjgtOGU5YmIwYmQ3NzE2CiQkJCQk
这是代码片段:
require 'net/imap'
imap = Net::IMAP.new('the_url', 143, false, nil, false)
imap.login('username', 'password')
imap.select('Inbox')
# All msgs in a folder
msgs = imap.uid_search(["ALL"])
# Read each message
msgs.each do |uid|
_body = imap.uid_fetch(uid, "RFC822")[0].attr["RFC822"]
require 'mail'
mail = Mail.new(_body)
attachment = mail.attachments.first
fn = attachment.filename
begin
File.open( fn, "w+b", 0644 ) { |f| f.write attachment.decoded}
rescue Exception => e
puts "Error : Unable to save data for #{fn} because #{e.message}"
end
end
imap.logout
请让我知道如何以正确的格式获取附件。
任何帮助将不胜感激。
谢谢