Python To Send Emails Via Outlook Express

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max M

    #16
    Re: Python To Send Emails Via Outlook Express

    ian@kirbyfooty. com wrote:[color=blue]
    > Hi Steve,[/color]
    [color=blue]
    > When it comes to sending emails the user has the option of sending them
    > via smtp, or via there email client (eg outlook express).
    > I prefer the send method as this makes setting up the email parameters
    > a lot easier for the user.[/color]


    If Outlook Express cannot be automated through COM, you are in abind.
    Maybe you should shange your tactics.

    What about just fetching the settings from the client?

    That way the send function will be the same, and you can control it. But
    the server settings could be fetched from the different clients.

    It is probably much simpler to find those than it is to use them as the
    sender.

    Most email clients can fetch the settings from other mail clients, so
    it's been done before.

    --

    hilsen/regards Max M, Denmark

    A small collection of CLAP synths and effects inspired by classic hardware.

    IT's Mad Science

    Comment

    • ian@kirbyfooty.com

      #17
      Re: Python To Send Emails Via Outlook Express

      Hi Max,
      Thanks for the suggestion. I'm always open to new ideas.

      Can you please tell me how to retrieve the default account settings
      from Outlook Express?

      Ian

      Comment

      • Ganesan R

        #18
        Re: Python To Send Emails Via Outlook Express

        >>>>> "ian" == ian <ian@kirbyfooty .com> writes:
        [color=blue]
        > Hi Max,
        > Thanks for the suggestion. I'm always open to new ideas.[/color]
        [color=blue]
        > Can you please tell me how to retrieve the default account settings
        > from Outlook Express?[/color]

        s.Configuration .Load(2) is supposed to do exactly that. I have no clue
        why that didn't work for you :-(.

        Ganesan

        Comment

        • ian@kirbyfooty.com

          #19
          Re: Python To Send Emails Via Outlook Express

          Hi Ganesan,
          I'm on the verge of giving up <sigh>
          I don't suppose you could write a small script to send the email for me
          via the default windows email client. I will then try running your
          script and my end to see if it works ok.
          I may have missed something and would really appreciate your help.
          Thanks in advance
          Ian

          Comment

          • Ganesan R

            #20
            Re: Python To Send Emails Via Outlook Express

            >>>>> "ian" == ian <ian@kirbyfooty .com> writes:
            [color=blue]
            > Hi Ganesan,
            > I'm on the verge of giving up <sigh>
            > I don't suppose you could write a small script to send the email for me
            > via the default windows email client.[/color]

            I can see no easy way to do this. Though you can access the default mail
            client using a registry key (I don't have enough win32 programming
            experience to do this), there is no guarantee that the email client is a COM
            server. Even Outlook Express is not a COM server. What if the user has
            configured Eudora or something like that?

            The best we can do is use Outlook Express settings and send the mail.
            [color=blue]
            > I will then try running your script and my end to see if it works ok. I
            > may have missed something and would really appreciate your help.[/color]

            The final script that you pasted:

            =====
            import win32com.client

            s = win32com.client .Dispatch('CDO. Message')
            s.From = "ian@cgbs.com.a u"
            s.To = "ian@kirbyfooty .com"
            s.Subject = "The subject"
            cdoSourceOutloo kExpress = 2
            s.Configuration .Load(cdoSource OutlookExpress)
            s.Send()
            =====

            works fine for me. According to CDO.Message documentation, if IIS is
            installed that configuration will be used by default, followed by OE
            configuration. I don't have IIS installed, so the script picked up the OE
            configuration automatically for me. Here's a variation of the above
            script.

            ======
            import win32com.client

            s = win32com.client .Dispatch('CDO. Message')
            c = win32com.client .Dispatch('CDO. Configuration')
            cdoSourceOutloo kExpress = 2
            c.Load(cdoSourc eOutlookExpress )
            s.Configuration = c
            s.From = "ian@cgbs.com.a u"
            s.To = "ian@kirbyfooty .com"
            s.Subject = "The subject"

            s.Send()
            ======

            If that doesn't help, I give up :-(.

            Ganesan

            Comment

            • ian@kirbyfooty.com

              #21
              Re: Python To Send Emails Via Outlook Express

              Heavy sigh... <grin>

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


              s = win32com.client .Dispatch('CDO. Message')
              c = win32com.client .Dispatch('CDO. Configuration')
              cdoSourceOutloo kExpress = 2
              c.Load(cdoSourc eOutlookExpress )
              s.Configuration = c
              s.From = "i...@cgbs.com. au"
              s.To = "i...@kirbyfoot y.com"
              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')
              cdoSourceOutloo kExpress = 2
              c.Load(cdoSourc eOutlookExpress )
              s.Configuration = c
              s.From = "i...@cgbs.com. au"
              s.To = "iwcook@yahoo.c om"
              s.Subject = "The subject"


              s.Send()
              ======

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

              Comment

              • Lenard Lindstrom

                #22
                Re: Python To Send Emails Via Outlook Express

                Steve Holden <steve@holdenwe b.com> writes:
                [color=blue]
                > ian@kirbyfooty. com wrote:
                >[color=green]
                > > Hi Fredrik,
                > > Thank you for the suggestion.
                > > I tried different from/to settings and guess what? The mail came
                > > thru.
                > > The script is now..
                > > import win32com.client
                > > s = win32com.client .Dispatch('CDO. Message')
                > > s.From = "ian@cgbs.com.a u" (was
                > > "ian@kirbyfooty .com")
                > > s.To = "ian@kirbyfooty .com" (was
                > > "someone@yahoo. com")
                > > s.Subject = "The subject"
                > > s.Send()
                > > My problem is thought, the message is still not being sent via
                > > Outlook
                > > Express.
                > > What am I missing?
                > > Thanks again for your help so far!!
                > > Kind regards
                > > Ian
                > >[/color]
                > 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).
                >[/color]
                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.m api32
                MAPILogon = mapi.MAPILogon
                ....

                It is messy though.

                Lenard Lindstrom
                <len-l@telus.net>

                Comment

                • ian@kirbyfooty.com

                  #23
                  Re: Python To Send Emails Via Outlook Express

                  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

                  Comment

                  • Lenard Lindstrom

                    #24
                    Re: Python To Send Emails Via Outlook Express

                    ian@kirbyfooty. com writes:
                    [color=blue]
                    > 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[/color]

                    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(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):
                    """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.Send Mail("someone@s omewhere.com",
                    "The subject line"
                    "This is the message content.\n")


                    Lenard Lindstrom
                    <len-l@telus.net>

                    Comment

                    • ian@kirbyfooty.com

                      #25
                      Re: Python To Send Emails Via Outlook Express

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

                      Thanks again

                      Ian

                      Comment

                      • ian@kirbyfooty.com

                        #26
                        Re: Python To Send Emails Via Outlook Express

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

                        Thanks again

                        Ian

                        Comment

                        • Lenard Lindstrom

                          #27
                          Re: Python To Send Emails Via Outlook Express

                          ian@kirbyfooty. com writes:
                          [color=blue]
                          > Hi Lenard,
                          > Absolutely fantastic!!
                          > That worked like a charm.
                          > Now onto adapting it to send attachments.
                          >[/color]
                          Glad to be of help.

                          Lenard Lindstrom

                          Comment

                          • ian@kirbyfooty.com

                            #28
                            Re: Python To Send Emails Via Outlook Express

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


                            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

                            Comment

                            • Lenard Lindstrom

                              #29
                              Re: Python To Send Emails Via Outlook Express

                              ian@kirbyfooty. com writes:
                              [color=blue]
                              > 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..[/color]

                              Actually the only real problem is with the lpFiles argument
                              to the MapiMessage constructor call.
                              [color=blue]
                              > ----------------------------------------------------------------------------------------[/color]
                              [snip][color=blue]
                              >
                              > 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[/color]

                              Add this else clause to the "if attach:"

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

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

                              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@telus.net>

                              Comment

                              • ian@kirbyfooty.com

                                #30
                                Re: Python To Send Emails Via Outlook Express

                                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=blue][color=green][color=darkred]
                                >>> import simplemapi
                                >>> simplemapi.Send Mail("ian@cgbs. com.au","The Subject","The[/color][/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


                                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.

                                God bless!!

                                Ian

                                Comment

                                Working...