Aditya
Aditya

Reputation: 405

PHP IMAP Receive Attachment from Microsoft Exchange Server

Hello my client has asked me to make a website using PHP using which he can read/reply all his emails from all his accounts. Now he has a company account on Microsoft Exchange Server, I am using PHP imap_open to get emails from the mail-servers, I am able to get Email Attachments from Gmail account but I am not able to retrieve attachments from Microsoft Exchange server accounts. No error is getting generated so I am not sure what is wrong. Do I need to write some kind of special code in order to retrieve Attachments from Microsoft Exchange server email accounts?

Also a similar problem persists when trying to send emails, I am able to send emails from the PHP system using Gmail SMTP details, but not with Microsoft Exchange server

I am still researching but a little help would be much appreciated, maybe if you can just point me in the right direction I'll be able to understand what is wrong with my code. I don't know much about what Microsoft Exchange server is or how it is different from other email providers like Gmail, so I would really appreciate if you can just point me in the right direction.

The attachment retrieval code is working fine for Gmail accounts

Upvotes: 0

Views: 1291

Answers (1)

TipuZaynSultan
TipuZaynSultan

Reputation: 783

Regarding SMTP mail sending problem for outlook server. I had the same problem, the problem is with the PORT Number.

Use Port:587 instead of Port:25 or any other options... I have been sending from Outlook with this port and I am successful every time.

And For IMAP attachment issue the solution is hidden in the case sensitivity...

If we look at the function object imap_fetchstructure ( resource $imap_stream , int $msg_number) which is generally responsible for fetching attachments.

What we get for Gmail And Others:

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
                                )

                        )

                )

        )

)

What We Get for 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
                                )

                        )

                )

        )

)
?>

So now, generally for fetching attachments somewhere we use an if statement for checking if there is any attachments in that part of the email. For this context I would refer to PHP IMAP Library Part 1 And PHP IMAP Library Part 2... The Fetching Attachment function is in the Part 2 of the Tutorial.

Get Attachment Function:

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;
}

Notice the if statement inside the else if statement You can see it is comparing with capital ATTACHMENT but Outlook result has no Capital ATTACHMENT they have attachment. So instead of that line I'll recommend using: strtoupper($part->disposition) == 'ATTACHMENT'...

In this way we have the attachment working...

So the final function should be:

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;
}

I hope that will fix the problem... Because it did for me...

Thank you...

Upvotes: 2

Related Questions