I have a threaded python script that pings 20 nodes on a local area network, and prints out the status of each: Node is Alive, Node is Down, etc.. I would like to have this outp
A great resource on the topic of sending email with Python is Al Sweigart's 'Automate the Boring Stuff with Python'. Chapter 18 is the part you want. In short, if you have an email address with one of the big email providers (think Google, Outlook, Yahoo, etc.) you can use their Simple Mail Transfer Protocol (SMTP) server(s) to handle your messages from Python. As Al says:
If you don't have an email address with one of the big providers or you're not in a position where you can use an email address from an outside provider, then it's a bit harder. Perhaps someone at your company can tell you 1) if your company has an SMTP server and 2) what its domain name and port number are.
Once you have all that, sending out an email message from your program is a piece of cake:
import smtplib
def main():
# get message from node
message1 = 'Node 1 is up :)'
# print message from node
print(message1)
# get message from another node
message2 = 'Node 2 is down :('
# print that too
print(message2)
# now, all done talking to nodes.
# time to compile node response results and send an email.
# first, let's get every thing setup for the email
from_me = 'awesome.name@my_email_provider.com'
to_me = 'awesome.name@my_email_provider.com'
email_message = message1 + '\n' + message2
# second, let's make sure we have a connection to a Simple Mail Transfer Protocol (SMTP) server
# this server will receive and then send out our email message
domain_name = 'smtp.my_email_provider.com'
port_number = 587 # or maybe, 465
server = smtplib.SMTP(domain_name, port_number)
# alright! if that last line didn't raise an exceptions, then you're good to go. Send that bad boy off.
server.sendmail(from_me, to_me, email_message)
if __name__ == '__main__':
main()
Loggers are great too, so don't discount what everyone's said about them. Print to the terminal. Log to a file. AND email out! Loggers can do everything.