php - 在 PHP 中使用 IMAP 下载附件到目录,随机工作

标签 php imap attachment

我在网上找到了 PHP 代码,可以从这里使用 IMAP 将附件下载到目录。 http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html

我稍微修改了一下

        $structure = imap_fetchstructure($mbox, $jk);
        $parts = ($structure->parts);

        $structure = imap_fetchstructure($mbox, $jk);
        $parts = ($structure);

让它正常运行,否则我得到一个关于 stdClass 没有定义名为 $parts 的属性的错误。这样做,我能够下载所有附件。不过最近我又试了一次,还是不行。好吧,它没有工作 6 次,工作了 7 次,然后就没有工作了。我认为这与我搞砸了部件处理有关,因为 count($parts) 一直为每条消息返回 1,所以它没有找到我认为的任何附件。

由于它在某一时刻毫无问题地下载了附件,因此我确信事情正在搞砸的地方就在这里。在此代码块之前是一个 for 循环,它遍历框中的每条消息,之后是一个循环,它只遍历每个 imap 结构的 $parts。感谢您的任何帮助,您可以提供。我查看了 php.net 上的 imap_fetchstructure 页面,无法弄清楚我做错了什么。

编辑:我只是在输入我的问题后仔细检查了文件夹,它全部弹出。我觉得我快疯了。在我开始输入之前的几分钟内,我没有运行代码,对我来说,触发需要这么长时间是没有意义的。我的邮箱中有大约 800 条消息,但我认为它在 PHP 的最后打印了我的声明,所有文件创建工作都已完成。

最佳答案

这是完美的工作答案,试试这个。

此示例运行正常,并且可以毫无问题地下载所有附件。

<?php

set_time_limit(3000); 

/* connect to gmail with your credentials */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'YOUR_USERNAME'; 
$password = 'YOUR_PASSWORD';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

$emails = imap_search($inbox, 'FROM "abc@gmail.com"');

/* if any emails found, iterate through each email */
if($emails) {

    $count = 1;

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) 
    {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);

        $message = imap_fetchbody($inbox,$email_number,2);

        /* get mail structure */
        $structure = imap_fetchstructure($inbox, $email_number);

        $attachments = array();

        /* if any attachments found... */
        if(isset($structure->parts) && count($structure->parts)) 
        {
            for($i = 0; $i < count($structure->parts); $i++) 
            {
                $attachments[$i] = array(
                    'is_attachment' => false,
                    'filename' => '',
                    'name' => '',
                    'attachment' => ''
                );

                if($structure->parts[$i]->ifdparameters) 
                {
                    foreach($structure->parts[$i]->dparameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'filename') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object->value;
                        }
                    }
                }

                if($structure->parts[$i]->ifparameters) 
                {
                    foreach($structure->parts[$i]->parameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'name') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['name'] = $object->value;
                        }
                    }
                }

                if($attachments[$i]['is_attachment']) 
                {
                    $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);

                    /* 3 = BASE64 encoding */
                    if($structure->parts[$i]->encoding == 3) 
                    { 
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    /* 4 = QUOTED-PRINTABLE encoding */
                    elseif($structure->parts[$i]->encoding == 4) 
                    { 
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }

        /* iterate through each attachment and save it */
        foreach($attachments as $attachment)
        {
            if($attachment['is_attachment'] == 1)
            {
                $filename = $attachment['name'];
                if(empty($filename)) $filename = $attachment['filename'];

                if(empty($filename)) $filename = time() . ".dat";
                $folder = "attachment";
                if(!is_dir($folder))
                {
                     mkdir($folder);
                }
                $fp = fopen("./". $folder ."/". $email_number . "-" . $filename, "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);
            }
        }
    }
} 

/* close the connection */
imap_close($inbox);

echo "all attachment Downloaded";

?>

关于更多,见链接

http://www.codediesel.com/php/downloading-gmail-attachments-in-php-an-update/

关于php - 在 PHP 中使用 IMAP 下载附件到目录,随机工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2649579/

相关文章:

php - CakePHP 电子邮件插件 - 使用自定义服务器

Python 脚本作为 cron 作业 - 如何将输出格式化为带有附件的电子邮件?

JavaMail 检查邮件内容 gmail IMAP

java - 使用 Javamail 访问 Microsoft Exchange 邮箱(IMAP、MS Exchange)

image - 在 Wordpress 的主页上显示随机帖子附件

java - 即使调用 file.delete() 方法,图像也不会被删除

php - 我怎样才能遍历这个数组?

php - PHP 5.3 之前的静态继承

php - 使用 Javascript 和 PHP 将数据从 NCBI 数据库传输到另一个站点

php - 如何使按钮删除我的数据库中也在我的 HTML 中的行?