How to send output from a python script to an email address

后端 未结 6 762
野性不改
野性不改 2021-02-05 22:38

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

6条回答
  •  太阳男子
    2021-02-05 23:22

    Use smtplib. The example they provide is pretty good.

    import smtplib
    
    def prompt(prompt):
        return raw_input(prompt).strip()
    
    fromaddr = prompt("From: ")
    toaddrs  = prompt("To: ").split()
    print "Enter message, end with ^D (Unix) or ^Z (Windows):"
    
    # Add the From: and To: headers at the start!
    msg = ("From: %s\r\nTo: %s\r\n\r\n"
           % (fromaddr, ", ".join(toaddrs)))
    while True:
        try:
            line = raw_input()
        except EOFError:
            break
        if not line:
            break
        msg += line
    
    print "Message length is " + repr(len(msg))
    
    server = smtplib.SMTP('localhost')
    server.set_debuglevel(1)
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()
    

提交回复
热议问题