Gmail’ing in Python


There must be tons of these laying around. Add one more to it. This is just so i do not go searching for it some place else.

But why do I need to put it, where i need to be writing something about the cloud? Because I had an alerting issue to tackle when mailing alerts from Munin which I installed on EC2 to track the instance.  The corporate mail server did not allow smtp authentication outside our intranet. There were three solutions.

  1. Setup a proxy through which i connect to the corporate smtp server
  2. Just create an alerting email account with gmail and send use libgmail.
  3. Just create an alerting email account with gmail and use smtplib

The first was out of question; company policies boss. The second did not work for me, somehow, no time figure this out. The third i had already done this with my previous company’s mailserver. So it was matter of tweeking it. Here is the code by the way.

#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os, getopt, sys

gmail_user = "youridentity@gmail.com" #Senders email if here
gmail_pwd  = "yourpassword"            #password here

def gmail(to, subject, text):
   msg = MIMEMultipart()

   msg['From']    = gmail_user
   msg['To']      = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   smptServer = smtplib.SMTP("smtp.gmail.com", 587)
   smptServer.ehlo()
   smtpServer.starttls()
   smtpServer.ehlo()
   smtpServer.login(gmail_user, gmail_pwd)
   smtpServer.sendmail(gmail_user, to, msg.as_string())

   smtpServer.close()

def usage():
    print "Usage: python %s -s <subject> -m <message> -t <to> -h"% sys.argv[0]
    print "-s      Optional parameter: To specify: Subject of the email"
    print "-m      Optional parameter: To specify: Body of the email"
    print "-t      Optional parameter: To specify: Recipient email address"
    print "-h      Print this usage"
    sys.exit(1)

if __name__ == "__main__":
    opts,arg = getopt.getopt(sys.argv[1:], "hs:m:t:")

    to      = "toemail@todoma.in" #Default recipient
    subject = "<No Subject>"      #Default subject
    message = "<No Message>"      #Default message

    for o,a in opts:
        if o=='-s':
            subject = a
        if o=='-m':
            message = a
        if o=='-t':
            to=a
        if o=='-h':
            usage()

    mail(to,subject,message)

Advertisements

One thought on “Gmail’ing in Python

  1. Pingback: JHK» Posten mittels Python an Twitter und Google Buzz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s