Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to test sending an html table in the body of an outlook email, using ntlm, urllib2 and some other stuff. I can send email text successfully, but not html. Please have a look at my code and see if you can spot the error. Thanks very much!

import urllib2
from ntlm import ntlm
from ntlm import HTTPNtlmAuthHandler
import urllib2
from urllib2 import URLError
from datetime import *
from html import HTML
import smtplib

h = HTML()
t = h.table(border='1')
for i in range(2):
   r = t.tr
   r.td('column 1')
   r.td('column 2')

h=str(h)

url = "https://corporateemail..."


passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)

#create NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

#create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

xml = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
  <soap:Body>
    <CreateItem MessageDisposition="SendAndSaveCopy" xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
      <SavedItemFolderId>
        <t:DistinguishedFolderId Id="drafts" />
      </SavedItemFolderId>
      <Items>
        <t:Message>
          <t:ItemClass>IPM.Note</t:ItemClass>
          <t:Subject>Testing Email</t:Subject>
          <t:Body BodyType="HTML">%s</t:Body>
          <t:ToRecipients>
            <t:Mailbox>
              <t:EmailAddress>[email protected]</t:EmailAddress>
            </t:Mailbox>
          </t:ToRecipients>
          <t:IsRead>false</t:IsRead>
        </t:Message>
      </Items>
    </CreateItem>
  </soap:Body>
</soap:Envelope>''' % h

req = urllib2.Request(url=url, data=xml, headers={'content-type': 'html/xml'})

response = urllib2.urlopen(req)
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.