jump to navigation

Send e-mail through gmail using python October 5, 2007

Posted by bdonovan in : Uncategorized , trackback

I’ve been running a number of simulations on a remote machine which take days to complete. I got tired to sshing into the machine to check the status so I’ve written a short python script to send a notification e-mail through gmail.


#!/usr/bin/env python

import smtplib
import sys

smtpserver = 'smtp.gmail.com'
user = ''
passwd = ''
subject = 'Processing Complete'

def send_gmail(to,msg):
    conn = smtplib.SMTP(smtpserver)
    conn.ehlo()
    conn.starttls()
    conn.ehlo()
    conn.login(user,passwd)
    msg = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s' % \
        (user,to,subject,msg)

    conn.sendmail(user,to,msg)
    conn.close()

if __name__ == '__main__':
    to = sys.argv[1]
    msg = sys.argv[2]
    send_gmail(to,msg)

The script can be run from the command line:

# send_gmail.py to@address.com 'Msg'

Comments

Sorry comments are closed for this entry