fix mail sending

Correct the logic related to parsing the server address so that the
default is not overwritten if there is no @ in the address.

Fix handling of a missing password by allowing an empty string to mean
no password was given.

Move the TO parsing out of the try/finally block so that errors in
parsing the message are not swallowed by errors when we quit a server
connection that wasn't opened properly.

Change-Id: I73a24a4d87c0f4a5aabd302d8a7d2884e5513c31
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-03-15 09:52:09 -04:00
parent 84976ffdf3
commit ffe14d7ed9
1 changed files with 7 additions and 5 deletions

View File

@ -39,20 +39,22 @@ def main():
user = None
pw = None
server = args.server
if args.server.find('@'):
creds, _, args.server = args.server.partition('@')
if '@' in server:
creds, _, server = args.server.partition('@')
user, _, pw = creds.partition(':')
with open(args.infile, 'r') as f:
msg = email.message_from_file(f)
server = smtplib.SMTP(args.server)
tolist = [address.strip() for address in msg['to'].split(",")]
server = smtplib.SMTP(server)
if args.verbose:
server.set_debuglevel(True)
try:
tolist = [address.strip() for address in msg['to'].split(",")]
if pw is not None:
if pw:
server.starttls()
server.login(user, pw)
server.sendmail(msg['from'], tolist, msg.as_string())