3

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 ?

4
  • Not all email clients are capable to render css, some just render inline styles Commented Jan 25, 2014 at 7:57
  • @furins : what about gmail ? Commented Jan 25, 2014 at 8:14
  • see zord's answer (as of today: no, it does not support style tags in head nor in body). In general, css in email is tricky and should be minimal or can gracefully degrade without compromising the readability of your message. Please also take in account that your email should not be larger than 600px to fit some online browsers (e.g. any image should not be wider than that size). Eventually a tool like putsmail.com may help you. Commented Jan 25, 2014 at 16:39
  • well, I figured out a way to embed style inline from python for git diff which I will share in separate thread and link up here. Commented Jan 25, 2014 at 17:08

1 Answer 1

10

Gmail doesn't support <style> blocks. You can see a css support comparison between popular mail clients here: Guide to CSS support in email.

Sign up to request clarification or add additional context in comments.

1 Comment

Gmail does support <style> blocks. At least now it does in 2021. But its still a bit goofy about it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.