PHP script to emulate POP3 login & retrieval of email from MS Exchange 2010

sunhux
sunhux used Ask the Experts™
on
I currently has a Cerberus application (it's sort of CRM / Call centre tool).
It uses POP3 to login to a couple of my corporate MS Exchange email
accounts to download those emails & then clear/delete those mails from
the Exchange server.

I won't be allowed to use POP3 soon.  Anyone has any PHP or has some
idea on how to write PHP script to emulate POP3 functionality ie download
& clear away the emails.

Cerberus vendor is not providing the script but they told me it's possible to
write such a PHP script.

At least some attempt to write such a script is appreciated even if the
script did not ultimately work.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:

Google will provide some sample of PHP script to do login but I'll need
something specifically for login to MS Exchange & download emails :

http://www.google.com.sg/#hl=ms&source=hp&biw=954&bih=652&q=sample+PHP+code+for+login&aq=f&aqi=g1&aql=&oq=&fp=3e79c0b357f7acbf
I've had quite a lot of success implementing the following class for mail management.  It builds up SOAP requests to send to Exchange Web Services (EWS).  You can list messages, retrieve them, delete them, move them, send messages... It's a much more powerful approach than using POP3.

http://www.howtoforge.com/talking-soap-with-exchange

If you're interested in going this route, I'd be happy to talk to you further.

Author

Commented:

Yes, I'm interested in knowing further : actually I have 2 apps (Cerberus & BMC Remedy)
that need to download+clear emails from the Exchange server as our corporate has issued
a directive that POP3 on the Exchange server will be discontinued due to security policy.

I think one technical hurdle is whether SOAP could store the downloaded mails in the
required repositories or mail storage formats that are required by Cerberus & Remedy
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

I'm not familar with either of these apps.  When requesting through the PHP class linked to above, the results come back as a plain text stdArray, so the question would be whether you can then rehash the content into the required form and push it back into these systems.  I would then run this on a cron job at whatever frequency is required.  I hope this helps.

Author

Commented:

Currently there's a cron job that runs a php job every minute to download the mails
from MS Exchange but I'll collect the code tomorrow (on leave today) :
/usr/bin/wget --no-check-certificate -O /tmp/cron.out -o /tmp/cron.out1 https://localhost/cerberus-gui/cron.php

Let me know how we want to approach this

Author

Commented:

attached the cron.php script :

I'm not sure if this uses POP3 protocol but would you be able to
amend it to use some other methods (say http service or SOAP or ...)
cronphp.txt
Most Valuable Expert 2011
Top Expert 2016
Commented:
Got a Neglected Question Alert on this one.  I'd like to suggest a slightly different approach.  I am not sure if this will meet your needs but I have used it in some applications with good results (It is a design pattern frequently found in help-ticket applications).  The thing is called an email pipe and it works like this.  An email address or perhaps several email addresses are set aside for automated processing.  Whenever a message arrives for one of the pipe addresses, instead of storing the email message in a mailbox file, the associated email pipe script is started and the entire message, including headers and body can be read from the standard input.  The pipe script can examine and parse the messages, update the data bases, re-send the messages to other email addresses, POST data to start asynchronous scripts, etc.

Since I have only implemented an email pipe on a Linux system I cannot give you the specifics for the Microsoft OS, but there may be a similar capability.  The code snippet contains an example of the PHP code used by a teaching example of an email pipe script.  

Best of luck with it, ~Ray
#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = '';


// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

Author

Commented:
Thanks v much.

Let me know how I should run your script?  Run every minute from a
cron?  How do I specify the parameters (ie the MS Exchange server
to download from & the mailbox name)


I have an application (called Cerberus) that runs on Linux that uses POP3
method to download the emails : I can download it from MS Exchange
(preferred) or a Linux Postfix (as I've managed to get my Postfix to
receive / send emails).  I also have another app (Remedy) which uses
Windows POP3 to download from MS Exchange.

So the crucial thing is for the script to simulate POP3 action
Most Valuable Expert 2011
Top Expert 2016
Commented:
You don't run this script - it gets run for you by the email handler and it only runs when email arrives for the defined email address.  There is no cron involved.

I can only help with the part that I understand, and that is the interface with cPanel.  It's documented in the code comments.

Author

Commented:

In MS Exchange, how does the email handler look like?

For postfix, how does its email handler look like?

Author

Commented:
Ok

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial