2011-01-27 194 views
0

我有一个文件包含一个电子邮件在“纯文本MIME邮件格式”。我不确定这是否是EML格式。该电子邮件包含附件,我想提取附件并再次创建这些文件。这是附件部分的样子 -正确的PHP方式来解析EML格式的电子邮件附件

... 
... 
Receive, deliver details 
... 
... 
From: sac ascsac <[email protected]> 

Date: Thu, 20 Jan 2011 18:05:16 +0530 

Message-ID: <[email protected]> 

Subject: Test attachments 

To: [email protected] 

Content-Type: multipart/mixed; boundary=20cf3054ac85d97721049a465e12 



--20cf3054ac85d97721049a465e12 

Content-Type: multipart/alternative; boundary=20cf3054ac85d97717049a465e10 



--20cf3054ac85d97717049a465e10 

Content-Type: text/plain; charset=ISO-8859-1 



hello this is a test mail. It contains two attachments 



--20cf3054ac85d97717049a465e10 

Content-Type: text/html; charset=ISO-8859-1 



hello this is a test mail. It contains two attachments<br> 


--20cf3054ac85d97717049a465e10-- 

--20cf3054ac85d97721049a465e12 

Content-Type: text/plain; charset=US-ASCII; name="simple_test.txt" 

Content-Disposition: attachment; filename="simple_test.txt" 

Content-Transfer-Encoding: base64 

X-Attachment-Id: f_gj5n2yx60 



aGVsbG8gd29ybGQKYWMgYXNj 
... 
encoded things here 
... 
ZyBmZyAKCjIKNDIzCnQ2Mwo= 

--20cf3054ac85d97721049a465e12 

Content-Type: application/x-httpd-php; name="oscomm_backup_code.php" 

Content-Disposition: attachment; filename="oscomm_backup_code.php" 

Content-Transfer-Encoding: base64 

X-Attachment-Id: f_gj5n5gxn1 



PD9waHAKCg ... 
... 
encoded things here 
... 
X2xpbmsoRklMRU5BTUVfQkFDS1VQKSk7Cgo/Pgo= 
--20cf3054ac85d97721049a465e12-- 

我可以看到X-Attachment-Id: f_gj5n2yx60ZyBmZyAKCjIKNDIzCnQ2Mwo=,既包括 之间的部分是第一个附件的内容。我想解析这些附件(文件名和内容并创建这些文件)。

我使用PHP类中可用的DBX Parser类解析dbx格式文件后得到了此文件。

我在很多地方搜索过,并没有在Script to parse emails for attachments以外的其他地方找到关于此的讨论。可能是我在搜索时错过了一些术语。在这个问题的答案中提到 -

可以使用boundries提取 编码信息

以base64但我不知道这是界限,究竟如何使用的界限?已经必须有一些库或者一些明确的方法来做到这一点。如果我尝试在这里重新发明轮子,我想我会犯很多错误。

回答

1

有一个PHP Mailparse扩展,你试过了吗?

手动的方式是,逐行处理邮件。当你打你的第一个Content-Type头(在你的例子中是这个): Content-Type:multipart/mixed; boundary = 20cf3054ac85d97721049a465e12

你有边界。该字符串用作多个部分之间的边界(这就是为什么他们称之为多部分)。 每一行都以破折号和这个字符串开始,一个新的部分开始。在你的例子中: - 20cf3054ac85d97721049a465e12

每个部分都将以标题,空白行和内容开始。通过查看标题的内容类型,您可以确定哪些是附件,它们的类型是什么以及它们的文件名。 阅读整个内容,去掉空格,对它进行base64_decode,并且获得了文件的二进制内容。这有帮助吗?

+0

绝对有帮助。谢谢,我正在尝试Mailparse扩展。 – 2011-01-28 07:30:28