Email Parse project

Hi Guys,

I had successfully completed this project by another means by using an android device.

However, I am looking to achieve this on my current website.
Rather than relying on a device I own yo do the task.

Here are the steps I want to take.

  1. A new email arrives and checks if it matches ( part of a subject ).
  2. If true capture the body content and store parts of it as a variable.
  3. Compose a custom email, send address will depend on who mentioned in the body and use their email address.
  4. a custom email body template that will use variables to change some parts of the text.
  5. Send the email.

Hi! I’m new here, is there a question or are you just stating you want this? :face_with_raised_eyebrow:

Is the email stored on an imap server?

I wrote a simple SMTP server back in December, it would accept email, parse out attachments and do something with them…

I wouldn’t recommend going down that route, SMTP is super ancient and there’s a gazillion RFCs that nobody fully implements because they just don’t make sense.

What are you using as a mail server on your website, can you just write a mail filter plugin for whatever is your MTA?

@McBrown It’s a question, sorry I did not make that clear. I was listing things that are needed for my use.

@cotton that’s correct, by the web host.

@risk I am not exactly sure, but it is nothing special really, there is no plugin support that I know of.

1 Like

Are you good with Python or Ruby?

I’ve done something that sounds very similar to your challenge using Ruby’s NET::IMAP and NET::SMTP classes. These are included in a standard ruby install.

Basically, use the IMAP class to connect to the imap server. You can then iterate over the emails on the server and construct structs that represent the components of the email (from, to, subject, body, etc). You might store those in an array as you construct them.

Once you have constructed the email structs you can iterate over those using the array I mentioned above and run regex against the subject property of each for whatever you are looking for.

In case there match case you might prepare a reply-to email struct which you can set the body to your template email, add the original email address’s from as the to address and then your from address in the from address.

Finally, use the NET::SMTP class to create a connection to you smtp server and send your email.

I typed this on a phone so I can’t be as complete as I’d like. But basically you create a connection to your imap server. Iterate over the emails and create structs out of them. Iterate over those structs’ subject line with regex to match whatever your looking for.

Next, when there’s matches construct email structs which you can send back. So, set the addressing and placing your body template based on the regex match.

Finally, use the smtp lib to connect to your smtp server and send those emails you built.

Good luck. Let me know if you run into any trouble.

First off, this sounds like it could become a security nightmare in the future, so please take precaution to guard against SQL injections and other unwanted things.

With that said, what you are trying to accomplish can be easily performed in python or PHP.

Interesting, it sounds good.

Unfortunately I don’t have any experience with Ruby nor Python.

My question with this type of setup is, how would the program execute? Will it listen to any incoming email that enters an inbox?

Go research IMAP

1 Like

Your quite right, Unfortunately I only have some experience with php

I can think of a few implementations, but a simple solution would be to poll the imap server to see when a new message is available. At that point execute the process script and then remove the message.

Rinse and repeated.

However, I’m not sure of the architecture of this system so there’s likely other options, but the above would likely be a simple implementation.

I used procmail, fetchmail, and shell scripts to hack something similar together many, many years ago. I’m sure there are projects readily available to do most of the dev work for you these days.


I have a Tasker (An Android app for automating UI actions etc) project that accomplishes what I want.

Which just waits for a notification to appear, to trigger the task. Although I would like to move away from this, as I prefer it not to run on my own personal devices.

I did come across this site > that does exactly what I want. The only downside is that it triggers the task every 15mins.

My suggestion is, make it simple and stay simple. A PHP script can easily do what you request.

My approach would be something like, first fix the script, then hook it up to the email service to process incoming email.

As for structure, something like:

Step 0, decide on a framework for email. For instance, something like php-mime-mail-parser for receiving mail, and PHPMailer for sending out mails.

Step 1, find a specific substring within a subject string (using mail-parser):

function parse_email($parser) {
    $pattern = "MySpecificPattern";
    $subject = $parser->getHeader('subject');
    if (strpos($subject, $pattern) < 0) {
        return [];

    $res = [];
    // Parse email here
    return $res;

The rest is pretty basic stuff once you get the parsing done.

1 Like

Looks grate!

By the looks of it php-mime-mail-parser, requires to be installed, however I don’t think my shared hosting account allows me to install this.

There are more than one way to fleece a sheep, same goes for installing software. As long as you have FTP access (or better yet, a shell account) there is a way. :slight_smile: