[Tutor] newbie: trouble importing email module

Magnus Lycka magnus@thinkware.se
Thu Mar 13 13:31:25 2003


Craig Davey wrote:
>I'm very new to python, but I'm trying to create a simple cgi script
>that will send an email message.

Hi Craig, and welcome!

Just a little warning. Don't make available a service that allows
anonymous people to send arbitrary emails. It's likely to be
abused by spammers, and then it will certainly be shut down, and
your ISP will probably not be very happy... Otherwise, this is
a simple thing to do...

>So I started out trying to construct a
>message using the email.Message module, unfortunately it seems like my
>isp is missing this module.

Look at the library reference:

http://www.python.org/doc/current/lib/module-email.html

"The email package is a library for managing email messages, including
MIME and other RFC 2822-based message documents. It subsumes most of the
functionality in several older standard modules such as rfc822, mimetools,
multifile, and other non-standard packages such as mimecntl. It is
specifically not designed to do any sending of email messages to SMTP
(RFC 2821) servers; that is the function of the smtplib module."

You might want to use this if you are assembling a multi part
MIME message etc, but not for simple text emails. It not needed
for that, and it's not at all involved in *sending* emails.

So, I don't think you will miss the email module. You can install
it separately for Python 2.1.3 if you like, but not for 2.1.2. :(

>The version I am using is 2.1.2 on an OpenBSD system. I'm wondering if
>I'm doing something completely wrong or not because I thought the email
>module was part of the standard distribution.

It is now...

The documentation for 2.1.2 is here:

http://www.python.org/doc/2.1.2/

As you see here, http://www.python.org/doc/2.1.2/lib/modindex.html ,
the email module wasn't in 2.1.2. It came with 2.2.

But the smtplib has been with us for ages! See
http://www.python.org/doc/2.1.2/lib/module-smtplib.html and
http://www.python.org/doc/2.1.2/lib/SMTP-example.html

That example is slightly dated though. Now, you can change

import string
...
string.join(toaddrs, ", ")

to just

", ".join(toaddrs)

Otherwise I think this example is just what the doctor ordered... ;)

To get data into your email, I guess you will want to examine
the contents of some kind of HTML form. For that, you use

http://www.python.org/doc/2.1.2/lib/module-cgi.html

See http://www.python.org/doc/2.1.2/lib/Using_the_cgi_module.html
for usage examples.

 From 2.2, there is also a nice cgitb module that helps with
debugging cgi scripts, but that's not available in 2.1.

Python 2.2 has been available for quite some time, and it
will be supported for long, so it's probably a good idea to
upgrade... You can always ask your ISP... CGI debugging it
often a bit tricky. You need to set up environment variables
correponding to what the web server sees, to run the scripts
from command line, and when you run it as CGI, it will be as
another user, and it might not work--and typically all you
see is "internal server error". cgitb is helpful then, and
that requires 2.2...


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se