1

On the shared-host where my website is, I have an e-mail forwarder that sends messages to my phone as a text message. Recently, however, it's been getting a bit out of hand.

I am hoping that, using the "User Level Filtering" in cPanel, I can setup a RegEx-based filter to only forward e-mails to my phone between the hours of 07:00:00 and 22:00:00.

This filter would match the following e-mail header, which typically occurs 2-3 lines into the message source:

Received: by 99.99.99.99 with SMTP id XyXyXyXyXyXyXy;
        Tue, 15 May 2012 01:22:33

... and this is how I am hoping it'd work:

Received: by (ANY IP) with (ANYTHING) id (ANYTHING);
        (Sun|Mon|Tue|Wed|Thu|Fri|Sat), (ANY DATE/MONTH/YEAR) (RegEx magic to match time?)

When the match succeeds (e.g. If the time is between 07:00:00 and 22:00:00) the message would then be sent to my phone, otherwise it would do nothing. (This has nothing to do with the RegEx itself, of course.)

I'm quite a Regular Expressions n00b, but have devised the following (non-working) RegEx, and have no idea how close or far away I am from it working:

\bReceived: by (.*) with [a-Z] id (.*)(Sun|Mon|Tue|Weds|Thu|Fri|Sat),(.*)[0-2][2-7]:[0-2][0-9]:[0-2][0-9]\bsi

Any thoughts, suggestions or creative solutions? I have been told by my hosting service that RegEx in cPanel needs to be in PERL format.

user1076122
  • 115
  • 5

2 Answers2

2

Amended version of Eugene's regex;

/\bReceived: by .*?;\s*(?:Sun|Mon|Tue|Weds|Thu|Fri|Sat),\s*\d+\s+\w+\s+\d+\s+(?:0[789]|1\d|2[01]):\d\d:\d\d\b/

This version uses non-capturing grouping, and restricts the acceptable time to 07:00:00 to 21:59:59. It assumes the time format is valid - in theory it would pass a time of 10:99:99, but I'm assuming you won't see those.

Rory Hunter
  • 3,425
  • 1
  • 14
  • 16
  • I had to modify this for use with cPanel as follows... `Received:.*?;\s*(?:Sun|Mon|Tue|Weds|Thu|Fri|Sat),\s*\d+\s+\w+\s+\d+\s+(?:0[789]|1\d|2[01]):\d\d:\d\d` ... but truly this was _exactly_ what I needed, and works a charm! Thanks you SO MUCH for your help! – user1076122 May 21 '12 at 20:57
  • Though this thread is 10 years old, I came across this during my search for a regex that would match the date and time in a Received header. I'm wondering why you would restrict the hour part to `(?:0[789]|1\d|2[01])`. This would e. g. not match 03:00:00 or 22:00:00. Instead, I went with this: `(?:0\d|1\d|2[0123])`. – Richard Steinbrecht Sep 16 '22 at 08:17
  • The original poster only wanted to match emails when they were received between particular hours of the day. Outside of those hours, the regex shouldn't match. – Rory Hunter Sep 18 '22 at 11:11
0

Fixed for you to work using http://gskinner.com/RegExr/, still largely suboptimal. Tell if you want to go into this further.

\bReceived: by (.*) with \w+ id (.*);\s*(Sun|Mon|Tue|Weds|Thu|Fri|Sat),\s*(\d+\s+\w+\s+\d+)\s+[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\b
Eugene Ryabtsev
  • 2,232
  • 1
  • 23
  • 37