quick python cgi to send email
In the previous post I mentioned a python script I was using with curl to send me a SMS message. Its a pretty quick and dirty one, but here is the script I use. Since these are quick messages meant for my phone - I don't do much with the body. And I just use this with a GET style URL, although the python cgi lib could just as easily handle a POST. So I call it like this:
http://www.domain.net/cgi-bin/sendmemail.py?to=number@txt.att.net&subject=printing_done
I use this via curl in other scripts as a sort of notification system.
#! /usr/bin/env pythonimport cgi,cgitbcgitb.enable()# Required header that tells the browser how to render the text.print "Content-Type: text/plain\n\n"# Print a simple message to the display window.from email.MIMEText import MIMETextimport smtplib,sysThe_Form = cgi.FieldStorage()body='''blank'''msg = MIMEText(body)mfrom = "email@example.com"to = The_Form['to'].valuemsg['From'] = mfrommsg['To'] = tomsg['Subject'] = The_Form['subject'].valueserver = smtplib.SMTP("smtp.gmail.com",587)server.ehlo()server.starttls()server.ehlo()server.login(<smtp login>,<smtp password>)server.sendmail(mfrom,[to],msg.as_string())server.quitprint 'sent'