Taken from the tip of the week for March 23, 2007 shows how to create an email contact form using Lasso.
The tip demonstrates a basic technique which is used on many sites. The tip includes instructions for a simple contact form and several variations including sending email to multiple recipients, sending HTML formatted email, and sending attachments with the email.
It may also be useful to have the site visitor email a file along with their message. This would provide a way for site visitors to submit images, files for printing, etc.
A working example of this form can be found in the download in the "attachment.lasso" file. You should replace the email address "firstname.lastname@example.org" with your own email address to test the form.
Two changes are required to the contact form. First, the form must be modified with an enctype="multipart/form-data" parameter. This instructs the browser that we will be uploading files with this form. Second, a "file" input is added to the form.
<input type="hidden" name="action" value="submit" /> <h2>Contact Us</h2> [If: (Action_Param: 'action', -Count) != ''] <p>Thank you. We will respond to your message shortly.</p> [Else] <p>Use this form to send us an email message.</p> <p>Your Name:<br /> <input type="text" name="name" value="" /></p> <p>Your Email Address:<br /> <input type="text" name="email" value="" /></p> <p>Comments:<br /> <textarea name="comments" rows="10" cols="40"></textarea></p> <p>Attachment:<br /> <input type="file" name="file" /></p> <p><input type="submit" name="action" value="Submit" /> [/If]
If the site visitor selects a file for upload then it will be accessible through the [File_Uploads] array on the server side. In the LassoScript at the top of the file we add the following lines.
Var: 'attachments' = Array; Iterate: File_Uploads, (Var: 'upload'); Var: 'origname' = $upload->(Find: 'origname'); Var: 'size' = $upload->(Find: 'file')->Size; $attachments->(Insert: $origname = $upload->(Find: 'file')->(Read: $size)); /Iterate;
First, we create a variable $attachments which holds an array. Then, we iterate through the [File_Uploads] array. We pull out the original file name "origname" of the uploaded file. We also get the size of the "file" object for the uploaded file. Then, we insert into the $attachments array the name of the file and the contents of the file.
Finally, we pass the $attachments array into the [Email_Send] tag as the -Attachments parameter.
Email_Send: -Toemail@example.com', -Fromfirstname.lastname@example.org', -Subject='Email Form - Simple', -Body='The following form was submitted on your Web site:\r\r' + 'Name:\r' + (Action_Param: 'name') + '\r\r' + 'Email:\r' + (Action_Param: 'email') + '\r\r' + 'Comments:\r' + (Action_Param: 'comments') + '\r\r' + 'Date/Time:\r' + Date + '\r\r' + 'Client:\r' + Client_IP + ' ' + Client_Type + '\r\r', -Attachments=$attachments;
Note that if we included more than one "file" input in the contact form then the site visitor would be able to upload more than one file. The code above can accept as many files as are uploaded and will attach each of them to the email message.
More information about all of the tags used in this example can be found in the Lasso Language Guide or in the online Lasso Reference.
Author: Fletcher Sandbeck
Created: 23 Mar 2007
Last Modified: 16 Mar 2011
Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.