using the email module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik Johnson

    #1

    using the email module


    THE GOAL: I need to send an email with a simple ASCII text body and an
    attached HTML file.

    I have scripts that send basic emails via the smtplib module that don't
    have any attachements and that seems to work fine. I first looked at the
    mimetools modules but it says it is depreceated since 2.3, so I started
    trying to use the email module. Here is a script that basically follows the
    second example given in section 7.1.13 of the Python Library Reference
    (http://docs.python.org/lib/node162.html). (***Note that there are some
    mistakes in that documentation about module names.***)

    When I run this and view the email I receive in MS Outlook Express, what
    I see is the HTML rendered in the body of the message, my body is not seen
    anywhere and there is no attachment. (Note: I like to bash MS as much as
    anyone: the reality is I need to work with this client - save it)

    I have not read the whole spec for RFC 822 and don't profess to
    understand it, but that's the point of having modules like email, right? Am
    I misunderstandin g something about how I am using 'email' here or is this
    module not working right? Does this give you an email with an HTML file
    attachement on your client? Can someone kindly point out what I've done
    wrong or give me a working example?

    Thanks!
    -ej

    Here's the script (put in your own email and server if you want to run
    it), and the text (with some identifying info stripped) of the email I
    receive is below that:


    #!/usr/bin/env python

    import smtplib
    from email.MIMEText import MIMEText
    from email.MIMEMulti part import MIMEMultipart


    # GLOBAL DATA
    #=============
    MAIL_SERVER = 'your_server.co m'
    MAIL_SUBJECT = "Python.SMT P email test"
    MAIL_TO = 'your_addr@exam ple.com'
    MAIL_FROM = "Python.SMT P email test"


    # Create a text/plain message

    Body = """\
    This is intended to be the body of my email. The HTML below should not
    be seen directly in the body but should be a separate attachment.
    """

    msg = MIMEMultipart()
    msg['Subject'] = MAIL_SUBJECT
    msg['From'] = MAIL_FROM
    msg['To'] = MAIL_TO
    msg.preamble = Body


    html = """\
    <html>
    <head>
    <title>Sample HTML File</title>
    </head>

    <body>
    <h1>Can you see this?</h1>
    <p>This is a short paragraph.</p>
    </body>

    </html>
    """
    msg.attach(MIME Text(html, 'html'))
    # print msg.as_string()


    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    smtp = smtplib.SMTP(MA IL_SERVER)
    smtp.sendmail(M AIL_FROM, [MAIL_TO], msg.as_string() )
    smtp.close()
    # end of python script


    Here's the text of the email I received:


    Return-Path: <Python.SMTP>
    Delivered-To: my_addr@example .com
    <several Received headers stripped>
    Content-Type: multipart/mixed; boundary="===== ==========16694 50343=="
    MIME-Version: 1.0
    Subject: Python.SMTP email test
    From: Python.SMTP email test
    To: my_addr@example .com

    This is intended to be the body of my email. The HTML below should not
    be seen directly in the body but should be a separate attachment.
    --=============== 1669450343==
    Content-Type: text/html; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit

    <html>
    <head>
    <title>Sample HTML File</title>
    </head>

    <body>
    <h1>Can you see this?</h1>
    <p>This is a short paragraph.</p>
    </body>

    </html>

    --=============== 1669450343==--



  • Sybren Stuvel

    #2
    Re: using the email module

    Erik Johnson enlightened us with:
    When I run this and view the email I receive in MS Outlook Express,
    what I see is the HTML rendered in the body of the message, my body
    is not seen anywhere and there is no attachment.
    If the HTML document should really be attached, give it a
    Content-Disposition: Attachment
    header. Check out the accompanying headers as well, by simply emailing
    yourself an attached HTML file and examining the email source.

    Sybren
    --
    Sybren Stüvel
    Stüvel IT - http://www.stuvel.eu/

    Comment

    • Erik Johnson

      #3
      Re: using the email module


      "Sybren Stuvel" wrote:
      If the HTML document should really be attached, give it a
      Content-Disposition: Attachment
      header. Check out the accompanying headers as well, by simply emailing
      yourself an attached HTML file and examining the email source.
      html = """\
      <html>
      ....
      </html>
      """
      attachment = MIMEText(html, 'html')
      attachment['Content-Disposition'] = 'attachment; filename="sampl e.html"'
      msg.attach(atta chment)


      # Ah! Yes, that works! Thank you! ;)


      Comment

      • Sybren Stuvel

        #4
        Re: using the email module

        Erik Johnson enlightened us with:
        # Ah! Yes, that works! Thank you! ;)
        You're welcome!

        Sybren
        --
        Sybren Stüvel
        Stüvel IT - http://www.stuvel.eu/

        Comment

        Working...