Python To Send Emails Via Outlook Express

Lenard Lindstrom len-1 at telus.net
Thu Dec 23 15:34:18 EST 2004


ian at kirbyfooty.com writes:

> Hi Lenard,
> 
> As the risk of severely embarassing myself can I ask for your help one
> more time.
> I have tried changing your script to include attachments, but guess
> what, (and this should come as no suprise) I can't do it.
> So....
> Here is my feeble attempt at changing the script..

Actually the only real problem is with the lpFiles argument
to the MapiMessage constructor call.

> ----------------------------------------------------------------------------------------
[snip]
> 
> def SendMail(recipient, subject, body, attach=[]):
> """Post an e-mail message using Simple MAPI
> 
> 
> recipient - string: address to send to
> subject - string: subject header
> body - string: message text
> attach - string: files to attach
> """
> attach = map( os.path.abspath, attach )
> nFileCount = len(attach)
> if attach:
> MapiFileDesc_A = MapiFileDesc * len(attach)
> fda = MapiFileDesc_A()
> for fd, fa in zip(fda, attach):
> fd.ulReserved = 0
> fd.flFlags = 0
> fd.nPosition = -1
> fd.lpszPathName = fa
> fd.lpszFileName = None
> fd.lpFileType = None
> lpFiles = fda

Add this else clause to the "if attach:"

    else:
        # No attachments
        lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

> 
> 
> recip = MapiRecipDesc(0, MAPI_TO, None, recipient, 0, None)
> #msg = MapiMessage(0, subject, body, None, None, None, 0,
> #                 cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
> #                 nFileCount, cast(NULL, lpMapiFileDesc))
> msg = MapiMessage(0, subject, body, None, None, None, 0,
> cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
> nFileCount, cast(NULL, lpFiles))

Replace "cast(NULL, lpFiles)" with "lpFiles".
> 
> 
> rc = MAPISendMail(0, 0, byref(msg), 0, 0)
> if rc != SUCCESS_SUCCESS:
> raise WindowsError, "MAPI error %i" % rc

And that is it.

The "cast(NULL, lpFiles)" was a way of assigning a null value to
the lpFiles member of the MapiMessage structure. Using None did
not work. But recasting a c_void_p(None) did.

Lenard Lindstrom
<len-l at telus.net>



More information about the Python-list mailing list