Showing posts with label wrong time in the email. Show all posts
Showing posts with label wrong time in the email. Show all posts

Wednesday, May 1, 2013

How to overwrite client time in the email using postfix

Introduction:
If you ever received  an email from someone whom do not care about configuring his machine  with the right date, you will find his email in the top of your emails list even if you have received many emails later. Because this email time is in the future, also another situation could happen if you received an  email with a historical date. You will not be able easily to find this email because it is hidden between hundreds of emails in very old date. 

Solution:
I have created a Perl script which will read all emails received by your MTA ( In my situation it will be Postfix ) and change the date to server date.

Steps:
  1. Postfix master files:
We have to edit master file to add  content filter ,  you can call it any name like (myhook) in my example.

#vi /etc/postfix/master.cf

smtp      inet  n       -       n       -       -       smtpd -o content_filter=myhook

myhook unix - n n - - pipe
 flags=DROhu user=nobody argv=/etc/postfix/tml.pl -f ${sender} -d ${recipient}


and also we will need to add another service which will be used by the script to send the emails outside.
127.0.0.1:10025   inet  n       -       n       -       -        smtpd
        -o content_filter=
                -o receive_override_options=no_unknown_recipient_checks
                -o smtpd_helo_required=no
                -o smtpd_helo_restrictions=
                -o smtpd_data_restrictions=
                -o smtpd_client_restrictions=
                -o smtpd_sender_restrictions=
                -o smtpd_recipient_restrictions=permit_mynetworks,reject
                -o mynetworks_style=host
                -o in_flow_delay=0




3-Editing postfix header_check file to prevent sending wrong sender in the header.

#echo "/^Sender:/ IGNORE" >>  /etc/postfix/header_checks

4-Editing main.cf fileWithout this configs master.cf  parameters like (O and D) will not work, these parameters used to allow sending BCC.

# vim /etc/postfix/main.cf

myhook_destination_recipient_limit=1

5-Adding perl Script  and make it exec

#vi /etc/postfix/script.pl



#!/usr/bin/perl
##author: Mahmoud Ibrahim
##Email 1.m.mahmoud@gmail.com
#######  http://www.linkedin.com/profile/view?id=134494584&trk=tab_pro#####
## Version 0.3
##You must have a specific parameters in master.cf and main.cf files to be able to send BCC.
##Please contact me for any help. I tested this script for BCC and cc and TO with attachment 
## and have succedded (Alhamdo Leelah).
##All required parameters could be found in my BLOG :).
##http://e2mm10.blogspot.com
use warnings;
use strict;
use MIME::Lite;
use Net::SMTP;
use Mail::Internet;
my  $sys_time = localtime;
my $message = Mail::Internet->new(\*STDIN);
my $mailbcc=$message->head->get('Delivered-To');
my $mysender=$message->head->get('From');
my $mailrecept=$message->head->get('To');
if ($mailrecept=~ m/undisclosed[a-zA-Z-]/ ) {$message->head->replace("To",$mysender);}
$message->smtpsend(Host =>'127.0.0.1:10025',Bcc=>$mailbcc,MailFrom=>$mysender);



#chmod +x   /etc/postfix/script.pl                                                                                      

5- now we will have to restart postfix and check the log.
#/etc/init.d/postfix restart
#tailf /var/log/maillog



May  1 18:39:31 Mailsrv1 postfix/smtpd[16927]: 5073D39F00DE: client=smtp09.bis7.eu.blackberry.com[178.239.85.14]
May  1 18:39:31 Mailsrv1 postfix/cleanup[16923]: 5073D39F00DE: message-id=<1548046835-1367421534-cardhu_decombobulator_blackberry.rim.net-1130506589-@b14.c19.bise7.blackberry>
May  1 18:39:35 Mailsrv1 postfix/qmgr[14595]: 5073D39F00DE: from=<SRS0=/4fppu=OS=xxxxxxxxxxxx.com.sa=xxxxxxxxxx@srs.bis7.eu.blackberry.com>, size=244565, nrcpt=2 (queue active)
May  1 18:39:35 Mailsrv1 postfix/pipe[16930]: 5073D39F00DE: to=<xxxxxxx@xxxxxxx.com.sa>, relay=myhook, delay=5.2, delays=5.1/0/0/0.17, dsn=2.0.0, status=sent (delivered via myhook service)
May  1 18:39:35 Mailsrv1 postfix/pipe[16930]: 5073D39F00DE: to=<xxxxxxxxx@xxxxxxx.com.sa>, relay=myhook, delay=5.2, delays=5.1/0/0/0.17, dsn=2.0.0, status=sent (delivered via myhook service)
May  1 18:39:35 Mailsrv1 postfix/qmgr[14595]: 5073D39F00DE: removed



As we se here that the email will be relayed to myhook service which is the script that will check message date and change it. after that it will be send to port 10025 ( which we created to prevent loop). Also be informed that you can use any other content filter you want. 

Refrences