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 (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("","Subject","Text Body")

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

fromaddr = 'node.dangles AT'
password = 'myFancyPassword'
smtpaddr = ''
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.login(fromaddr, password)

   server.sendmail(fromaddr, toadd, text)

Leave a Reply

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

You are commenting using your 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