How to add an attachment with smtplib module

Discussion in 'Python' started by Sean Berry, Apr 28, 2004.

  1. Sean Berry

    Sean Berry Guest

    I have the sample script that uses the smtplib module
    that is available at python.org.

    How can I use this to send an attachment?

    Help is greatly appreciated.

    Thanks.

    --
    Sean Berry, Apr 28, 2004
    #1
    1. Advertising

  2. On Wed, 28 Apr 2004 20:22:32 GMT, (Sean Berry)
    declaimed the following in comp.lang.python:

    > I have the sample script that uses the smtplib module
    > that is available at python.org.
    >
    > How can I use this to send an attachment?
    >

    By brute force... You create the proper MIME headers to identify
    the presence of an attachment; you use the proper module(s) to convert
    the file of the attachment into an SMTP safe format (Base64?); you copy
    the attachment, with proper MIME boundary markers, into the body of the
    email.

    No doubt there is a simpler, more automated method, but by
    knowing what goes into the process, you can get more control over it.

    smtplib is merely the low level interface between a sending
    client and the receiving smtp daemon. The "msg" argument has to be a
    fully formed message with all headers and body.

    Check mimetools, mimify, and email modules.

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Apr 29, 2004
    #2
    1. Advertising

  3. Sean Berry <> wrote:
    > I have the sample script that uses the smtplib module
    > that is available at python.org.
    >
    > How can I use this to send an attachment?


    You don't. smtplib is a transport for an already formatted email
    message. You create a message with email package, like this:

    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    from email.MIMEImage import MIMEImage
    msg = MIMEMultipart()
    msg.attach(MIMEText(file("text.txt").read())
    msg.attach(MIMEImage(file("image.png").read())

    And then send it:

    import smtplib
    mailer = smtplib.SMTP()
    mailer.connect()
    mailer.sendmail(from_, to, msg.as_string())
    mailer.close()

    Read email package documentation for more details.
    Seo Sanghyeon, Apr 29, 2004
    #3
  4. Sean Berry

    Steve Holden Guest

    Sean Berry wrote:

    > I have the sample script that uses the smtplib module
    > that is available at python.org.
    >
    > How can I use this to send an attachment?
    >
    > Help is greatly appreciated.
    >
    > Thanks.
    >

    Take a look at the "email" module, which is the usual way of generating
    messages of complex structure. Then, specifically, look at "Creating
    email and MIME objects from scratch" at (e.g.)
    http://docs.python.org/lib/node501.html - you'll probably have the same
    page in your local Python docs.

    regards
    Steve
    Steve Holden, Apr 29, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zameer
    Replies:
    2
    Views:
    3,337
    zameer
    Dec 4, 2003
  2. Stephen Boulet
    Replies:
    2
    Views:
    512
    Christos TZOTZIOY Georgiou
    Jul 30, 2004
  3. praba kar
    Replies:
    0
    Views:
    295
    praba kar
    May 11, 2005
  4. Evan
    Replies:
    3
    Views:
    470
  5. Tobiah
    Replies:
    4
    Views:
    192
    Tobiah
    Nov 15, 2012
Loading...

Share This Page