skip to main | skip to sidebar

Python Programs and Examples

Pages

  • Home
 
  • RSS
  • Facebook
  • Twitter
Saturday, October 20, 2012

HTML Email using Python

Posted by Raju Gupta at 10:26 AM – 4 comments
 
We created a module called sendmail which email html content. Each of application dumps data from the datastructure into html format and then calls this module

def createhtmlmail( html, text, subject ):
   """Create a mime-message that will render HTML in popular
      MUAs, text in better ones"""
   import MimeWriter
   import mimetools
   import cStringIO

   out = cStringIO.StringIO() # output buffer for our message
   htmlin = cStringIO.StringIO(html)

   writer = MimeWriter.MimeWriter(out)
#
# set up some basic headers... we put subject here
# because smtplib.sendmail expects it to be in the
# message body
#
   
   writer.addheader("Subject", subject)
   writer.addheader("MIME-Version", "1.0")
#
# start the multipart section of the message
# multipart/alternative seems to work better
# on some MUAs than multipart/mixed
#
   writer.startmultipartbody("alternative")
   writer.flushheaders()
#
# the plain text section
#
   if text != None:
      txtin = cStringIO.StringIO(text)
      subpart = writer.nextpart()
      subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
      pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
      mimetools.encode(txtin, pout, 'quoted-printable')
      txtin.close()
#
# start the html subpart of the message
#
   subpart = writer.nextpart()
   subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
#
# returns us a file-ish object we can write to
#
   pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
   mimetools.encode(htmlin, pout, 'quoted-printable')
   htmlin.close()
#
# Now that we're done, close our writer and
# return the message body
#
   writer.lastpart()
   msg = out.getvalue()
   out.close()
   #print msg
   return msg

def sendmail (sender,to,subject,htmlFilename):
   import smtplib
   f = open(htmlFilename, 'r')
   html = f.read()
   f.close()
   message = createhtmlmail(html, None, subject)
   server = smtplib.SMTP("mta-hub")
   server.sendmail(sender, to, message)
   server.quit()


Email This BlogThis! Share to X Share to Facebook

4 Responses so far.

  1. Dipanwita says:
    August 12, 2018 at 11:27 PM

    Great blog. Keep up the good work. python training in Chennai

  2. brilliantkidsptdltd says:
    December 10, 2021 at 6:39 AM

    At this time, if you can use good medicinal materials and ingredients to strengthen hormones,Creative Voucher Provider In NSW will be able to retain your youth and enjoy masculine power.

  3. imlanguages says:
    December 14, 2021 at 10:53 AM

    Nice info, I am very thankful to you for sharing this important knowledge. This information is helpful for everyone. Read more info about Toefl Preparation Courses Hk. So please always share this kind of information. Thanks.

  4. Amisina Juniors Academy says:
    April 9, 2022 at 9:43 AM

    You are providing good knowledge. It is really helpful and factual information for us and everyone to increase knowledge. about Learn Python Programming for Kids .Continue sharing your data. Thank you.

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

  • To Send the entire contents of directory as an email Message.
    Here is a Python Program to send the entire contents of a directory as an email message #!/usr/bin/env python """Send the...
  • Control Structures in Python
    This Program explains different control structures in python If ==== # basic "If" if boolean: x = 1 else: x = 0 # No...
  • Python Code for creating Screen saver
    This is the Python code for creating a screensaver of a moving ball. The ball will bee moving along the screen and when it hits any of the ...
  • Python script for walking the directory tree structure but excluding some directories and files
    The script walks the directory structure recursively. Some of the directories and files can be excluded from the walk by providing them in ...
  • XML to CSV Converter
    Python Program to extract each execution data (few children in xml) and create a csv file. def xml2csv(inputFile,outputFile,elemName,n...
  • HTML Email using Python
    We created a module called sendmail which email html content. Each of application dumps data from the datastructure into html format and th...
  • Overview of Python Programming Language
    Python is an easy to learn, powerful programming language. It has efficient high level data structures and a simple but effective approac...
  • Fast and efficient Backup script that works on Windows and Linux
    This python script backup your directory structure that works on Linux and Windows both. It directly uses the shell to do this task and she...
  • Run Application from host
    import os os.execl( "c:/Windows/Notepad.exe", "c:/userlog.txt") print "Running notepad" #or import subpro...
  • Orphan Checker
    The script does not use any outside libraries, thus keeping its execution simple. we will start by initializing the arrays that we'll...
Powered by Blogger.

Archives

  • ▼  2012 (66)
    • ▼  October (28)
      • Find text/pattern under parent folder from matched...
      • XML to CSV Converter
      • XSL to CSV converter
      • Spell Corrector in Python
      • Format a plain ASCII file using Python script
      • Control Structures in Python
      • Python script to zip and unzip files
      • Fast and efficient Backup script that works on Win...
      • File Splitter Script
      • Python script for walking the directory tree struc...
      • Python XML Parser
      • IP Address Generation
      • Orphan Checker
      • HTML Email using Python
      • Python interface to load commands.
      • Python script to Validate Tabular format Data and ...
      • Python script to Load Configuration file content i...
      • Python Script to search file for specific content
      • Network Wise File Concatenation
      • Python Script for Adding or Subtracting the Dates
      • Program to create make file in VMS/UNIX
      • Python Code for creating Screen saver
      • Run Application from host
      • Multithreading in Python
      • This utility determines the size of a folder and i...
      • Using UNIX Environment Variables From Python
      • CVS Macro for Commited Files
      • Generating unique id's
    • ►  September (36)
    • ►  March (1)
    • ►  February (1)
 

Labels

  • Command Execution Using Python (2)
  • Control Structure (1)
  • CSV Examples (4)
  • Database (3)
  • Features of Python Programming Language (1)
  • File Example (2)
  • IF Statement (1)
  • List (2)
  • Mail (5)
  • Multithreading (1)
  • MySQL (3)
  • Python Date Example (2)
  • Python Example (2)
  • Python File Example (7)
  • Python Introduction (1)
  • Python Network Example (1)
  • Python XML Parser (2)
  • Searching Example (2)
  • Simple Calculation Program (1)
  • Strings (4)
  • Zip File Example (1)

Followers

 
 
© 2011 Python Programs and Examples | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger