Best Way to Access Exchange Using PHP

Best way to access Exchange using PHP?

Update as of 2020:

Over a decade since this question and things have moved on. Microsft now has a Rest API that will allow you to easily access this data.


Original Answer

I have not used PHP to do this but have experience in using C# to achieve the same thing.

The Outlook API is a way of automating Outlook rather than connecting to Exchange directly. I have previously taken this approach in a C# application and it does work although can be buggy.

If you wish to connect directly to the Exchange server you will need to research extended MAPI.

In the past I used this wrapper MAPIEx: Extended MAPI Wrapper.

It is a C# project but I believe you can use some .NET code on a PHP5 Windows server. Alternatively it has a C++ core DLL that you may be a able to use. I have found it to be very good and there are some good example applications.


Sorry for the delay no current way to keep track of posts yet.

I do agree adding more layer on to your application and relying on 3rd party code can be scary (and rightfully so.)

Today I read another interesting post tagged up as MAPI that is on a different subject. The key thing here though is that it has linked to this important MS article. I have been unaware of the issues until now on using managed code to interface to MAPI although the C++ code in the component should be unaffected by this error as it is unmanaged.

This blog entry also suggests other ways to connect to MAPI/Exchange server. In this case due to these new facts http://us3.php.net/imap may be the answer as suggested by the other user.

How can I retrieve groups and contacts from microsoft exchange server using php?

I believe the new version of Exchange Server has a web services interface that you could access to retrieve information.

http://msdn.microsoft.com/en-us/library/bb204119(v=exchg.140).aspx

PHP IMAP Receive Attachment from Microsoft Exchange Server

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...

php imap_open with microsoft exchange (outlook web app)

After a week of waiting it turns out that responsible IT-department has now finally configured OWA correctly. I can use '{mail.company.com:143}' to log in the IMAP mailbox.



Related Topics



Leave a reply



Submit