Python To Send Emails Via Outlook Express

Lenard Lindstrom len-1 at telus.net
Wed Dec 22 00:33:13 EST 2004


ian at kirbyfooty.com writes:

> That sound really promising. Is there any chance you could forward me a
> copy of the script. I'm still very new to Python and it would help me a
> lot.
> Thanks again
> 
> Ian

This is a simple example I have put together:

=============== SimpleMAPI.py ======================
# module SimpleMAPI

from ctypes import *

FLAGS = c_ulong
LHANDLE = c_ulong
LPLHANDLE = POINTER(LHANDLE)

# Return codes
SUCCESS_SUCCESS = 0
# Recipient class
MAPI_ORIG = 0
MAPI_TO = 1

NULL = c_void_p(None)

class MapiRecipDesc(Structure):
    _fields_ = [('ulReserved', c_ulong),
                ('ulRecipClass', c_ulong),
                ('lpszName', c_char_p),
                ('lpszAddress', c_char_p),
                ('ulEIDSize', c_ulong),
                ('lpEntryID', c_void_p),
               ]
lpMapiRecipDesc = POINTER(MapiRecipDesc)

class MapiFileDesc(Structure):
    _fields_ = [('ulReserved', c_ulong),
                ('flFlags', c_ulong),
                ('nPosition', c_ulong),
                ('lpszPathName', c_char_p),
                ('lpszFileName', c_char_p),
                ('lpFileType', c_void_p),
               ]
lpMapiFileDesc = POINTER(MapiFileDesc)

class MapiMessage(Structure):
    _fields_ = [('ulReserved', c_ulong),
                ('lpszSubject', c_char_p),
                ('lpszNoteText', c_char_p),
                ('lpszMessageType', c_char_p),
                ('lpszDateReceived', c_char_p),
                ('lpszConversationID', c_char_p),
                ('flFlags', FLAGS),
                ('lpOriginator', lpMapiRecipDesc), # ignored?
                ('nRecipCount', c_ulong),
                ('lpRecips', lpMapiRecipDesc),
                ('nFileCount', c_ulong),
                ('lpFiles', lpMapiFileDesc),
               ]
lpMapiMessage = POINTER(MapiMessage)

MAPI = windll.mapi32

MAPISendMail=MAPI.MAPISendMail
MAPISendMail.restype = c_ulong          # Error code
MAPISendMail.argtypes = (LHANDLE,       # lhSession
                         c_ulong,       # ulUIParam
                         lpMapiMessage, # lpMessage
                         FLAGS,         # lpFlags
                         c_ulong,       # ulReserved
                         )

def SendMail(recipient, subject, body):
    """Post an e-mail message using Simple MAPI

    recipient - string: address to send to
    subject - string: subject header
    body - string: message text
    """

    recip = MapiRecipDesc(0, MAPI_TO, None, recipient, 0, None)
    msg = MapiMessage(0, subject, body, None, None, None, 0,
                      cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
                      0, cast(NULL, lpMapiFileDesc))
    rc = MAPISendMail(0, 0, byref(msg), 0, 0)
    if rc != SUCCESS_SUCCESS:
        raise WindowsError, "MAPI error %i" % rc
================= Example usage =========================
import SimpleMAPI
SimpleMAPI.SendMail("someone at somewhere.com",
                    "The subject line"
                    "This is the message content.\n")


Lenard Lindstrom
<len-l at telus.net>




More information about the Python-list mailing list