Sending an HTML rich email using python
Question: 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.

So far here is the 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) <=3: subjAdd = ','.join(files)
                            if len(files) > 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
    

and here is the HTML formatted text I am sending, (I have removed the HTML
head section it being long due to css)

    
    
    <body class="body_foreground body_background" style="font-size: normal;" >
    <pre>
    
    ---
     send.py | 4 <span class="ansi32">+</span><span class="ansi31">---</span>
     1 file changed, 1 insertion(+), 3 deletions(-)
    
    <span class="ansi1">diff --git a/send.py b/send.py</span>
    <span class="ansi1">index 87126d5..abb1fd8 100644</span>
    <span class="ansi1">--- a/send.py</span>
    <span class="ansi1">+++ b/send.py</span>
    <span class="ansi36">@@ -49,14 +49,12 @@</span> class EMail(object):
                            server.quit()
    
            def prepareMail(self, From, to, subject, msgHTML, attachments):
    <span class="ansi31">-              msg = MIMEMultipart('alternative')</span>
    <span class="ansi32">+</span>               <span class="ansi32">msg = MIMEMultipart()</span>
                    msg['From'] = From
                    msg['To'] = to
                    msg['Date'] = formatdate(localtime=True)
                    msg['Subject'] = subject
    
    <span class="ansi31">-              print msgHTML</span>
    <span class="ansi31">-</span>
                    #The Body message
                    msg.attach(MIMEText(msgHTML, 'html'))
                    msg.attach(MIMEText("Sent from maya by Mini Me"))
    -- 
    1.8.3.4 (Apple Git-47)
    
    
    </pre>
    </body>
    
    </html>
    

it seems only pre tag formatting is working not the css .. why would that be ?


Answer: Gmail doesn't support `<style>` blocks. You can see a css support comparison
between popular mail clients here: [Guide to CSS support in
email](http://www.campaignmonitor.com/css/).

