Search code examples
phpemailsymfony

Retrieve and parse emails from (POP3 or IMAP) mailbox in Symfony2


I am looking for a package/bundle that can access an email inbox, retrieve the emails in the inbox and parse them (Sender Email, Subject, Body, Attachments) for processing in my Symfony2 application. The idea is a "helpdesk" where people can email a specific email address and a cron job will run through the email inbox and convert emails into helpdesk tickets (with attachments).

Before anyone tells me to Google or something equally unhelpful, please note that I have spent over an hour on Google as well as gone over more than 100 StackOverflow threads looking for something that can help me with this. Most of the content involves sending emails and not retrieving them. I have built a pure PHP parser before and it was a nightmare (as almost every email client composes the emails a little differently).

I found https://packagist.org/packages/lasso/mail-parser-bundle, but it seems to only be for Zend. I have also found https://github.com/iJanki/MailMimeDecodeBundle, but it seems to still be a work in progress and there is no documentation to speak of. Additionally, I found Correct way to retrieve mails by IMAP in symfony2, but that looks just like the start of the nightmare I had last time when I built one from scratch.

I would appreciate any suggestions or pointers from anyone who has implemented something similar or has been in a similar situation.


Solution

  • I have found a PHP class that does exactly what I wanted and neatly parses the emails into fromAddress, subject, body and attachments. It even saves the attachments to specified location on your server.

    https://github.com/barbushin/php-imap

    While it is not a Symfony2 Bundle, it is very easy to integrate into Symfony by supplying the class with a namespace and then using it in your desired controller.