Dotaz

Dobrý deň , V svojom programe používam funkciu, ktorá spustí predvoleného emailového klienta, vypíše text správy, vyplní sa predmet a príjemca.:

  os.startfile('mailto:tomasiol.sk?subject=Sprava&body=text')

Ak je text správy dlhší ako 2000 znakov tak mi vypíše WindowsError pristup bol odporen.

Nevie niekto ako by som to mohol vyriešiť?

Prostý mail

Mírně zjednodušený příklad z Helpu:

  import smtplib

  odesilatel = "ilovepy@post.cz"
  prijemce  = "ilovepytoo@post.cz" 
  predmet="ahoj"

  # Add the From: and To: headers at the start!
  msg = "From: %s\r\nSubject: %s\r\nTo: %s\r\n\r\n" % (odesilatel, predmet, prijemce)
  zprava="Ahoj jak se mas. "*1000
  msg=msg+zprava
  print "Zprava: \n\n", msg
  print "\nZprava je dlouha", len(msg),"bitu\n"

  server = smtplib.SMTP('smtp.seznam.cz')     # zde zadejte svůj smtp server
  server.login('....','...')                  # vaše uzivatelské jmeno a heslo pro odesílání - někdy není tato autentizace potřeba
  server.sendmail(odesilatel, prijemce, msg)
  server.quit()

Mail s přílohami

Opsáno od http://www.bigbold.com/snippets/posts/show/2038 :

    import smtplib, os
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.Utils import formatdate
    from email import Encoders

    def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):

        msg = MIMEMultipart()
        msg['From'] = send_from
        msg['To'] = send_to
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject

        msg.attach(MIMEText(text))

        for fileOne in files:
            part = MIMEBase('application', "octet-stream")
            part.set_payload( file(fileOne,"rb").read() )
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(fileOne))
            msg.attach(part)

        smtp = smtplib.SMTP(server)
        # smtp.login(jmeno, heslo)  # jen kdyz je treba
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.close()

Otevření výchozího e-mailového klienta (Windows)

Zkoušel jsem kde co (ač by to mělo být vcelku snadné, narážíte na spoustu problémů - no Wokna, co by člověk nechtěl :-) ), nakonec pomohl modul simplemapi.py :

    import os
    from ctypes import *

    MAPI_LOGON_UI = 0x00000001
    MAPI_NEW_SESSION = 0x00000002
    MAPI_DIALOG = 0x00000008

    class MapiRecipDesc(Structure):
        _fields_ = [
            ("ulReserved", c_uint),
            ("ulRecipClass", c_uint),
            ("lpszName", c_char_p),
            ("lpszAddress", c_char_p),
            ("ulEIDSize", c_uint),
            ("lpEntryID", c_void_p),
        ]

    class MapiFileDesc(Structure):
        _fields_ = [
            ("ulReserved", c_uint),
            ("flFlags", c_uint),
            ("nPosition", c_int),
            ("lpszPathName", c_char_p),
            ("lpszFileName", c_char_p),
            ("lpFileType", c_void_p),
        ]

    class MapiMessage(Structure):
        _fields_ = [
            ("ulReserved", c_uint),
            ("lpszSubject", c_char_p),
            ("lpszNoteText", c_char_p),
            ("lpszMessageType", c_char_p),
            ("lpszDateReceived", c_char_p),
            ("lpszConversationID", c_char_p),
            ("flFlags", c_uint),
            ("lpOriginator", POINTER(MapiRecipDesc)),
            ("nRecipCount", c_uint),
            ("lpRecips", POINTER(MapiRecipDesc)),
            ("nFileCount", c_uint),
            ("lpFiles", POINTER(MapiFileDesc)),
        ]

    def mapiSend( subj, body, addresses, attach=[] ):
        """mapiSend( subj, body, addresses, attach )

        subj = "simple mapi",
        body = "first self-sending prigram :)\nregards",
        addresses = [(1,'a@example.invalid'),(2,'b@example.invalid')],
        attach = ['ct_mapi.py'],
        """
        attach = map( os.path.abspath, attach )

        MapiRecipDesc_A = MapiRecipDesc * len(addresses)
        to = MapiRecipDesc_A()
        for ra,a in zip(to,addresses):
            ra.ulReserved = 0
            ra.ulRecipClass = a[0]
            ra.lpszName = None
            ra.lpszAddress = a[1]

        msg = MapiMessage()
        msg.ulReserved = 0
        msg.lpszSubject = subj
        msg.lpszNoteText = body
        msg.lpszMessageType = None
        msg.lpszDateReceived = None
        msg.lpszConversationID = None
        msg.flFlags = 0

        msg.nRecipCount = len(addresses)
        msg.lpRecips = to
        msg.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
            msg.lpFiles = fda

        mapi = windll.mapi32
        err = mapi.MAPISendMail( 0, 0, byref(msg),
            MAPI_LOGON_UI|MAPI_DIALOG, 0 )
        if err:
            raise RuntimeError, err

    if __name__=='__main__':
        mapiSend( subj = "simple mapi",
                body = """
    first self-sending program :)
    maybe for inclusion in ctypes?

    regards,
    Niki Spahiev
    """,
                addresses = [
                    (1,'ctypes-users@lists.sourceforge.net'),
                    (2,'python-list@python.org')],
                attach = ['ct_mapi.py'],
        )