Name: [16797] Tim Bower
Member: 62 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/>`_ ...

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

in series: Python Network Programming

video tutorial by Tim Bower, added 07/09

(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

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)

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 306 (since July 30th)
  • Plays in last week: 2
  • Published: 61 months ago

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.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

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

Rashid


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Tim

By the Same Author

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com