#!/usr/bin/python2 # copy and paste this script # and save as date_check.py import sys import rfc822 from time import gmtime, mktime, timezone # get current UTC time in seconds since epoch current_time = mktime(gmtime()) # get email date header, if it exists headers = rfc822.Message(sys.stdin) header_date = headers.getdate_tz('Date') # if no date header, delete with "no date header" error if not header_date: print "The email had no Date field in the header" sys.exit(99) # convert date from message header to secs since epoch # header_date[9] is offset in secs for timezone header_time = mktime(header_date[0:9]) - header_date[9] time_diff = current_time - header_time # if date is older than one year # delete the message with an error year_seconds = 365*24*60*60 if time_diff > year_seconds: print "The header date in the email was more than a year old." sys.exit(99) # if date is more than two days in the future # delete the message with an error two_days_future_seconds = -2*24*60*60 if time_diff < two_days_future_seconds: print "The header date in the email was more than two days in the future." sys.exit(99)