如何在php中保存电子邮件的附件?
我正在检索电子邮件,然后解析到数据库中。
但问题是我能够检索电子邮件,但附件部分也显示在浏览器上。我需要将此附件部分保存到某个位置。附件的格式是文本/纯文本。
这是我的代码
<?php
$inbox=imap_open("{xyz.com:995/pop3/ssl/novalidate-cert}INBOX", "username", "password");
$count = imap_num_msg($inbox);
for($i = 1; $i <= 1; $i++) {
$raw_body = imap_body($inbox, $i);
echo $raw_body;
imap_delete($inbox, 1);
}
imap_expunge ($inbox);
?>
,我检索了一封电子邮件
,但我不知道如何保存附件。 当我使用 imap_body
时,附件也会显示在电子邮件正文下方。
那么我应该如何将这两个分开..
I am retrieving the emails and then parsing into the database.
But the problem is i am able to retrieve the email but the attachment part is also shown on the browser.I need to save this attachment part to the some location .The format of attachment is text/plain.
here is my code
<?php
$inbox=imap_open("{xyz.com:995/pop3/ssl/novalidate-cert}INBOX", "username", "password");
$count = imap_num_msg($inbox);
for($i = 1; $i <= 1; $i++) {
$raw_body = imap_body($inbox, $i);
echo $raw_body;
imap_delete($inbox, 1);
}
imap_expunge ($inbox);
?>
here i have retrievd a email
But i dont know how to save the attachment .
The attachment is also shown below the body of the email when i am using the imap_body
.
So how should i separate these two..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在您知道如何从 Lars 回复的电子邮件源中提取附件了。
我想您可能还需要解码附件。
为此,您需要知道它是如何编码的。
1.base64_encode()
2. chunk_split()
对文件进行编码的代码如下所示:
结果如下:
现在要对其进行解码,您需要再次反向执行两件事。
1. 取出编码部分并去掉所有行,这样你就有了一个单行字符串
2.解码
..当然还有将文件保存到磁盘:)
Well now you know how to extract the attachments from the email source from Lars response.
I think you might also need to decode the attachment.
To do that you need to know how it was coded.
1. base64_encode()
2. chunk_split()
The code to encode a file looks like this:
The result is something like this:
Now to decode it you need to do the 2 things again in reverse.
1. take the encoded part and strip all lines so you have a single line string
2. decode it
.. and of course save file to disk :)
通过使用 imap_fetchstruct() 分析邮件,您可以获得一个列表到邮件的不同部分。通过 imap_fetchstruct(),您实际上可以将感兴趣的部分你。查看后面的文档以获取注释中的示例代码。
by analysing the mail with imap_fetchstructure() you can get a list to the different parts of the mail. With a imap_fetchstructure(), you actually take the parts interesting to you. Have a look at the latter documentation for an example code in the comments.