cookies

Friday 21 August 2015

Sending e-mail via python script - version 2, simplified


Usage :  ./script.sh   "E-Mail Subject"   /var/log/dmesg.log


This version puts contents of /var/log/dmesg.log in to an email message directly


script.sh :
#!/usr/bin/env python

import sys
from os import path
from smtplib import SMTP
from email.mime.text import MIMEText
from email.MIMEBase import MIMEBase
from email import Encoders

me = 'author@server.com'
you = 'recipient@example.com'
cc = 'other@carbon.copy'
bcc = 'nsa@gov.us'

fp = open(sys.argv[2], 'rb')
msg = MIMEText(fp.read(), 'plain', _charset="UTF-8")
fp.close()

msg['Subject'] = sys.argv[1]
msg['From'] = me
msg['To'] = you
msg['Cc'] = cc
msg['Bcc'] = bcc

sndm = SMTP()
sndm.set_debuglevel(1)
sndm.connect('smtp.server.com', 25)
sndm.starttls()
try :
    sndm.esmtp_features['auth'] = 'DIGEST-MD5 LOGIN'
    sndm.login('author@server.com', 'Pas5_W0rd')
    sndm.sendmail(me, [you,cc,bcc], msg.as_string())
    sndm.quit()
except Exception, e:
    print e

No comments:

Post a Comment