Python emailer

So I have a nightly process that runs and I, being the lazy programmer I am, didn’t want to bother checking a log file each morning to see how it went. The natural answer is to have the results emailed to me because I do have to check my email.

Since the process is already handled mostly in python, thought I would implement the emailing via python. It was actually simple enough to do–just required figuring out the setting for my SMTP server (actually I didn’t figure it our for my work server so I’m using my GMail account).

I first wrote eMailer.py (see code below) and included it in my python path. Now, whenever I want to send code from a python application, it takes two easy lines:

import eMailer
eMailer.SendMess("someoneg@company.com","Subject","Text Body")

eMailer.py

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

fromaddr = 'node.dangles AT gmail.com'
password = 'myFancyPassword'
smtpaddr = 'smtp.gmail.com'
smtpport = 587

def SendMess(toadd, subject, body):

   msg = MIMEMultipart()
   msg['From'] = fromaddr
   msg['To'] = toadd
   msg['Subject'] = subject
   msg.attach(MIMEText(body, 'plain'))

   text = msg.as_string()

   server = smtplib.SMTP(smtpaddr, smtpport)
   server.ehlo()
   server.starttls()
   server.ehlo()
   server.login(fromaddr, password)

   server.sendmail(fromaddr, toadd, text)
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s