will indeed send an email message as specified by the input parameters at the end. That
URL string is a lot to type into a browser’s address field, of course, but it might be useful
if generated automatically by another script. As we saw in Chapters 13 and 15, the
module urllib.request can then be used to submit such a URL string to the server from
within a Python program. Example 16-5 shows one way to automate this.
Example 16-5. PP4E\Internet\Web\PyMailCgi\sendurl.py
"""
####################################################################
Send email by building a URL like this from inputs:
http://servername/pathname/
onEditPageSend.py?site=smtp.rmi.net&
[email protected]&
[email protected]&
Subject=test+url&
text=Hello+Mark;this+is+Mark
####################################################################
"""
from urllib.request import urlopen
from urllib.parse import quote_plus
url = 'http://localhost:8000/cgi-bin/onEditPageSend.py'
url += '?site=%s' % quote_plus(input('Site>'))
url += '&From=%s' % quote_plus(input('From>'))
url += '&To=%s' % quote_plus(input('To >'))
url += '&Subject=%s' % quote_plus(input('Subj>'))
url += '&text=%s' % quote_plus(input('text>')) # or input loop
print('Reply html:')
print(urlopen(url).read().decode()) # confirmation or error page HTML
Running this script from the system command line is yet another way to send an email
message—this time, by contacting our CGI script on a web server machine to do all
the work. The script sendurl.py runs on any machine with Python and sockets, lets us
input mail parameters interactively, and invokes another Python script that lives on a
possibly remote machine. It prints HTML returned by our CGI script:
C:\...\PP4E\Internet\Web\PyMailCgi> sendurl.py
Site>smtpout.secureserver.net
From>[email protected]
To >[email protected]
Subj>testing sendurl.py
text>But sir, it's only wafer-thin...
Reply html:
<html><head><title>PyMailCGI: Confirmation page (PP4E)</title></head>
<body bgcolor="#FFFFFF"><h1>PyMailCGI Confirmation</h1><hr>
<h2>Send mail operation was successful</h2>
<p>Press the link below to return to the main page.</p>
</p><hr><a href="http://www.python.org">
<img src="../PythonPoweredSmall.gif"
align=left alt="[Python Logo]" border=0 hspace=15></a>
1248 | Chapter 16: The PyMailCGI Server