PHP IMAP从Microsoft Exchange Server接收附件

时间:2014-11-17 12:42:58

标签: php outlook exchange-server imap

您好我的客户要求我使用PHP创建一个网站,使用该网站可以阅读/回复他所有帐户中的所有电子邮件。现在他在Microsoft Exchange Server上有一个公司帐户,我使用PHP imap_open从邮件服务器获取电子邮件,我可以从Gmail帐户获取电子邮件附件,但我无法从Microsoft Exchange服务器帐户检索附件。没有错误产生,所以我不确定是什么问题。我是否需要编写某种特殊代码才能从Microsoft Exchange服务器电子邮件帐户中检索附件?

尝试发送电子邮件时也存在类似问题,我可以使用Gmail SMTP详细信息从PHP系统发送电子邮件,但不能使用Microsoft Exchange服务器发送电子邮件

我还在研究,但是我会非常感激你的帮助,也许如果你能指出我正确的方向,我将能够理解我的代码有什么问题。我不太了解Microsoft Exchange服务器是什么或与Gmail等其他电子邮件提供商有什么不同,所以如果您能指出我正确的方向,我将非常感激。

附件检索代码适用于Gmail帐户

1 个答案:

答案 0 :(得分:2)

关于Outlook服务器的SMTP邮件发送问题。我有同样的问题,问题在于PORT Number。

使用Port:587代替Port:25或任何其他选项...我一直在使用此端口发送Outlook,并且每次都成功。

对于IMAP附件问题,解决方案隐藏在区分大小写...

如果我们查看通常负责获取附件的函数object imap_fetchstructure ( resource $imap_stream , int $msg_number)

我们为Gmail和其他人获得了什么:

stdClass Object
(
    [type] => 1
    [encoding] => 0
    [ifsubtype] => 1
    [subtype] => MIXED
    [ifdescription] => 0
    [ifid] => 0
    [ifdisposition] => 0
    [ifdparameters] => 0
    [ifparameters] => 1
    [parameters] => Array
        (
            [0] => stdClass Object
                (
                    [attribute] => BOUNDARY    // Notice Here...
                    [value] => b1_04114a96a39b7789f88fdabc7feadc61
                )

        )

    [parts] => Array
        (
            [0] => stdClass Object
                (
                    [type] => 0
                    [encoding] => 1
                    [ifsubtype] => 1
                    [subtype] => HTML
                    [ifdescription] => 0
                    [ifid] => 0
                    [lines] => 20
                    [bytes] => 597
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => CHARSET   // Notice Here...
                                    [value] => iso-8859-1
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [type] => 5
                    [encoding] => 3
                    [ifsubtype] => 1
                    [subtype] => JPEG
                    [ifdescription] => 0
                    [ifid] => 0
                    [bytes] => 266988
                    [ifdisposition] => 1
                    [disposition] => ATTACHMENT     // Notice Here...
                    [ifdparameters] => 1
                    [dparameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => FILENAME    // Notice Here...
                                    [value] => oIROo0jJDb-15.jpg
                                )

                        )

                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => NAME    // Notice Here...
                                    [value] => oIROo0jJDb-15.jpg
                                )

                        )

                )

        )

)

我们对OUTLOOK的看法:

 1
    [encoding] => 0
    [ifsubtype] => 1
    [subtype] => MIXED
    [ifdescription] => 0
    [ifid] => 0
    [ifdisposition] => 0
    [ifdparameters] => 0
    [ifparameters] => 1
    [parameters] => Array
        (
            [0] => stdClass Object
                (
                    [attribute] => boundary
                    [value] => b1_df2cd0669f50efc788d5aecfdded4957
                )

        )

    [parts] => Array
        (
            [0] => stdClass Object
                (
                    [type] => 0
                    [encoding] => 1
                    [ifsubtype] => 1
                    [subtype] => HTML
                    [ifdescription] => 0
                    [ifid] => 0
                    [lines] => 23
                    [bytes] => 729
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => charset    // Notice Here...
                                    [value] => iso-8859-1
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [type] => 5
                    [encoding] => 3
                    [ifsubtype] => 1
                    [subtype] => JPEG
                    [ifdescription] => 0
                    [ifid] => 0
                    [bytes] => 266988
                    [ifdisposition] => 1
                    [disposition] => attachment    // Notice Here...
                    [ifdparameters] => 1
                    [dparameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => filename    // Notice Here...
                                    [value] => cqLaQAZSei-15.jpg
                                )

                        )

                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => name    // Notice Here...
                                    [value] => cqLaQAZSei-15.jpg
                                )

                        )

                )

        )

)
?>

现在,通常在某处提取附件我们使用if语句来检查电子邮件的那部分是否有任何附件。对于此上下文,我将引用PHP IMAP Library Part 1PHP IMAP Library Part 2 ...“获取附件”功能位于教程的第2部分中。

获取附件功能:

function getAttachments($imap, $mailNum, $part, $partNum) {
    $attachments = array();

    if (isset($part->parts)) {
        foreach ($part->parts as $key => $subpart) {
            if($partNum != "") {
                $newPartNum = $partNum . "." . ($key + 1);
            }
            else {
                $newPartNum = ($key+1);
            }
            $result = getAttachments($imap, $mailNum, $subpart,
                $newPartNum);
            if (count($result) != 0) {
                 array_push($attachments, $result);
             }
        }
    }
    else if (isset($part->disposition)) {
        if ($part->disposition == "ATTACHMENT") {    // Notice here...
            $partStruct = imap_bodystruct($imap, $mailNum,
                $partNum);
            $attachmentDetails = array(
                "name"    => $part->dparameters[0]->value,
                "partNum" => $partNum,
                "enc"     => $partStruct->encoding
            );
            return $attachmentDetails;
        }
    }

    return $attachments;
}

请注意if声明中的else if声明您可以看到它与首都ATTACHMENT进行比较,但Outlook结果中没有ATTACHMENT他们拥有attachment strtoupper($part->disposition) == 'ATTACHMENT' 。因此,我建议使用:{{1}} ...

而不是那条线

通过这种方式,我们可以使用附件...

所以最终的功能应该是:

function getAttachments($imap, $mailNum, $part, $partNum) {
    $attachments = array();

    if (isset($part->parts)) {
        foreach ($part->parts as $key => $subpart) {
            if($partNum != "") {
                $newPartNum = $partNum . "." . ($key + 1);
            }
            else {
                $newPartNum = ($key+1);
            }
            $result = getAttachments($imap, $mailNum, $subpart,
                $newPartNum);
            if (count($result) != 0) {
                 array_push($attachments, $result);
             }
        }
    }
    else if (isset($part->disposition)) {
        if (strtoupper($part->disposition) == "ATTACHMENT") {
            $partStruct = imap_bodystruct($imap, $mailNum,
                $partNum);
            $attachmentDetails = array(
                "name"    => $part->dparameters[0]->value,
                "partNum" => $partNum,
                "enc"     => $partStruct->encoding
            );
            return $attachmentDetails;
        }
    }

    return $attachments;
}

我希望能解决这个问题...因为它对我有用......

谢谢...