1

I have for years as a side project wanted to build a nice little PHP library that can allow emails sent to a predefined email address to then be parsed with PHP and saved into a MySQL database.

Many support ticket and help desk have this functionality. You can send an email to an address and based on the email address it is from and content it will create a support ticket for a user. Also reply emails will add ticket replies to the system al from Emails sent.

This whole concept has always fascinated me and at one time I had done enough research to fully know how to do it. It's been years now though and I am not sure the best route to achieve this anymore and I am looking for information on how to do it now.

I would like to build some sort of SugarCRM plugin that uses this capability to be able to assign emails from my clients to there Account and Contact records in my CRM.

My initial search tonight has found this project PHP Mime Mail Parser here https://code.google.com/p/php-mime-mail-parser/ It states: "You will need to download the email to a file first and then pass it to MimeMailParser." So that could be helpful to find a program or method that does that as well...saves the emails to a file where I can access with PHP and then delete them. I just am not sure if that is the best and standard route of doing this?

Also this library which looks the same but on Github and recently worked on (recent to the post date of this question that is) https://github.com/eXorus/php-mime-mail-parser

1) Is there some special email software to run on the server to simplify this process?

2) How can I achieve this? I am not looking for how to read the email and parse the ID and other data in the email content...I just need help with getting the email content passed to my PHP script!

Of course, if some open source library exist that I can study or maybe even use that does this functionality, that would be amazing too but I am not aware of it yet.

Look forward to some info on the subject please?

This is a similar question to How do I parse emails in realtime as they are received however the answer on that question explains how to pipe/forward emails to a PHP script only if you are using CPanel which I am not! It is the desired action I need, just without CPanel's help.

Community
  • 1
  • 1
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 1
    I am not sure if their is a simpler solution. We us powerMTA on our mail server which allows us to process inbound email through a php script. We then use the php pear library Mail_mimeDecode to parse the actual email into an email object that we can access like $mail->body, etc. – Jacob Mar 11 '15 at 23:14
  • 1
    possible duplicate of [How do I parse emails in realtime as they are recieved](http://stackoverflow.com/questions/7541278/how-do-i-parse-emails-in-realtime-as-they-are-recieved) – showdev Mar 11 '15 at 23:17
  • install one of the many open source ticking systems that already do this ? –  Mar 11 '15 at 23:17
  • @showdev I need the same result of that question however it;s answer explains how to do it only if you are using CPanel on your server which I am not. – JasonDavis Mar 11 '15 at 23:43
  • @Dagon A ticket system is not my desired result. I am wanting to learn how to do this to implement into several projects of my own. – JasonDavis Mar 11 '15 at 23:44
  • install one and read the code? –  Mar 11 '15 at 23:45
  • 1)no special software required; depending on your email software its one line to pipe email coming in to your server to a file (php script). 2.see 1. –  Mar 11 '15 at 23:46
  • The place to start is with whatever service handles incoming mail on your server. It could be sendmail, or postfix, or exim, or... – Roger Halliburton Mar 11 '15 at 23:56

2 Answers2

2

Most MTA's have the ability to pipe incoming mail to a script. Then a script like the on that you reference in How do I parse emails in realtime as they are recieved can process incoming messages as the arrive.

As far as MTA's go, qmail (http://cr.yp.to/qmail.html) is one of the simpler MTA's to setup and manage. If you decide to go with qmail, I would recommend the site lifewithqmail.org for an excellent guide on how to install it and set it up. Once you have qmail installed and running, do the following to configure incoming messages to be piped to your PHP script:

Add a line in /var/qmail/control/rcphosts to tell qmail that it should be acting as the MX for your domain.

In /var/qmail/control/virtualdomains, add a line like so to tell qmail which user account to route mail to for this domain: domain.tld:username

create a .qmail-default file in user directory, containing a pipe to the script that you want to route the mail to: | /path/to/script

restart qmail.

You'll need to grant execute permissions for all on the script.

At this point, each incoming message should be piped to the PHP script by qmail.

Community
  • 1
  • 1
mti2935
  • 11,465
  • 3
  • 29
  • 33
  • Sounds like a nice simple solution! Question, my server currently has no mail server setup. Can Qmail be used for regular email as well as for this pipe to php script project? Or would you say I should probably use another mail server for my daily regular email? Thanks! – JasonDavis Mar 12 '15 at 04:45
  • You can use qmail for both purposes. If you also need to send outgoing mail from the server, you can use qmail for that as well. – mti2935 Mar 12 '15 at 09:51
  • 1
    Another option that you might want to consider - there are a number of third-party services that will receive incoming email messages, parse them, and post info and content of the messages to a URL via https. See http://www.mailgun.com/inbound-routing or https://sendgrid.com/blog/sendgrids-parse-api-parsing-incoming-email-is-now-faster-and-easier/. – mti2935 Mar 12 '15 at 09:58
  • Good idea, I had forgot about the services. I prefer to self host but I will check this out as well, thanks! – JasonDavis Mar 12 '15 at 23:26
0

I'm using Parseur.com to receive incoming email messages, parse them, and post info and content of the messages to a URL via http (json). My PHP script extracts the desired data such as the Sender, Subject, and saves the Attachments to my server. Free plan processes up to 20 messages per month.

My arrangement is that emails sent to a public address (e.g. message@myDomain.com) are forwarded to myInbox@parseur.com and then the service parses the message, and sends the data I want to my script.

Genki
  • 378
  • 6
  • 17