You are not logged in Log in Join
You are here: Home » Zope Documentation » How-To » How to create a mail form

Log in
Name

Password

 

How to create a mail form

Zope makes it pretty easy to accomplish simple CGI style tasks with a little DTML. One common task everyone wants to do is to create a form that sends email. Here's a simple example showing how to create a mail form with Zope. All it takes is a Mail Host object, and two DTML Document Objects.

Start by creating a Mail Host object. This object will allow Zope to send email. If you have already defined a Mail Host object, skip to the next step. You probably want to create this object at the top level of Zope so that all objects will have access to mail facilities.

To create a Mail Host, choose "Mail Host" from the pop-up list of objects to add, and click "Add". You will next need to define your Mail Host. The import thing to specify is the SMTP Host. This should be the name of a computer which you can use to send mail. It may be the same as the localhost computer.

Create a Mail Host

Next create two documents, one to collect the data, and one to send the mail.

You can create these Documents at any level in Zope that can acquire the Mail Host.

Let's call the form Document Feedback, and the mail sending Document SendFeedback. You should give the Feedback Document DTML similar to this:

    <!--#var standard_html_header-->
    <H2>We want your input!</H2> 

    <form action="SendFeedback" method="post"> 
    Your Name: <input type="text" name="name" size="40"> 
    <br> 
    <textarea name="comments" rows="10" cols="50">
    Type your comments here.
    </textarea><br> 
    <input type="submit" value="Send Feedback"> 
    </form> 
    <!--#var standard_html_footer-->

As you can see it defines a form whose action is SendFeedback. So this Document calls the other Document.

The form defines two variables, name, and comments.

Create a Form

The SendFeedback Document should send the mail, and give the user some assurance that the mail has been sent. Here's one way to do that:

    <!--#var standard_html_header-->

    <!--#sendmail mailhost="MailHost"--> 
    To: Feedback Recipient <[email protected]> 
    From: Zope Feedback Form <[email protected]> 
    Subject: Feedback from the web 

    Feedback from : <!--#var name-->
    Comments:
    <!--#var comments--> 
    <!--#/sendmail--> 

    <h1>Thank you for your input!</h1>
    <p>Your comments have been sent.</p>

    <!--#var standard_html_footer-->

This Document consists of two parts, the first part inside the sendmail tag is the text of an email message. The sendmail tag forwards an email message to a Mail Host for sending. It does not display anything in the browser window. As you can see, you must define the email recipient, sender and subject. For the recipient you should use your email address, or the address of the person who will read the feedback. For the sender you should again use your own email address, or that of a system administrator.

The second part of the Document displays a confirmation message to the user which lets them know that their comments have been sent.

Create a Form Processor

As it is you have everything you need to build a working mail form. The only missing ingredient at this point is deciding on a permissions policy. By default, only users with the manage role can send mail. This is to keep anonymous users from abusing your mail sending facilities. In our case, however, we want to allow anonymous users to be able to send comments via mail.

We could change the permission settings on our Mail Host, but this would defeat the conservative default policy and might not be appropriate for the whole site. Instead we can solve this problem with "Proxy roles". Proxy roles define additional roles for a Document. Normally a Document can only access resources if the current user has adequate permissions. Proxy roles allow a Document to behave as though the current users had additional roles.

So if you give our SendFeedback Document the Manager Proxy-role, then it will be able to send mail. The Document will behave as though the user accessing the Document has the Manager role, even if the user is not a manager.

To set the Proxy-roles, click the "Proxy" tab on the management screen while editing the SendFeedback Document. Select manage and click "Change".

Setting Permissions

That's it, you've just built a feedback form.

Now test the mail form to make sure it works. The most likely problem that you will encounter is that you may not have selected the right computer for your SMTP host. You may have to contact your system administrator to find out the correct configuration.

Testing the Mail Form