Form Processing With Zope - You've Got Mail! (
Page 6 of 7 )
Finally, how about a complete example
to wrap all this up? Here's the form:
<dtml-var standard_html_header>
<table border=0 cellspacing=5 cellpadding=5>
<form
action="FeedbackFormProcessor" method="POST">
<tr>
<td valign="top">Name:</td>
<td><input
name="name:string"></td>
</tr>
<tr>
<td valign="top">Email address:</td>
<td><input
name="email:required"></td>
</tr>
<tr>
<td valign="top">Comments:</td>
<td><textarea
name="comment:text" cols="30" rows="6"></textarea></td>
</tr>
<tr>
<td
colspan=2 align=center><input type="Submit" name="submit"
value="Send!"></td>
</tr> </table> </form>
<dtml-var standard_html_footer>
And here's what it looks like:
As you can see, this is a simple feedback form for a Web site. When a user submits
it, the information entered into it should be automagically emailed to the site's
Webmaster.
How is this done in Zope? Very simple. First, create a new Mail Host object (I've
called mine "LocalRelay"), and assign it appropriate values for your SMTP server.
Then, create a FeedbackFormProcessor, which uses this Mail Host object to send
the contents of the form to a specified email address.
Here's the code:
<dtml-var standard_html_header>
<dtml-sendmail mailhost="LocalRelay">
To:
webmaster@domain.com
From: <dtml-var email>
Subject: Feedback on <dtml-var
SERVER_URL>
Name: <dtml-var name>
Email address: <dtml-var email>
Comment:
<dtml-var comment>
--
URL: <dtml-var URL>
Remote address: <dtml-var
REMOTE_ADDR>
User
agent: <dtml-var HTTP_USER_AGENT>
</dtml-sendmail>
Your
feedback was sent
successfully. Thank you for your comments!
<dtml-var standard_html_footer>
In this case, the <dtml-sendmail> directive is used to tell Zope that the
contents of the block are to be sent out as email via the specified Mail Host.
You must ensure that the email message includes "To", "From" and "Subject" headers,
or else either Zope or your mail server will barf. In addition to these three
mandatory headers, you can also specify additional, optional headers, such as
"Cc", "Reply-To" and so on.
Notice how I've included a few built-in variables in the email message. This
is useful to provide the site's Webmaster with some additional information on
the client used to send the feedback.
In case you have difficulty configuring a Mail Host, you can also use this alternative
construction:
<dtml-sendmail smtphost="my.mail.server" port="25" >
...
</dtml-sendmail>
Once the email message has been sent, a message is displayed to the user confirming
message delivery.
If you're going to be using a lot of feedback forms, you might want to consider
making the recipient email address a property, so that you can refer to it as
a variable rather than hard-wiring it into the DTML Document code itself.