简体   繁体   中英

Use PHP to parse a confirmation email?

I'm integrating my website with a third party system. Here's the workflow between my website and the third party system:

1) From my website, I use PHP to automate upload of a CSV file to the third party system.

2) After the third party system receives my file, it will conduct a few operations.

3) The third party system will email john@mydomain.com a list of successful and failed operations including any error messages.

4) My website needs to record the list of successful/failed operations in the confirmation email.

5) My website performs operations based on which items were successful/failed in the confirmation email.

First question: What do I need to research to be able to implement #4?

Second question: Is parsing a confirmation email the best way to record the successful/failed operations of the 3rd party system? It feels like a problematic way of getting to step 5 (for example, what if the language in the email changes? Then i have to rewrite the parser".

Well, obviously a better way would be some sort of push from the 3rd party system to a script on your end, following a defined convention of parameters. If there is any chance to do so, talk to the 3rd party whether such a way can't be implemented somehow. It could be very little work on their end.

To parse E-Mail: The standard method in PHP to check POP3 and IMAP mailboxes is here

What you need to parse the E-Mails themselves depends on what they look like. Probably regular expressions will be involved.

I've done a similar setup to this. Setting up a mailbox to receive the output from another system is certainly not the most fail proof setup, but sometimes there are no other options.

Zend_Mail makes this a breeze. My whole script for checking the mail inbox was just a few lines:

    $mail = new Zend_Mail_Storage_Imap(array(
        'host' => $config->reports->email->incoming,
        'user' => $config->reports->email->address,
        'password' => $config->reports->email->password
    ));

    foreach ($mail as $message) {
        $message->getContent(); // Do something with the message content
        if ($message->isMultipart()) {
            $part = $message->getPart(2); // Deal with attachment, 
                                          // if your output is an  
                                          // attachment
            if ($part->contentTransferEncoding=='base64') {
                $raw = base64_decode($part->getContent());
                // Do something with the attachment data
            }
        }
    }

Since you're parsing HTML, passing this off to a DOM parser would be the next logical step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM