Python 100 project #39: Email Inbound-SMTP Test

When introducing a new security layer to the email service, sometimes I need to test if those servers are working well without changing anything in production server. It is usually quite difficult as Email services are usually company wide. But if I can specify the smtp server to use(in this case the new security appliance server ip address), it’s possible at least I can see if that server is configured correctly(to receive email).

 

Output Example:

$ python3 mail_helper.py 
This utility sends the email using specified SMTP server.
Please enter smtp server: 108.177.15.26

Please enter from address: admin@example.com

Please enter to address: XXXXXXXX@gmail.com

Please enter the subject of this email: this is a test subject

Please enter mail body: this is a test body from python3

Please enter file name (in full path) if any separated by space: /Users/XXXXX/test.csv /Users/XXXXXX/test.zip

Email has been sent.

 

 

Here is the code:

import smtplib
from email import encoders, utils
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import mimetypes


def attachment(filename):
    mimetype, mimeencoding = mimetypes.guess_type(filename)
    if mimeencoding or (mimetype is None):
        mimetype = 'application/octet-stream'
    maintype, subtype = mimetype.split('/')
    if maintype == 'text':
        with open(filename, 'r') as fd:
            retval = MIMEText(fd.read(), _subtype=subtype)
    else:
        with open(filename, 'rb') as fd:
            retval = MIMEBase(maintype, subtype)
            retval.set_payload(fd.read())
            encoders.encode_base64(retval)
    retval.add_header('Content-Disposition', 'attachment', filename=filename)
    return retval


def create_message(fromaddr, toaddr, subject, message, files):
    msg = MIMEMultipart()
    msg['To'] = toaddr
    msg['From'] = fromaddr
    msg['Subject'] = subject
    msg['Date'] = utils.formatdate(localtime=True)
    msg['Message-ID'] = utils.make_msgid()

    body = MIMEText(message, _subtype='plain')
    msg.attach(body)

    for filename in files:
        msg.attach(attachment(filename))
    return msg.as_string()


def send(smtpsrv, fromaddr, toaddr, message):
    s = smtplib.SMTP(host=smtpsrv, port=25)
    s.sendmail(fromaddr, [toaddr], message)
    s.close()


if __name__ == '__main__':

    print("This utility sends the email using specified SMTP server.")
    smtpsrv = input("Please enter smtp server: ")
    print()
    fromaddr = input("Please enter from address: ")
    print()
    toaddr = input("Please enter to address: ")
    print()
    subject = input("Please enter the subject of this email: ")
    print()
    msg = input("Please enter mail body: ")
    print()
    attach = input("Please enter file name (in full path) if any separated by space: ").split()
    print()
    message = create_message(fromaddr, toaddr, subject, msg, attach)
    try:
        send(smtpsrv, fromaddr, toaddr, message)
        print("Email has been sent.")
    except:
        print("Something wrong...")