Skip to content

Feature: MCVE for testing email settings #74

@VARGA-Peter

Description

@VARGA-Peter

Due to the fact testing the email feature can be for none python guys and none administrators very tiresome I wrote here a very small MCVE with which everybody can very easy play around until the proper email settings have been found out. Copy them into VmBackup.py and you are ready.

Change the value between <> according to your environment. Save the code as `TestEmail.py, change the permission to +x. Run it repeatedly with playing around until you received the test email.

#!/usr/bin/python
#
#NAUVmBackup/TestEmail.py
# created from VmBackup.py V3.22 November 2017
# Adapted by Peter VARGA
#
#@NAUbackup - NAU/ITS Department:
# Douglas Pace
# David McArthur
# Duane Booher
# Tobias Kreidl
#
# With external contributions gratefully made by:
# @philippmk -
# @ilium007 -
# @HqWisen -
# @JHag6694 -
# @lancefogle - Lance Fogle

# Copyright (C) 2017  Northern Arizona University

import sys, time, os, datetime, subprocess, re, shutil, XenAPI, smtplib, re, base64, socket
from email.MIMEText import MIMEText
from subprocess import PIPE
from subprocess import STDOUT
from os.path import join

############################# OPTIONAL
# optional email may be triggered by configure next 3 parameters then find MAIL_ENABLE and uncommenting out the desired lines
MAIL_TO_ADDR = '<YOUR-NAME>'
# note if MAIL_TO_ADDR has ipaddr then you may need to change the smtplib.SMTP() call
MAIL_FROM_ADDR = '<YOUR-NAME>'
MAIL_SMTP_SERVER = '<SMTP-SERVER-IP-OR-DOMAIN>'

def send_email(to, subject, body_fname):

    smtp_send_retries = 3
    smtp_send_attempt = 0

    message = open('%s' % body_fname, 'r').read()

    msg = MIMEText(message)
    msg['subject'] = subject
    msg['From'] = MAIL_FROM_ADDR
    msg['To'] = to

    while smtp_send_attempt < smtp_send_retries:
        smtp_send_attempt += 1
        if smtp_send_attempt > smtp_send_retries:
            print("Send email count limit exceeded")
            sys.exit(1)
        try:
            # note if using an ipaddress in MAIL_SMTP_SERVER,
            # then may require smtplib.SMTP(MAIL_SMTP_SERVER, local_hostname="localhost")

            ## Optional use of SMTP user authentication via TLS
            ##
            ## If so, comment out the next line of code and uncomment/configure
            ## the next block of code. Note that different SMTP servers will require
            ## different username options, such as the plain username, the
            ## domain\username, etc. The "From" email address entry must be a valid
            ## email address that can be authenticated  and should be configured
            ## in the MAIL_FROM_ADDR variable along with MAIL_SMTP_SERVER early in
            ## the script. Note that some SMTP servers might use port 465 instead of 587.
            s = smtplib.SMTP(MAIL_SMTP_SERVER)
            #### start block
            username = '<YOUR-NAME>'
            password = '<YOUR_PASSWORD>'

            # change the port number. Possible values: 25,465,587
            s = smtplib.SMTP(MAIL_SMTP_SERVER, <YOUR-SMTP-SERVER-PORT>)
            s.ehlo()

#           some SMTP don't use it - comment it when receiving an error like this
#           Exception: SMTPException - STARTTLS extension not supported by server.
#           s.starttls()

#           comment the next line if your receive an error like this
#           Exception: SMTPException - No suitable authentication method found.
            s.login(username, password)

            #### end block
            s.sendmail(MAIL_FROM_ADDR, to.split(','), msg.as_string())
            s.quit()
            break
        except socket.error, e:
            print("Exception: socket.error -  %s" %e)
            time.sleep(1)
        except smtplib.SMTPException, e:
            print("Exception: SMTPException - %s" %e.message)
            time.sleep(1)

status_log = '<ENTER-PATH-TO-A-TEST-FILE>'
send_email(MAIL_TO_ADDR, 'Test-Email VmBackup.py', status_log)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions