#! /usr/local/bin/python --
import os, sys, string, time
def SendMail(subject, body, fromEmail, fromWhom, toAddress,
cc=None,
inreplyto=None,
replyto=None,
returnpath=None,
extraheaders=None,
sendmailpath=None):
sendmailPaths = ['/usr/sbin/sendmail', '/usr/lib/sendmail']
if not returnpath:
returnpath = fromEmail
if sendmailpath:
sendmailPaths.insert(0, sendmailpath)
# Go hunting for a proper one
sendmailpath = None
for f in sendmailPaths:
try:
if os.stat(f):
sendmailpath = f
break
except:
continue
if sendmailpath == None:
print "No valid sendmail found -- cannot send mail!"
sys.exit(1)
cmd = string.join([sendmailpath, '-t'])
## cmd = '/var/qmail/bin/qmail-inject -H -f "%s"' % (returnpath, )
try:
fp = os.popen(cmd, "w")
if extraheaders != None:
for line in extraheaders:
fp.write(line + "\n")
if not fromWhom:
fp.write('From: %s\n' % fromEmail)
else:
fp.write('From: "%s" <%s>\n' % (fromWhom, fromEmail))
fp.write("Subject: %s\n" % subject)
sec_offset = time.timezone;
t = time.localtime(time.time())
datestr = time.strftime ('%a, %d %b %Y %H:%M:%S', t)
if sec_offset >= 0:
sign = '-'
else:
sign = '+'
emailDate = "%s %s%02d00 (%s)" % (datestr, sign, abs(sec_offset / 3600), time.tzname[time.daylight])
fp.write("Date: %s\n" % emailDate)
if inreplyto != None:
fp.write("In-Reply-To: %s\n" % inreplyto)
if replyto != None:
fp.write("Reply-To: %s\n" % replyto)
fp.write("To: %s\n" % toAddress)
if cc==None or cc=="":
pass
else:
fp.write("Cc: %s\n" % cc)
fp.write("\n")
fp.write(body)
fp.close()
except:
import traceback
traceback.print_exc()
sys.stderr.write("error while trying to sendmail")
def main(argc, argv):
pass
if __name__ == "__main__":
main(len(sys.argv), sys.argv)