0

I've been trying to pipe an incoming email to a perl script so that I can monitor potential issues with invoices being emailed, and am not having much luck. Working on Red Hat Linux 2.6.32-696-6.3.el6.x86_64, cPanel 56.0 (build 51), and Exim 4.87

Using cPanel, I've forwarded emails received by test@someaddress.com to |/home/myuser/emailtest.pl. The script I've cobbled together from different searches looks like this (script permissions set to 755):

#!/usr/bin/perl
# This is a test!
use strict;
use warnings;
use File::Touch;
use File::stat;
use Time::localtime;
my $file_list = ('/home/myuser/testfile');
touch $file_list;
my $timestamp = ctime(stat($file_list)->mtime);
#print $timestamp;
#print "\n";

I was originally getting a bounceback error, after some reading I saw it was probably because I was printing the timestamp. I don't really need to print the timestamp, I was just doing that when I was running the script myself to verify that I could 'touch' the file, and also read the date. After commenting out the print commands, no more bounceback. Script still works when run from shell, and I can verify the timestamp from 'ls', but still not luck when I send an email to my test account.

It doesn't seem like the script is successfully touching the file when an email arrives, though. I don't get a bounceback, but the 'testfile' never changes unless I manually run the script from the shell. I've tried moving the script into /home/myuser/public_html as well but same result.

I would like to ultimately be able to 'touch' the file when a email is received, then use a daily cron job to check the timestamp of the file. If 4 days go by without a change to the file, I want it to shoot me an email and let me know that there may be a problem. I'd like to throw a check in there to not bother touching the file if it's already the current date, but all of that hinges on being able to 'touch' the file first based on an incoming email and that's where I'm stuck.

Can anybody see what the issue is here? I'm not committed to solving this using Perl if PHP (or another solution) is better suited for this, or if there is something else I'm overlooking.

2 Answers2

1

You may use exim's extended syntax of ~/.forward files to pipe incoming message into your perl script under some conditions. See also forwarding and filtering in exim.

AnFi
  • 6,103
  • 1
  • 14
  • 27
  • Thanks for the info, Andrzej! I just read through those, just to make sure I understand, it looks like this is something that would go into the actual user folder `/home/someuser` and not `/home/someuser/mail/mydomain.com/invtest`, would that be something that affects ALL emails, not just emails to invtest@mydomain.com? – Mario Ramirez Jul 21 '17 at 21:25
  • 1
    It should be `~test/.forward` file => `.forward` file in home directry of the recipient. It should handle only email for `test` user. It avoids problems with sctipy being executed with "wrong" use privileges => you may try to use `Sys::Syslog` to get some reports even in such case. – AnFi Jul 22 '17 at 03:17
  • Gotcha, thanks again, Andrzej! One last question -- this server is actually a server we use for email and website. Since the test user isn't an actual user on this server, just an email address, should I create an actual user with that name so that there will be a home folder? Sorry for so many followup questions! – Mario Ramirez Jul 24 '17 at 14:47
  • Creating special Operating System account makes smtp server configuration simpler. It also simplifies deployment of VERP https://en.wikipedia.org/wiki/Verp. Without VERP finding which of MANY recipient "bounces" is sometimes very tricky. – AnFi Jul 24 '17 at 16:45
0

You can use procmail to do deliveries. It is very programmable and can do multiple deliveries. You will need a .procmailrc file in the home directory of the user that you need to filter messages for.

There are examples for a number of use cases. I suspect you want to filter mail that meets some criteria and then deliver it. The filter language allows you to provide multiple selection criteria and filter the headers, body or both.

procmail should be available as a YUM package. Normally, it is enabled in the Exim configuration so that it will be used as the MDA if the .procmailrc file exists. The documentation should be readily available on line if you want to read it first.

BillThor
  • 27,737
  • 3
  • 37
  • 69
  • Hi Bill, thanks for the suggestion! Will procmail and exim work well together? I'm not terribly familiar with either, but have more experience with exim. I found this https://forums.cpanel.net/threads/why-cant-exim-and-procmail-work-together.245801/, it seemed doable if only initially frustrating for that guy. – Mario Ramirez Jul 24 '17 at 14:52
  • @MarioRamirez `procmail` is my primary delivery method. They work extremely well together. – BillThor Jul 25 '17 at 01:03