Python To Send Emails Via Outlook Express

I

ian

Heavy sigh... <grin>

======
This script WILL send the email
import win32com.client


s = win32com.client.Dispatch('CDO.Message')
c = win32com.client.Dispatch('CDO.Configuration')
cdoSourceOutlookExpress = 2
c.Load(cdoSourceOutlookExpress)
s.Configuration = c
s.From = "(e-mail address removed)"
s.To = "(e-mail address removed)"
s.Subject = "The subject"


s.Send()
======


======
But if a change the TO email address to a yahoo address the server
rejects it
import win32com.client


s = win32com.client.Dispatch('CDO.Message')
c = win32com.client.Dispatch('CDO.Configuration')
cdoSourceOutlookExpress = 2
c.Load(cdoSourceOutlookExpress)
s.Configuration = c
s.From = "(e-mail address removed)"
s.To = "(e-mail address removed)"
s.Subject = "The subject"


s.Send()
======

It's official. I have given up sending emails any other way but via
smtp.
 
L

Lenard Lindstrom

Steve Holden said:
Unfortunately Outlook Express isn't programmable in the same way as
Outlook. I used to use it because this property gave it a certain
degree of immunity from macro viruses (back in the days when Outlook
came configured to open any Office document it came across).
Outlook Express can be accessed through the Simple Mapi interface -
at least the version on Win98 can. I have a program that reads
and deletes messages from the new mail folder. So I would imaging
mail can be also sent through Outlook Express using Simple Mapi,
but I have not tried it. I have also not tried using Simple Mapi
under Python, only VC++. But a quick test shows I can access
MAPI32.DLL use the ctypes package:

import ctypes
mapi = ctypes.windll.mapi32
MAPILogon = mapi.MAPILogon
....

It is messy though.

Lenard Lindstrom
<[email protected]>
 
I

ian

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
 
L

Lenard Lindstrom

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("(e-mail address removed)",
"The subject line"
"This is the message content.\n")


Lenard Lindstrom
<[email protected]>
 
I

ian

Hi Lenard,
Absolutely fantastic!!
That worked like a charm.
Now onto adapting it to send attachments.

Thanks again

Ian
 
I

ian

Hi Lenard,
Absolutely fantastic!!
That worked like a charm.
Now onto adapting it to send attachments.

Thanks again

Ian
 
I

ian

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..
----------------------------------------------------------------------------------------
import os
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, 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


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))


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc
 
L

Lenard Lindstrom

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
<[email protected]>
 
I

ian

Hello again,
Thanks for the advice!
Unfortunately I still cannot get it to send attachments.
It comes up with the following windows error..
(I have a feeling it has something to do with the file count)
body","c:\ian\ian.txt")
nFileCount 14
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "simplemapi.py", line 111, in SendMail
raise WindowsError, "MAPI error %i" % rc
WindowsError: MAPI error 2


This is the updated script..

----------------------------------
import os
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, 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
else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

print "nFileCount ",nFileCount


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, lpFiles)


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc

--------------------------

Thanks again for your help so far on this. I really appreciate it!
Have a safe and very merry Christmas.

God bless!!

Ian
 
L

Lenard Lindstrom

Hello again,
Thanks for the advice!
Unfortunately I still cannot get it to send attachments.
It comes up with the following windows error..
(I have a feeling it has something to do with the file count)

body","c:\ian\ian.txt")
nFileCount 14
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "simplemapi.py", line 111, in SendMail
raise WindowsError, "MAPI error %i" % rc
WindowsError: MAPI error 2

Two problems:

1) SendMail is looking for a list of attachment files.
2) SendMail wants the attachments to be in the current
working directory; that is what the:

attach = map( os.path.abspath, attach )

is about, right?

Try this:

import simplemapi
import os
os.chdir("c:\ian")
simplemapi.SendMail("(e-mail address removed)", "The Subject",
"The body", ["ian.txt"])
This is the updated script..

----------------------------------
import os
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, 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
else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

print "nFileCount ",nFileCount


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, lpFiles)


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc
And the same to you.

Lenard Lindstrom
<[email protected]>
 
I

ian

Hi Lenard,
You just beat me to it.
Suprise, suprise, I discovered the answer myself this time.

I have modified the script to allow the attachment(s) to still be
passed as a string.
Some error checking is also done to verify the attachment file exists.

I have also modified it so it can be used for multiple email addresses.

Here is the updated working script ...
---------------------------------------------------------

import os
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, attachfiles):
"""Post an e-mail message using Simple MAPI


recipient - string: address to send to (multiple address sperated
with a semicolin)
subject - string: subject header
body - string: message text
attach - string: files to attach (multiple attachments sperated
with a semicolin)

Example usage
import simplemapi

simplemapi.SendMail("(e-mail address removed);[email protected]","My
Subject","My message body","c:\attachment1.txt;c:\attchment2")


"""

# get list of file attachments
attach = []
AttachWork = attachfiles.split(';')

#verify the attachment file exists
for file in AttachWork:
if os.path.exists(file):
attach.append(file)


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
else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

# Get the number of recipients
RecipWork = recipient.split(';')
RecipCnt = len(RecipWork)

