#!/usr/bin/env python # # Rick van der Zwet # # import argparse import mimetypes import os import random import re import smtplib import socket import string import sys import textwrap import time from time import gmtime,strftime from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate from email import encoders __svnversion__ = "$Id: foomail.py 405 2020-06-03 11:53:43Z rick $" __author__ = "Rick van der Zwet" default_subject = strftime('Test email send at %a, %d %b %Y %T %z', gmtime()) parser = argparse.ArgumentParser( description=textwrap.dedent(''' Sent test emails for mailserver verification and debugging Author : %s Version: %s ''' % (__author__, __svnversion__)), formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--from', '-f', dest='sender', required=True) parser.add_argument('--recipient', '-r', required=True) parser.add_argument('--server', '-s', required=True) parser.add_argument('--cc', help='Carbon Copy header') parser.add_argument('--file', action='append', help='Attachments to sent') parser.add_argument('--hostname', default=socket.gethostname(), help='EHLO/HELO hostname to present') parser.add_argument('--message', help='Alternative message text of body') parser.add_argument('--subject', default=default_subject, help='Alternative Subject header') parser.add_argument('--spam', action='store_true', help='Include X-Spam testing headers') parser.add_argument('--port', type=int, default=25, help='SMTP port used for connection') parser.add_argument('--reply-to', help='Set Reply-To: header') parser.add_argument('--to', help='Alternative To: header') parser.add_argument('--silent', action='store_true', help='Do not-follow SMTP flow verbosely') parser.add_argument('--virus', action='store_true', help='Include eicar.txt sample virus') parser.add_argument('--dry-run', '-d', action='store_true', help='Do not actually sent out the message') args = parser.parse_args() email = MIMEMultipart() email['From'] = 'Testing sender <' + args.sender + '>' email['To'] = args.to or args.recipient if args.cc: email['Cc'] = args.cc email['X-Server'] = args.server email['X-Port'] = str(args.port) email['X-Hostname'] = args.hostname email['Message-ID' ] = '<' +''.join(random.sample(string.ascii_letters + string.digits,10)) + '@' + email['X-Hostname'] + '>' email['Date'] = strftime('%a, %d %b %Y %T %z',gmtime()) email['Subject'] = args.subject email['X-Version'] = __svnversion__ email['X-Author'] = __author__ tests = [] if args.spam: tests.append('spam') if args.virus: test.append('virus') email['X-Tests'] = ' '.join(tests) if args.reply_to: email['Reply-To'] = args.reply_to if args.spam: email['X-Spam'] = 'XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X' if args.message: f = open(args.message,'r') msg = f.read() f.close() else: msg = textwrap.dedent(''' 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 list(email.keys()): v = email[k] msg += "* %-15s : %s\n" % (k,v) msg += textwrap.dedent(''' Best regards, Your system administrator -- BSD Licensed Source Code for this tool at: https://rickvanderzwet.nl/svn/personal/foomailer ''') email.attach(MIMEText(msg)) if args.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 args.file: for filename in args.file: mimetype = mimetypes.guess_type(filename)[0].split('/') part = MIMEBase(mimetype[0],mimetype[1]) f = open(filename,'r') part.set_payload(f.read()) f.close() encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename)) email.attach(part) print(email.as_string()) if not args.dry_run: mark = time.time() server = smtplib.SMTP(args.server, args.port, timeout=600) server.set_debuglevel(not args.silent) server.helo(args.hostname) server.ehlo(args.hostname) server.sendmail(args.sender, args.recipient, email.as_string()) server.quit() print("Sending took sec: %.2f" % (time.time() - mark))