title: Sending an HTML rich email using python
body: <p>I am trying to send HTML rich email, so far the code is working but the colour formatting i had in html message content is not showing when I check in my mailbox i sent to.</p>
  
  <p>So far here is the code :</p>
  
  <pre><code>from email.MIMEMultipart import MIMEMultipart
  from email.MIMEBase import MIMEBase
  from email.MIMEText import MIMEText
  from email.Utils import COMMASPACE, formatdate
  from email import Encoders
  import smtplib
  
  class EMail(object):
          """ Class defines method to send email of attachment
          """
          def __init__(self, sendto, mailFrom, server, usrname, password, files, debug=False, subjt=None):
                  self.debug = debug
                  self.mailto = sendto
                  self.mailFrom = mailFrom
                  self.smtpserver = server
                  self.EMAIL_PORT = 587
                  self.usrname = usrname
                  self.password = password
                  self.subject = subjt
  
                  # self.send(files)
  
          def sendMessage(self, msgContent, files):
  
                  #collect info and prepare email
                  if files:
                      if self.subject == "":
                          #Subject should contains of file attached
                          if len(files) &lt;=3: subjAdd = ','.join(files)
                          if len(files) &gt; 3: subjAdd = ','.join(files[:3]) + '...'
                          self.subject= self.systemLogin() +" sent mail from maya "+ os.path.basename(subjAdd)
                  print "subject: ", self.subject
                  msg = self.prepareMail(self.mailFrom, self.mailto, self.subject, msgContent, files)
  
                  # connect to server and send email
                  server=smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
                  server.ehlo()
                  server.starttls()#use encrypted SSL mode
                  server.ehlo() # to make starttls work
                  server.login(self.usrname, self.password)
                  server.set_debuglevel(self.debug)
                  try:
                          failed = server.sendmail(From, to, msg.as_string())
                  except Exception as er:
                          print er
                  finally:
                          server.quit()
  
          def prepareMail(self, From, to, subject, msgHTML, attachments):
                  msg = MIMEMultipart()
                  msg['From'] = From
                  msg['To'] = to
                  msg['Date'] = formatdate(localtime=True)
                  msg['Subject'] = subject
  
                  #The Body message
                  msg.attach(MIMEText(msgHTML, 'html'))
                  msg.attach(MIMEText("Sent from maya by Mini Me"))
                  if attachments:
                      for phile in attachments:
                              #we could check for MIMETypes here
                              part = MIMEBase('application',"octet-stream")
                              part.set_payload(open(phile, "rb").read())
                              Encoders.encode_base64(part)
                              part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(phile))
                              msg.attach(part)
                  return msg
  </code></pre>
  
  <p>and here is the HTML formatted text I am sending, (I have removed the HTML head section it being long due to css)</p>
  
  <pre><code>&lt;body class="body_foreground body_background" style="font-size: normal;" &gt;
  &lt;pre&gt;
  
  ---
   send.py | 4 &lt;span class="ansi32"&gt;+&lt;/span&gt;&lt;span class="ansi31"&gt;---&lt;/span&gt;
   1 file changed, 1 insertion(+), 3 deletions(-)
  
  &lt;span class="ansi1"&gt;diff --git a/send.py b/send.py&lt;/span&gt;
  &lt;span class="ansi1"&gt;index 87126d5..abb1fd8 100644&lt;/span&gt;
  &lt;span class="ansi1"&gt;--- a/send.py&lt;/span&gt;
  &lt;span class="ansi1"&gt;+++ b/send.py&lt;/span&gt;
  &lt;span class="ansi36"&gt;@@ -49,14 +49,12 @@&lt;/span&gt; class EMail(object):
                          server.quit()
  
          def prepareMail(self, From, to, subject, msgHTML, attachments):
  &lt;span class="ansi31"&gt;-              msg = MIMEMultipart('alternative')&lt;/span&gt;
  &lt;span class="ansi32"&gt;+&lt;/span&gt;               &lt;span class="ansi32"&gt;msg = MIMEMultipart()&lt;/span&gt;
                  msg['From'] = From
                  msg['To'] = to
                  msg['Date'] = formatdate(localtime=True)
                  msg['Subject'] = subject
  
  &lt;span class="ansi31"&gt;-              print msgHTML&lt;/span&gt;
  &lt;span class="ansi31"&gt;-&lt;/span&gt;
                  #The Body message
                  msg.attach(MIMEText(msgHTML, 'html'))
                  msg.attach(MIMEText("Sent from maya by Mini Me"))
  -- 
  1.8.3.4 (Apple Git-47)
  
  
  &lt;/pre&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>it seems only pre tag formatting is working not the css .. why would that be ?</p>