KoblentsBlog Photography
Contact About
Ches
Sending Mail With Python, pt 2
Work based on a previous reference: my python script to send mail on MacOS. (As previously mentioned, on Linux I just use mail/sendmail.) I believe interacting with this script is cleaner and simpler. The password is hardcoded, but for the automation infrastructure we're using, something has to store the password, so why not the caller? We can probably do better ... but.
Script after the jump.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
#!/usr/bin/env python

import sys
import smtplib
import optparse
from os.path import basename
import datetime
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate

def send_mail(hostname, port, username, password,
		send_from, send_to, subject, text, files=None):
	assert isinstance(send_to, list)
	
	msg = MIMEMultipart()
	msg['From'] = send_from
	msg['To'] = COMMASPACE.join(send_to)
	msg['Date'] = formatdate(localtime=True)
	msg['Subject'] = subject

	msg.attach(MIMEText(text))

	for f in files or []:
		with open(f, "rb") as fil:
			part = MIMEApplication(
				fil.read(),
				Name=basename(f)
			)
		# After the file is closed
		part['Content-Disposition'] = \
			'attachment; filename="%s"' % basename(f)
		msg.attach(part)
	
	smtp = smtplib.SMTP(hostname, port)
	smtp.ehlo()
	smtp.starttls()
	smtp.ehlo()
	smtp.login(username, password)

	smtp.sendmail(send_from, send_to, msg.as_string())
	smtp.close()

parser = optparse.OptionParser(usage='%prog --user <user> --passwd <pass> --host <host> [--port <port] --from_addr <from> --to_addr <to, comma separated> --subj <subject> [--attach <files, comma separated>', description='Sends an email. All options are passed in by parameter, including the body. The username is your SMTP mail server\'s username.\n')

parser.add_option('--user', default=None, help='Username')
parser.add_option('--passwd', default=None, help='Password')
parser.add_option('--host', default=None, help='Hostname (SMTP server)')
parser.add_option('--port', default=587, help='Port (usually 587 or 25)')
parser.add_option('--from_addr', default=None, help='From (email)')
parser.add_option('--to_addr', default=None, help='To (email, list)')
parser.add_option('--subj', default=None, help='Subject')
parser.add_option('--body', default=None, help='Body (opt)')
parser.add_option('--attach', default=None, help='Attachment (file)')

opts, args = parser.parse_args()

if (opts.user is None or opts.passwd is None or opts.host is None or
		opts.from_addr is None or opts.to_addr is None or
		opts.subj is None):
	parser.print_help()
	sys.exit(0)


date_object = datetime.date.today()
body = 'Board status update for %s\r\n\r\n' % date_object
if opts.body:
	body += opts.body

body += '\r\n\r\n'

send_mail(opts.host, opts.port, opts.user, opts.passwd,
	opts.from_addr, [opts.to_addr], opts.subj, body, [opts.attach])
Ches Koblents
June 20, 2019
 
« Newer Older »
© Copyright Koblents.com, 2012-2024