#!/usr/local/bin/python

import sys
from time import time, asctime
from os import environ
from sys import exit

def deliver(str):
	sndr = str.readline()
	sndr = sndr[0:len(sndr) - 1]
	rcpt = str.readline()
	rcpt = rcpt[0:len(rcpt) - 1]
	msg = str.read()


	print "Delivering message from %s to %s..." % (sndr, rcpt)

	try:
		mailpath = environ['MAILBOX']
	except:
		print "Error: mailpath NOT set!"
		exit(0)

	mail = open("%s/%s" % (mailpath, rcpt), 'a')
	mail.write("From %s %s\n" % (sndr, asctime()))
	mail.write(msg)


if __name__ == '__main__':
	try:
		deliver(sys.stdin)
	except Exception, e:
		print e
		exit(0)
	exit(1)
