Send mail using Python’s smtplib module

Python has a built in module to send mail to recipient[s] as to,cc,bcc. Here assumption is that : the smtp is configured in localhost (where the script will run).

import socket
import smtplib
from email.mime.text import MIMEText

 def SendMail(file,Email,status):
 fp = open(file,'rb')
 msg = MIMEText(fp.read())
 fp.close()
 to=Email
 cc='def@example.com'
 bcc='123@example.com'
 msg['Subject'] = 'MULTINODE SETUP :: '+status
 msg['From'] = 'abc@example.com'
 msg['to'] = to
 msg['cc'] = cc

 msg['bcc'] = bcc
 toaddr=to.split(",")+cc.split(",")+bcc.split(",")
 s = smtplib.SMTP('localhost')
 s.sendmail('something@example.com',toaddr ,msg.as_string())
 s.quit()

 file='/u01/work/tmp/sidtest'
 Email='myname@example.com'
 status='testing'
 SendMail(file,Email,status)

 

 

 

Advertisements
This entry was posted in ARTICLES, DevOps, HOWTO, Programming, SCRIPTING, TIPS & T RICS and tagged , , , . Bookmark the permalink.

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