P. 1
Python Programming

Python Programming

|Views: 146|Likes:
Published by Eric Kivuva

More info:

Published by: Eric Kivuva on Jul 23, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






  • Learning Python
  • Python Programming/Overview
  • Python Programming/Getting Python
  • Python Programming/Setting it up
  • Python Programming/Interactive mode
  • Python Programming/Self Help
  • Python Programming/Creating Python programs
  • Python Programming/Basic Math
  • Python Programming/Decision Control
  • Python Programming/Conditional Statements
  • Python Programming/Loops
  • Python Programming/Sequences
  • Python Programming/Source Documentation and Comments
  • Python Programming/Modules and how to use them
  • Python Programming/Files
  • Python Programming/Text
  • Python Programming/Errors
  • Python Programming/Namespace
  • Python Programming/Object-oriented programming
  • Python Programming/User Interaction
  • Python Programming/Databases
  • Python Programming/Internet
  • Python Programming/Networks
  • Python Programming/Tips and Tricks
  • Concepts
  • Python Programming/Basic syntax
  • Python Programming/Data types
  • Python Programming/Numbers
  • Python Programming/Strings
  • Python Programming/Lists
  • Python Programming/Tuples
  • Python Programming/Dictionaries
  • Python Programming/Sets
  • Python Programming/Operators
  • Python Programming/Flow control
  • Python Programming/Functions
  • Python Programming/Decorators
  • Python Programming/Scoping
  • Python Programming/Exceptions
  • Python Programming/Input and output
  • Python Programming/Modules
  • Python Programming/Classes
  • Python Programming/MetaClasses
  • Modules
  • Python Programming/Standard Library
  • Python Programming/Regular Expression
  • Python Programming/XML Tools
  • Python Programming/Email
  • Python Programming/Threading
  • Python Programming/Sockets
  • Python Programming/GUI Programming
  • Python Programming/WSGI web programming
  • Python Programming/Web Page Harvesting
  • Python Programming/Database Programming
  • Python Programming/Game Programming in Python
  • Python Programming/PyQt4
  • Python Programming/Dbus
  • Python Programming/pyFormex
  • Python Programming/Extending with C
  • Python Programming/Extending with C++
  • Python Programming/Extending with Pyrex
  • Python Programming/Extending with ctypes
  • Article Sources and Contributors
  • Image Sources, Licenses and Contributors
  • License

Previous: XML


IndexNext: Threading

Python includes several modules in the standard library for working with emails and email servers.

Sending mail

Sending mail is done with Python's smtplib using an SMTP (Simple Mail Transfer Protocol) server. Actual usage

varies depending on complexity of the email and settings of the email server, the instructions here are based on

sending email through Google's Gmail.

The first step is to create an SMTP object, each object is used for connection with one server.

import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)

The first argument is the server's hostname, the second is the port. The port used varies depending on the server.

Next, we need to do a few steps to set up the proper connection for sending mail.




These steps may not be necessary depending on the server you connect to. ehlo() is used for ESMTP servers, for

non-ESMTP servers, use helo() instead. See Wikipedia's article about the SMTP protocol for more information about

this. The starttls() function starts Transport Layer Security mode, which is required by Gmail. Other mail systems

may not use this, or it may not be available.

Next, log in to the server:

server.login("youremailusername", "password")

Then, send the mail:

msg = "\nHello!" # The /n separates the message from the headers (which

we ignore for this example)

server.sendmail("you@gmail.com", "target@example.com", msg)

Note that this is a rather crude example, it doesn't include a subject, or any other headers. For that, one should use the

email package.

Python Programming/Email


The email package

Python's email package contains many classes and functions for composing and parsing email messages, this section

only covers a small subset useful for sending emails.

We start by only importing only the classes we need, this also saves us from having to use the full module name


from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

Then we compose some of the basic message headers:

fromaddr = "you@gmail.com"

toaddr = "target@example.com"

msg = MIMEMultipart()

msg['From'] = fromaddr

msg['To'] = toaddr

msg['Subject'] = "Python email"

Next, we attach the body of the email to the MIME message:

body = "Python test mail"

msg.attach(MIMEText(body, 'plain'))

For sending the mail, we have to convert the object to a string, and then use the same prodecure as above to send

using the SMTP server..

import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)




server.login("youremailusername", "password")

text = msg.as_string()

server.sendmail(fromaddr, toaddr, text)

If we look at the text, we can see it has added all the necessary headers and structure necessary for a MIME

formatted email. See MIME for more details on the standard:

The full text of our example message

>>> print text

Content-Type: multipart/mixed; boundary="===============1893313573=="

MIME-Version: 1.0

From: you@gmail.com

To: target@example.com

Subject: Python email


Content-Type: text/plain; charset="us-ascii"

MIME-Version: 1.0

Content-Transfer-Encoding: 7bit

Python test mail


Python Programming/Email


Previous: XML


IndexNext: Threading

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->