#!/usr/bin/python2

#Importing modules
#-----------------
import os
import sys
import time
import tempfile


#Temporary files (file1=headers, file2=mail body)
#---------------

pi,po=os.popen2("".join( "%s %s" % ("/usr/bin/gpg --batch --armor --encrypt --recipient", sys.argv[1])))


data = sys.stdin.readline()	#Remove first line because it's not relevant for SMTP
data = sys.stdin.readline()

#Extraction of the headers
#-------------------------
headers=""
while data !='\n':
	headers+=data
	data = sys.stdin.readline()
headers+="\n"

#The rest of the input is the message body
#-------------------------
data = sys.stdin.readline()
pi.write(data)
pi.close()

body=po.read()


#Send the reencrypted mail and remove tempfiles
#-------------------------


sending=os.popen("".join("%s %s" % ("/usr/sbin/exim ", sys.argv[1])), 'w')




sending.write(headers+body)
print headers + body
