Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse emails in realtime as they are recieved

I need to build an email parsing script which would parse emails that would come into an inbox and dump the contents into a database, while at the same time make a curl request with details parsed from the email.

At this moment I'm quite stuck on implementing the part on how to parse emails in realtime as they are recieved in the inbox. Is there a way to set triggers to do something like this? I've extensive experience with working with php based webmail clients but this seems different though.

How can this be accomplished - I am assuming a cron job but if theres another way to do so I'm all ears.

like image 571
Ali Avatar asked Dec 16 '22 09:12

Ali


2 Answers

Yes, there is. You can pipe emails to your scripts.

Assuming you are using cPanel, follow this steps:

  • Log in to your cPanel.
  • Click on the Forwarders icon, under the Mail tab.
  • Click on the Add Forwarder button.
  • Fill in Address to Forward and put the mail address you would like to pipe the messages from.
  • Select Pipe to a Program and fill in the full path to the script which will handle the messages.

And here is a mail catcher example that sends received email to your other mail (just for demonstration):

#!/usr/bin/php -q
<?php
 
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
    $email .= fread($fd, 1024);
}
fclose($fd);
 
 
mail('[email protected]','From my email pipe!','"' . $email . '"');
 
?>
like image 172
R Pasha Avatar answered Jan 29 '23 19:01

R Pasha


You would use a cron job if you wanted to do something at specific times. If you want to do something whenever an email arrives you need to tie your code into your email system. The usual way to do this is with Procmail (there is a recipe you can use (just read PHP for Perl/shell)).

like image 42
Quentin Avatar answered Jan 29 '23 21:01

Quentin