Re: Python To Send Emails Via Outlook Express
ian@kirbyfooty. com writes:
[color=blue]
> 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)
>[color=green][color=darkred]
> >>> import simplemapi
> >>> simplemapi.Send Mail("ian@cgbs. com.au","The Subject","The[/color][/color]
> body","c:\ian\i an.txt")
> nFileCount 14
> Traceback (most recent call last):
> File "<interacti ve input>", line 1, in ?
> File "simplemapi.py" , line 111, in SendMail
> raise WindowsError, "MAPI error %i" % rc
> WindowsError: MAPI error 2
>[/color]
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:\ia n")
simplemapi.Send Mail("ian@cgbs. com.au", "The Subject",
"The body", ["ian.txt"])
[color=blue]
>
> 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(S tructure):
> _fields_ = [('ulReserved', c_ulong),
> ('ulRecipClass' , c_ulong),
> ('lpszName', c_char_p),
> ('lpszAddress', c_char_p),
> ('ulEIDSize', c_ulong),
> ('lpEntryID', c_void_p),
> ]
> lpMapiRecipDesc = POINTER(MapiRec ipDesc)
>
>
> class MapiFileDesc(St ructure):
> _fields_ = [('ulReserved', c_ulong),
> ('flFlags', c_ulong),
> ('nPosition', c_ulong),
> ('lpszPathName' , c_char_p),
> ('lpszFileName' , c_char_p),
> ('lpFileType', c_void_p),
> ]
> lpMapiFileDesc = POINTER(MapiFil eDesc)
>
>
> class MapiMessage(Str ucture):
> _fields_ = [('ulReserved', c_ulong),
> ('lpszSubject', c_char_p),
> ('lpszNoteText' , c_char_p),
> ('lpszMessageTy pe', c_char_p),
> ('lpszDateRecei ved', c_char_p),
> ('lpszConversat ionID', c_char_p),
> ('flFlags', FLAGS),
> ('lpOriginator' , lpMapiRecipDesc ), # ignored?
> ('nRecipCount', c_ulong),
> ('lpRecips', lpMapiRecipDesc ),
> ('nFileCount', c_ulong),
> ('lpFiles', lpMapiFileDesc) ,
> ]
> lpMapiMessage = POINTER(MapiMes sage)
>
>
> MAPI = windll.mapi32
>
>
> MAPISendMail=MA PI.MAPISendMail
> MAPISendMail.re stype = c_ulong # Error code
> MAPISendMail.ar gtypes = (LHANDLE, # lhSession
> c_ulong, # ulUIParam
> lpMapiMessage, # lpMessage
> FLAGS, # lpFlags
> c_ulong, # ulReserved
> )
>
>
> def SendMail(recipi ent, 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 ",nFileCoun t
>
>
> 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.
>[/color]
And the same to you.
Lenard Lindstrom
<len-l@telus.net>
ian@kirbyfooty. com writes:
[color=blue]
> 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)
>[color=green][color=darkred]
> >>> import simplemapi
> >>> simplemapi.Send Mail("ian@cgbs. com.au","The Subject","The[/color][/color]
> body","c:\ian\i an.txt")
> nFileCount 14
> Traceback (most recent call last):
> File "<interacti ve input>", line 1, in ?
> File "simplemapi.py" , line 111, in SendMail
> raise WindowsError, "MAPI error %i" % rc
> WindowsError: MAPI error 2
>[/color]
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:\ia n")
simplemapi.Send Mail("ian@cgbs. com.au", "The Subject",
"The body", ["ian.txt"])
[color=blue]
>
> 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(S tructure):
> _fields_ = [('ulReserved', c_ulong),
> ('ulRecipClass' , c_ulong),
> ('lpszName', c_char_p),
> ('lpszAddress', c_char_p),
> ('ulEIDSize', c_ulong),
> ('lpEntryID', c_void_p),
> ]
> lpMapiRecipDesc = POINTER(MapiRec ipDesc)
>
>
> class MapiFileDesc(St ructure):
> _fields_ = [('ulReserved', c_ulong),
> ('flFlags', c_ulong),
> ('nPosition', c_ulong),
> ('lpszPathName' , c_char_p),
> ('lpszFileName' , c_char_p),
> ('lpFileType', c_void_p),
> ]
> lpMapiFileDesc = POINTER(MapiFil eDesc)
>
>
> class MapiMessage(Str ucture):
> _fields_ = [('ulReserved', c_ulong),
> ('lpszSubject', c_char_p),
> ('lpszNoteText' , c_char_p),
> ('lpszMessageTy pe', c_char_p),
> ('lpszDateRecei ved', c_char_p),
> ('lpszConversat ionID', c_char_p),
> ('flFlags', FLAGS),
> ('lpOriginator' , lpMapiRecipDesc ), # ignored?
> ('nRecipCount', c_ulong),
> ('lpRecips', lpMapiRecipDesc ),
> ('nFileCount', c_ulong),
> ('lpFiles', lpMapiFileDesc) ,
> ]
> lpMapiMessage = POINTER(MapiMes sage)
>
>
> MAPI = windll.mapi32
>
>
> MAPISendMail=MA PI.MAPISendMail
> MAPISendMail.re stype = c_ulong # Error code
> MAPISendMail.ar gtypes = (LHANDLE, # lhSession
> c_ulong, # ulUIParam
> lpMapiMessage, # lpMessage
> FLAGS, # lpFlags
> c_ulong, # ulReserved
> )
>
>
> def SendMail(recipi ent, 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 ",nFileCoun t
>
>
> 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.
>[/color]
And the same to you.
Lenard Lindstrom
<len-l@telus.net>
Comment