Search code examples
phpemailsmtpattachmentmime

How to get XML/TXT attachments from diverse email sources in PHP?


Once the SMTP/MIME text is obtained, it is hard to tell how complex it can be to parse/handle emails coming from all kind of random SMTP/MIME programs.

For the particular pose of this question, obtaining header information is completly irrelevant. Only attachments are needed.

¿Is out there any bullet proof solution for this?

Thank you


Solution

  • You will need a MIME parser to process MIME data. In PHP, the Mailparse extension can do this for you. It's a fairly basic library, so a wrapper like php-mime-mail-parser can greatly simplify things. In any case, assuming Mailparse, the basic outline of the process is the following: load/parse the email, then enumate all parts and subparts where the following conditions are met:

    • The Content-Disposition header for the part is attachment.
    • The decoded filename parameter of the Content-Dispositon header ends with the literal .xml or .txt. Alternatively, you can rely on the Content-Type header for type detection (text/XML and text/plain, respectively), but that will be less reliable, because it depends on the email sender finding out the right MIME type for the attached document.

    Once the attachments parts are found, they can be decoded from MIME representation using the PHP-provided functions for BASE64 or quoted-printable decoding.