From eb40179a270469f6074f23b954710dcaf5293f77 Mon Sep 17 00:00:00 2001 From: clamsawd Date: Thu, 29 Oct 2015 22:01:25 +0100 Subject: [PATCH] Error handling when sending messages --- ip2e-daemon.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/ip2e-daemon.py b/ip2e-daemon.py index 500bcb5..a15010f 100755 --- a/ip2e-daemon.py +++ b/ip2e-daemon.py @@ -217,11 +217,23 @@ while PublicIP <= 2: SmtpSubject = "[ip2e-daemon] ["+CurrentTime+"] IP has changed" SmtpHeader = "From: "+FromEmail+"\n"+"To: "+ToEmail+"\n"+"Subject: "+SmtpSubject+"\n" SmtpBodyMessage = SmtpHeader+"\n"+"[ip2e] New IP is "+NewIP+"\n\n" - server = smtplib.SMTP(SmtpFromEmail) - server.ehlo() - server.starttls() - server.ehlo() - server.login(FromEmailUser,FromEmailPass) + try: + server = smtplib.SMTP(SmtpFromEmail) + server.ehlo() + server.starttls() + server.ehlo() + server.login(FromEmailUser,FromEmailPass) + except: + if os.name == "posix": + print (chr(27)+"[1;31m"+"[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")") + print (chr(27)+"[1;31m"+"[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]") + elif os.name == "nt": + RedColor() + print ("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")") + print ("[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]") + editlog.write("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")\n") + editlog.write("[ip2e-daemon] ["+CurrentTime+"] Check your settings [Aborted]\n") + exit(1) #Check sending errors try: server.sendmail(FromEmail, ToEmail, SmtpBodyMessage)