#!/usr/bin/env python # Rick van der Zwet # TODO: May need to implement some RFC checkers # # import sys,smtplib,re,socket,random,string,os,getopt import mimetypes import time from time import gmtime,strftime from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate from email import Encoders __svnversion__ = "$Id: foomail.py 633 2008-06-12 19:29:56Z rick $" __author__ = "Rick van der Zwet" def usage(): print "usage %s -f -r -s [--spam]" % sys.argv[0] print "[--virus] [--file ] [--message ]" print "[--subject ] [--to ] [--cc ]" print "[--force]" default_domain = 'example.com' def emailparse(email): if re.compile('.*@.*').match(email): return(email) else: return(email + '@' + default_domain) try: opts, args = getopt.getopt(sys.argv[1:], "f:r:s:d", ["from=","force","recipient=","server=","spam","virus","files=","dry-run","message=","subject=","to=","cc="]) except getopt.GetoptError: usage() sys.exit(128) spam = False virus = False server = False recipient = False sender = False files = False force = False dry = False message = False subject = False to = False cc = False tests = "" for o, a in opts: if o in ("-f","--from"): sender_raw = a sender = emailparse(a) elif o in ( "-r", "--recipient"): recipient_raw = a recipient = emailparse(a) elif o in ("-s", "--server"): server = a elif o == "--spam": spam = True tests += " Spam" elif o == "--virus": virus = True tests += " Virus" elif o == "--files": files = a elif o in ("-d","--dry-run"): dry = True elif o in ("--message"): message = a elif o in ("--subject"): subject = a elif o in ("--to"): to = a elif o in ("--cc"): cc = a elif o in ("--force"): force = True; error = False if not sender: print "No sender defined" error = True elif not recipient: print "No recipient defined" error = True elif not server: print "No server defined" error = True if error: usage() sys.exit(128) if force: sender = sender_raw recipient = recipient_raw #Subject here, just to keep the settings/layout a bit more constent if not subject: subject = 'Test email send at %a, %d %b %Y %T %z' subject = strftime(subject,gmtime()) if not to: to = 'Testing received <' + recipient + '>' to = recipient email = MIMEMultipart() hostname = socket.gethostname() email['From'] = 'Testing sender <' + sender + '>' email['To'] = to if cc: email['Cc'] = cc email['X-Server'] = server email['X-Hostname'] = hostname email['Message-ID' ] = '<' +''.join(random.sample(string.letters + string.digits,10)) + '@' + email['X-Hostname'] + '>' email['Date'] = strftime('%a, %d %b %Y %T %z',gmtime()) email['Subject'] = subject email['X-Version'] = __svnversion__ email['X-Author'] = __author__ email['X-Tests'] = tests #email['Reply-To'] = "Rick vd Zwet , Wouter Simons " if spam: email['X-Spam'] = 'XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X' def defaultmessage(): msg = '' msg += \ ''' Hi, Sorry to bother you... Please ignore this message as it is used to test and verify systems to make sure everything works flawless. %(virus)s Technical details:\n''' % email for k in email.keys(): v = email[k] msg += "* %-15s : %s\n" % (k,v) msg = msg + \ ''' Best regards, Your system administrator -- BSD Licensed Source Code for this tool at: https://rickvanderzwet.nl/svn/personal/foomailer ''' return(msg) if message: f = open(message,'r') msg = f.read() f.close() else: msg = defaultmessage() email.attach(MIMEText(msg)) if virus: part = MIMEBase('application', "octet-stream") part.set_payload('X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*') Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="eicar.txt"') email.attach(part) if files: for file in files.split(':'): mimetype = mimetypes.guess_type(file)[0].split('/') part = MIMEBase(mimetype[0],mimetype[1]) f = open(file,'r') part.set_payload(f.read()) f.close() Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) email.attach(part) print email.as_string() if not dry: mark = time.time() server = smtplib.SMTP(server) server.helo(hostname) server.ehlo(hostname) server.set_debuglevel(0) server.sendmail(sender, recipient, email.as_string()) server.quit() print "Sending took sec: %.2f" % (time.time() - mark)