Create an ActiveSync SendMail request in python

1k views Asked by At

I am trying to implement simple EAS client functionality in python and making use of this library: https://code.google.com/p/py-eas-client/

I can successfully Provision, FolderSync, Sync, and Fetch to read messages and download attachments. The library has built in functions for those commands, but not for SendMail. So I'm trying to implement SendMail myself:

def sendmail(self):
    send_url = self.add_parameters(self.get_url(),
                                   {"Cmd": "SendMail", "User": self.username, "DeviceId": self.device_id,
                                    "DeviceType": self.device_type})
    d = self.agent.request(
        'POST',
        send_url,
        Headers({'User-Agent': ['python-EAS-Client 1.0'],
                 'Host': [self.server],
                 'MS-ASProtocolVersion': [self.server_version],
                 'X-MS-PolicyKey': [str(self.policy_key)],
                 'Content-Type': ["application/vnd.ms-sync.wbxml"],
                 'Authorization': [self.authorization_header()]}),
        SendMailProducer())
    d.addCallback(self.wbxml_response)
    return d



And then here is the SendMail wbxml producer:

class SendMailProducer(WBXMLProducer):
    def __init__(self):
        msg = """From: [email protected]
To: [email protected]
Subject: From NSync
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
This is the body text."""
        wbdict = {
        "SendMail": [
            ("ClientId", "12312312312352173263123"),                   
            ("Mime", msg)
        ]
        }

        wb = convert_dict_to_wbxml(wbdict, default_page_num=3)
        print wb
        return WBXMLProducer.__init__(self, wb, verbose=True)



And in dewbxml.py, I had to add support for the ComposeMail namespace:

{ # Page 3 - ComposeMail
            "name":"ComposeMail",
                0x05: ('SendMail', None),
                0x10: ('Mime', None),
                0x11: ('ClientId', None)
            }



Using this I get a 503 error response. I'm almost certain I am not preparing the wbxml correctly. Provided that I can successfully obtain a Policy Key, is there a simpler way to execute activesync SendMail in python? I have not been able to find any other way to encode wbxml. Thanks for any help.

1

There are 1 answers

0
decentjam On BEST ANSWER

Figured it out, it was an issue with the wbxml. Using the library libwbxml works fine. But unfortunately there's no wrapper in python