Name: [16797] Tim Bower
Member: 8 months
Authored: 8 videos
Description: Assistant Professor Computer Systems Technology Kansas State University at Salina `Tim Bower <http://www.sal.ksu.edu/faculty/tim/>`_ My blog: `Open Source on the Prairie <http://prairiepy.blogspot.com/>`_ ...

[1551] Testing a Program that Sends E-mail with the Fakemail Server (4/4)

in series: Python Network Programming

video tutorial by Tim Bower

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

This video demonstrated how you can use the Fakemail e-mail server for testing purposes while developing a program that sends e-mail messages.

Fakemail was written by Graham Ashton and the project's home page is hosted on Source Forge.

In addition, the video also shows the use of Python's email.MIMEText module to build a simple message and shows sending the message with sockets according to the SMTP protocol. You will want to use the smtplib module to send e-mail messages, instead of doing it your self with sockets. I use sockets in the video just to demonstrate the SMTP protocol conversation between the client and server.

For more information about Python network programming, see my Network Programming Study Guide

Video Tutorials related by tag:

python networkprogramming fakemail e-mail SMTP

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 20 (since July 30th)
  • Plays in last week: 3
  • Published: 6 months ago
from email.MIMEText import MIMEText
from email import Utils
import socket


def send_recv(sock, msg):
    print msg
    sock.send(msg+'\r\n')
    try:
        r = sock.recv(1024)
        print r
    except socket.timeout, e:
        print "Socket Timeout - %s\n" % str(e)

def smtpSocket(server, domain, msg):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.settimeout(5)
    try:
        s.connect(server)
        r = s.recv(1024)
        print r
    except socket.timeout, e:
        print "Socket Timeout - %s\n" % str(e)
        return
    send_recv(s,'HELO ' + domain)
    mFrom = "MAIL From:<%s>" % msg['From']
    send_recv(s, mFrom)
    mRcpt = "RCPT To:<%s>" % msg['To']
    send_recv(s, mRcpt)
    send_recv(s, 'DATA')
    send_recv(s, msg.as_string()+'\r\n.')
    send_recv(s, 'QUIT')
    s.close()

message = """Hello,
This is a test message.

    -- Tim"""

msg = MIMEText(message)

msg['To'] = 'you@home'
msg['From'] = 'me@school'
msg['Subject'] = 'Testing Python e-mail'

msg['Date'] = Utils.formatdate(localtime = 1)
msg['Message-ID'] = Utils.make_msgid()

server = ('localhost', 8025)
domain = 'localhost'
##server = ('postman.sal.ksu.edu', 25)
#domain = 'sal.ksu.edu'
smtpSocket(server, domain, msg)

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Please provide your email address (won't be published)

Thanks for the valuable stuff. Its very helpful to increase the knowledge.

Rashid


Kudos and Thanks for Tim

Learn Python the easy way!
If you want to learn the Python programming language from a very gentle start or are looking to increase your ability to do cool things with Python, check out our club
View this series as a one-page learning-path
This feature is for club members. Click here to find out more.
Your email address will not be published.

Show some quick comments >>








By the Same Author

Tim Bower Videos related to the production of educational screencast video.
Tim Bower Videos about how to use Unix / Linux. Emphasis will be on entering commands at the shell [...]