Select to view content in your preferred language

Sending e-mail following completion of python script

1478
2
10-20-2011 06:17 PM
BillBass
Deactivated User
I have a rather long python scripting process that runs several hours. I would like to code into the script something that allows an e-mail message to be sent to me at the end of the program. I'm not looking to have any errors or logging sent, just a simple e-mail sent such as, 'The program has finished.' It would be the final step in the python program.

I have looked on Python.org and tried a couple of examples, but nothing seems to work.

Thanks.
Tags (2)
0 Kudos
2 Replies
AlexOulton1
Emerging Contributor
Hi Bill,

This code works for me:

import smtplib

#add the following lines to the end of your python script

to = '' #insert reciever email address (can be same as sender)
gmail_user = '' #your gmail sender address
gmail_pwd = '' #your gmail password
smtpserver = smtplib.SMTP("smtp.gmail.com",587) #the technical stuff
smtpserver.ehlo() #the technical stuff
smtpserver.starttls() #the technical stuff
smtpserver.ehlo #the technical stuff
smtpserver.login(gmail_user, gmail_pwd) #the technical stuff
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
msg = header + '\n' + 'Your Python Script has now Completed!' #The completion message
smtpserver.sendmail(gmail_user, to, msg) #Sending the mail
smtpserver.close() #closing the mailserver connection
0 Kudos
ScottBlankenbeckler
Deactivated User
Alex,

When I try to use your code I get the following error

Traceback (most recent call last):
  File "email.py", line 26, in <module>
    import smtplib
  File "c:\Python26\ArcGIS10.0\lib\smtplib.py", line 46, in <module>
    import email.utils
  File "C:\GISData\Scripts\email.py", line 31, in <module>
    smtpserver = smtplib.SMTP("smtp.gmail.com",587) #the technical stuff
AttributeError: 'module' object has no attribute 'SMTP'


Any clue on what is happening?
0 Kudos