Skip to content

Quick python mailer

wordpress meta

title: 'Quick Python Mailer'
date: '2014-09-23T12:47:38-05:00'
status: publish
permalink: /quick-python-mailer
author: admin
excerpt: ''
type: post
id: 743
category:
    - Python
    - SMTP
tag: []
post_format: []

import sys,smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

msg = MIMEMultipart()
msg['From'] = 'root@domain.ad'
msg['To'] = 'user@domain.com'
msg['Subject'] = 'Process finished -' + sys.argv[1]
message = 'here is the email'
msg.attach(MIMEText(message))

mailserver = smtplib.SMTP('mail.domain.com',25)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()

mailserver.sendmail('root@domain.ad','user@domain.com',msg.as_string())

mailserver.quit()

** This script need to be optimized. I am duplicating from and to fields