从EML格式解析电子邮件附件的正确PHP方法 [英] Proper PHP way to parse email attachments from EML format
问题描述
我有一个文件,其中包含纯文本MIME邮件格式"的电子邮件.我不确定这是否为EML格式.电子邮件包含附件,我想提取附件并再次创建这些文件.这是附件部分的样子-
I have a file containing an email in "plain text MIME message format". I am not sure if this is the EML format. The email contains an attachment and I want to extract the attachment and create those files again. This is how the attachment part looks like -
...
...
Receive, deliver details
...
...
From: sac ascsac <sacsac@sacascsac.ascsac>
Date: Thu, 20 Jan 2011 18:05:16 +0530
Message-ID: <AANLkTimmSL0iGW4rA3tvSJ9M3eT5yZLTGsqvCvf2fFC3@mail.gmail.com>
Subject: Test attachments
To: ascsacsa@ascsac.com
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_gj5n2yx60
和ZyBmZyAKCjIKNDIzCnQ2Mwo=
之间的部分,包括
是第一个附件的内容.我想解析那些附件(文件名和内容并创建那些文件).
I can see that the part between X-Attachment-Id: f_gj5n2yx60
and ZyBmZyAKCjIKNDIzCnQ2Mwo=
, both including
is the content of the first attachment. I want to parse those attachments (file names and contents and create those files).
I got this file after parsing a dbx format file using a DBX Parser class available in PHP classes.
I searched in many places and did not find much discussion regarding this here in SO other than Script to parse emails for attachments. May be I missed some terms while searching. In that answer it is mentioned -
您可以使用边界提取 base64编码的信息
you can use the boundries to extract the base64 encoded information
但是我不确定哪些边界以及如何精确使用边界?已经必须有一些库或定义良好的方法来执行此操作.我想如果我在这里重新发明轮子的话,我会犯很多错误.
But I am not sure which are the boundaries and how exactly to use the boundaries? There already must be some libraries or some well defined method of doing this. I guess I will commit many mistakes if I try reinventing the wheel here.
推荐答案
有一个PHP Mailparse扩展,您尝试过吗?
There's an PHP Mailparse extension, have you tried it?
手动方法是逐行处理邮件.当您点击第一个Content-Type标头时(示例中的该标头): 内容类型:多部分/混合; boundary = 20cf3054ac85d97721049a465e12
The manual way would be, process the mail line by line. When you hit your first Content-Type header (this one in your example): Content-Type: multipart/mixed; boundary=20cf3054ac85d97721049a465e12
您有边界.此字符串用作您的多个部分之间的边界(这就是为什么他们将其称为多部分). 每当一行以破折号和该字符串开头时,就会开始一个新的部分.在您的示例中: --20cf3054ac85d97721049a465e12
You have the boundary. This string is used as the boundary between your multiple parts (that's why they call it multipart). Everytime a line starts with the dashes and this string, a new part begin. In your example: --20cf3054ac85d97721049a465e12
每个部分都将以标题,空白行和内容开头.通过查看标题的内容类型,可以确定哪些是附件,附件的类型和文件名. 阅读全部内容,删除空格,对它进行base64_decode,您已经获得了文件的二进制内容.这有帮助吗?
Every part will start with headers, a blank line, and content. By looking at the content-type of the headers you can determine which are attachments, what their type is and their filename. Read the whole content, strip the spaces, base64_decode it, and you've got the binary contents of the file. Does this help?
这篇关于从EML格式解析电子邮件附件的正确PHP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!