# Formulate the recipients
MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
rda = MapiRecipDesc_A()
for rd, ra in zip(rda, RecipWork):
rd.ulReserved = 0
rd.ulRecipClass = MAPI_TO
rd.lpszName = None
rd.lpszAddress = ra
rd.ulEIDSize = 0
rd.lpEntryID = None
recip = rda

# send the message
msg = MapiMessage(0, subject, body, None, None, None, 0,
cast(NULL, lpMapiRecipDesc), RecipCnt, recip,
nFileCount, lpFiles)


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc
-----------------------------------
 
L

Lenard Lindstrom

Hi Lenard,
You just beat me to it.
Suprise, suprise, I discovered the answer myself this time.

I have modified the script to allow the attachment(s) to still be
passed as a string.
Some error checking is also done to verify the attachment file exists.

I have also modified it so it can be used for multiple email addresses.

Here is the updated working script ...
---------------------------------------------------------

import os
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, attachfiles):
"""Post an e-mail message using Simple MAPI


recipient - string: address to send to (multiple address sperated
with a semicolin)
subject - string: subject header
body - string: message text
attach - string: files to attach (multiple attachments sperated
with a semicolin)

Example usage
import simplemapi

simplemapi.SendMail("(e-mail address removed);[email protected]","My
Subject","My message body","c:\attachment1.txt;c:\attchment2")


"""

# get list of file attachments
attach = []
AttachWork = attachfiles.split(';')

#verify the attachment file exists
for file in AttachWork:
if os.path.exists(file):
attach.append(file)


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
else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

# Get the number of recipients
RecipWork = recipient.split(';')
RecipCnt = len(RecipWork)

# Formulate the recipients
MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
rda = MapiRecipDesc_A()
for rd, ra in zip(rda, RecipWork):
rd.ulReserved = 0
rd.ulRecipClass = MAPI_TO
rd.lpszName = None
rd.lpszAddress = ra
rd.ulEIDSize = 0
rd.lpEntryID = None
recip = rda

# send the message
msg = MapiMessage(0, subject, body, None, None, None, 0,
cast(NULL, lpMapiRecipDesc), RecipCnt, recip,
nFileCount, lpFiles)


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc
-----------------------------------

Looks good.

Lenard Lindstrom
 
M

Max M

Lenard said:
Hi Lenard,
You just beat me to it.
Suprise, suprise, I discovered the answer myself this time.

I have modified the script to allow the attachment(s) to still be
passed as a string.
Some error checking is also done to verify the attachment file exists.

I have also modified it so it can be used for multiple email addresses.

Here is the updated working script ...
---------------------------------------------------------

import os
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, attachfiles):
"""Post an e-mail message using Simple MAPI


recipient - string: address to send to (multiple address sperated
with a semicolin)
subject - string: subject header
body - string: message text
attach - string: files to attach (multiple attachments sperated
with a semicolin)

Example usage
import simplemapi

simplemapi.SendMail("(e-mail address removed);[email protected]","My
Subject","My message body","c:\attachment1.txt;c:\attchment2")


"""

# get list of file attachments
attach = []
AttachWork = attachfiles.split(';')

#verify the attachment file exists
for file in AttachWork:
if os.path.exists(file):
attach.append(file)


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
else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

# Get the number of recipients
RecipWork = recipient.split(';')
RecipCnt = len(RecipWork)

# Formulate the recipients
MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
rda = MapiRecipDesc_A()
for rd, ra in zip(rda, RecipWork):
rd.ulReserved = 0
rd.ulRecipClass = MAPI_TO
rd.lpszName = None
rd.lpszAddress = ra
rd.ulEIDSize = 0
rd.lpEntryID = None
recip = rda

# send the message
msg = MapiMessage(0, subject, body, None, None, None, 0,
cast(NULL, lpMapiRecipDesc), RecipCnt, recip,
nFileCount, lpFiles)


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc
-----------------------------------


Looks good.

Lenard Lindstrom

Nice quoting

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
S

Steve Holden

[sixty or more lines, mostly code ...]
Nice quoting
So what is this, some kind of competition? If you really though Lenard's
quoting was a sin (since I took your remarks to be sardonic), how much
more so was your gratuitous repetition thereof?

Far more pleasant to either a) ignore a gaff by a possibly
less-experienced usenet correspondent than yourself, or b) point out the
error without repeating it, as I hope I have done here.

regards
Steve
 
I

ian

Hey guys, I'm just thankful the answer has been found and hope this
helps someone else. To everyone (especially Lenard) that responded to
my request for help, thank you!!
Merry Christmas everyone!!!
God bless
Ian
 
M

Max M

So what is this, some kind of competition? If you really though Lenard's
quoting was a sin (since I took your remarks to be sardonic), how much
more so was your gratuitous repetition thereof?

I thought that showing by example might have a better effect than just
grumping.

Far more pleasant to either a) ignore a gaff by a possibly
less-experienced usenet correspondent than yourself, or b) point out the
error without repeating it, as I hope I have done here.

You are right.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
D

David Fraser

Hey guys, I'm just thankful the answer has been found and hope this
helps someone else. To everyone (especially Lenard) that responded to
my request for help, thank you!!
Merry Christmas everyone!!!
God bless
Ian
Thanks Ian, Why not post this to the python-win32 mailing list as well,
then maybe your changes can be incorporated into pywin32?

Happy christmas
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top