如何在php中保存电子邮件的附件?

发布于 2024-12-03 16:01:34 字数 551 浏览 0 评论 0原文

我正在检索电子邮件,然后解析到数据库中。

但问题是我能够检索电子邮件,但附件部分也显示在浏览器上。我需要将此附件部分保存到某个位置。附件的格式是文本/纯文本。

这是我的代码

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魄砕の薆 2024-12-10 16:01:34

现在您知道如何从 Lars 回复的电子邮件源中提取附件了。
我想您可能还需要解码附件。
为此,您需要知道它是如何编码的。
1.base64_encode()
2. chunk_split()

对文件进行编码的代码如下所示:

<?php
$body .= "--".$boundary1 . $this->line;
$body .= "Content-Type: " . $file_type . "; name=\"" . $file_name . "\"" . $this->line;
$body .= "Content-Transfer-Encoding: base64" . $this->line;
$body .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"; size=" . $file_size . ";" . $this->line;
$body .= $this->line; // empty line

$fp = fopen($file_url, 'r');
do {
  $data = fread($fp, 8192);
  if (strlen($data) == 0) break;
  $content .= $data;
}
while (true);

$body .= chunk_split(base64_encode($content));
$body .= $this->line;
$body .= $this->line;
?>

结果如下:

--ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
Content-Type: text/plain; name="sample.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="sample.txt"; size=123;

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==

现在要对其进行解码,您需要再次反向执行两件事。
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:

<?php
$body .= "--".$boundary1 . $this->line;
$body .= "Content-Type: " . $file_type . "; name=\"" . $file_name . "\"" . $this->line;
$body .= "Content-Transfer-Encoding: base64" . $this->line;
$body .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"; size=" . $file_size . ";" . $this->line;
$body .= $this->line; // empty line

$fp = fopen($file_url, 'r');
do {
  $data = fread($fp, 8192);
  if (strlen($data) == 0) break;
  $content .= $data;
}
while (true);

$body .= chunk_split(base64_encode($content));
$body .= $this->line;
$body .= $this->line;
?>

The result is something like this:

--ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
Content-Type: text/plain; name="sample.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="sample.txt"; size=123;

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==

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 :)

清醇 2024-12-10 16:01:34

通过使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文