{"id":23,"date":"2020-02-12T10:36:13","date_gmt":"2020-02-12T10:36:13","guid":{"rendered":"https:\/\/www.lcn.com\/support\/knowledge-base\/how-to-create-an-email-form-with-perl\/"},"modified":"2020-03-04T15:12:16","modified_gmt":"2020-03-04T15:12:16","slug":"how-to-create-an-email-form-with-perl","status":"publish","type":"ht_kb","link":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/","title":{"rendered":"How to create an email form with Perl"},"content":{"rendered":"<p>Form handling in Perl can be a very involved process. Below is a step-by-step for creating a simple feedback form. A visitor to your website fills this out and the information is emailed to you.<\/p>\n<h3>Create the web form<\/h3>\n<p>First we need to create a simple HTML form, to start with we&#8217;ll keep the form simple by just asking for the users email address and comments. Here is our HTML form:<\/p>\n<pre class=\"prettyprint linenums lang-css\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Simple Feedback Form&lt;\/title&gt;\n&lt;style&gt;label{display:block;}&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;form action=\"\/cgi-bin\/feedback_form.cgi\" method=\"post\"&gt;\n\n&lt;label&gt;Email Address&lt;\/label&gt;\n&lt;input type=\"text\" name=\"email_address\" size=\"40\"&gt;\n\n&lt;label&gt;Your Feedback&lt;\/label&gt;\n&lt;textarea name=\"feedback\" cols=\"50\" rows=\"10\"&gt;&lt;\/textarea&gt;\n\n&lt;input type=\"submit\" name=\"send\" value=\"Submit\"&gt;\n\n&lt;\/form&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p><\/br>This form will send two parameters to our cgi script, <i>email_address<\/i> and <i>feedback<\/i>. Save this file as <i>feedback_form.html<\/i> and upload it to the <strong>web<\/strong> folder on your hosting.<\/p>\n<h3>Create the form script<\/h3>\n<p>We&#8217;re going to use the <strong>CGI.pm Perl module<\/strong> to help make writing our cgi script easier. At the top of the script we start with the location of the perl interpretor, then we tell Perl we want to use the CGI.pm module and create a new cgi object:<\/p>\n<pre class=\"prettyprint linenums\">#!\/usr\/bin\/perl\n\nuse CGI;\n\nmy $cgi = new CGI;<\/pre>\n<p><\/br>The CGI.pm module is object-orientated, this means all of the CGI.pm functions and data are accessed through an instance of CGI.pm, in our script this instance is called <i>$cgi<\/i>.<\/p>\n<p>Lets use our CGI object to retrieve the information from the form the user filled in. To access the form parameters we can use the CGI objects <i>param<\/i> function:<\/p>\n<pre class=\"prettyprint linenums\">my $email_address = $cgi-&gt;param('email_address');\nmy $feedback = $cgi-&gt;param('feedback');<\/pre>\n<p><\/br>We store the form data in two local Perl variables, <i>$email_address<\/i> and <i>$feedback<\/i>.<\/p>\n<h3>Filtering user submitted data<\/h3>\n<p>Whenever you write a cgi script that receives data from an unknown source you should always filter the data to make sure it doesn&#8217;t contain anything harmful. For example, if we don&#8217;t filter the data in our form it would be quite easy for a Hacker to use our cgi script to send out spam to thousands of people. The golden rule is never trust any data you haven&#8217;t created or don&#8217;t control.<\/p>\n<p>To filter our user data we&#8217;re going to create two filter functions:<\/p>\n<pre class=\"prettyprint linenums\">sub filter_email_header\n{\nmy $form_field = shift;\n$form_field = filter_form_data($form_field);\n$form_field  =~ s\/[nr|!\/&lt;&gt;^$%*&amp;]+\/ \/g;\n\nreturn $form_field;\n}\n\nsub filter_form_data\n{\nmy $form_field = shift;\n$form_field  =~ s\/From:\/\/gi;  \n$form_field  =~ s\/To:\/\/gi;  \n$form_field  =~ s\/BCC:\/\/gi;  \n$form_field  =~ s\/CC:\/\/gi;  \n$form_field  =~ s\/Subject:\/\/gi;  \n$form_field  =~ s\/Content-Type:\/\/gi;    \n\nreturn $form_field;\n}<\/pre>\n<p><\/br>The first filter function removes special characters which could be used to trick our script into sending spam and is applied to the <i>$email_address<\/i> data.The second filter function removes common email headers from the data the user submitted and can be applied to both <i>$email_address<\/i> and <i>$feedback<\/i>. We&#8217;ll place the two functions at the bottom of our script.<\/p>\n<p>Now we&#8217;ll call the two filter functions to clean up our user submitted data:<\/p>\n<pre class=\"prettyprint linenums\">$email_address  = filter_email_header($email_address);\n$feedback = filter_form_data($feedback);<\/pre>\n<h3>Emailing the feedback<\/h3>\n<p>Once we have the filtered data we need to email it back to you. Our web hosting servers run a local mail server (sendmail) that your cgi script can use to send email. To send the email our cgi script opens a communication channel to the sendmail program using the pipe (|) symbol. It then prints all the information necessary to send an email across that channel:<\/p>\n<pre class=\"prettyprint linenums\">open ( MAIL, \"| \/usr\/lib\/sendmail -t\" );\nprint MAIL \"From: $email_addressn\";\nprint MAIL \"To: you@domain.comn\";\nprint MAIL \"Subject: Feedback Form Submissionnn\";\nprint MAIL \"$feedbackn\";\nprint MAIL \"n.n\";close ( MAIL );<\/pre>\n<p><\/br>Make sure you set your email address on line 3, you&#8217;ll need to escape the @ symbol by putting a backslash () before it because Perl uses the @ symbol to denote a special type of variable. The two newline characters (nn) at the end of line 4 are used to mark the end of the email headers ready for the content. The n.n on line 6 prints a dot (.) on its own line to tell sendmail that we&#8217;ve finished printing the message.<\/p>\n<h3>Thank the user for their feedback<\/h3>\n<p>Finally, when a user submits your form, let&#8217;s show a page thanking them for their feedback:<\/p>\n<pre class=\"prettyprint linenums\">print $cgi-&gt;header(-type =&gt; 'text\/html');\n\nprint &lt;&lt;HTML_PAGE;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Thank You&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Thank You&lt;\/h1&gt;\n&lt;p&gt;Thank you for your feedback.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\nHTML_PAGE<\/pre>\n<p><\/br>The first thing we do is print back the HTTP header, using the CGI header function, to let the web browser know what type of content to expect. Then we print out the HTML page.<\/p>\n<h3>The final script<\/h3>\n<p>This example script shows a very basic way to get form contents emailed to you, it doesn&#8217;t however have the refinements of a professional script, e.g. input validation. Below is the finished script. We&#8217;ve added some comments (lines beginning with #) to help make it clearer.<\/p>\n<pre class=\"prettyprint linenums\">#!\/usr\/bin\/perl\n\nuse CGI;\n\n# Create a CGI.pm object\nmy $cgi = new CGI;\n\n# Get the form data\nmy $email_address = $cgi-&gt;param('email_address');\nmy $feedback = $cgi-&gt;param('feedback');\n\n# Filter the form data\n$email_address  = filter_email_header($email_address);\n$feedback = filter_form_data($feedback);\n\n# Email the form data\nopen ( MAIL, \"| \/usr\/lib\/sendmail -t\" );\nprint MAIL \"From: $email_addressn\";\nprint MAIL \"To: you@domain.comn\";\nprint MAIL \"Subject: Feedback Form Submissionnn\";\nprint MAIL \"$feedbackn\";\nprint MAIL \"n.n\";\nclose ( MAIL );\n\n# Print the HTTP header\nprint $cgi-&gt;header(-type =&gt; 'text\/html');\n\n# Print the HTML thank you page\nprint &lt;&lt;HTML_PAGE;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Thank You&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Thank You&lt;\/h1&gt;\n&lt;p&gt;Thank you for your feedback.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\nHTML_PAGE\n\n# Functions to filter the form data\n\nsub filter_email_header\n{\nmy $form_field = shift;  \n$form_field = filter_form_data($form_field);  \n$form_field  =~ s\/[nr|!\/&lt;&gt;^$%*&amp;]+\/ \/g;  \n\nreturn $form_field ;\n}\n\nsub filter_form_data\n{  my $form_field = shift;\n$form_field  =~ s\/From:\/\/gi;  \n$form_field  =~ s\/To:\/\/gi;  \n$form_field  =~ s\/BCC:\/\/gi;  \n$form_field  =~ s\/CC:\/\/gi;  \n$form_field  =~ s\/Subject:\/\/gi;  \n$form_field  =~ s\/Content-Type:\/\/gi;\n\nreturn $form_field ;\n}<\/pre>\n<p><\/br>Save this script as <i>feedback_form.cgi<\/i> and upload it to the cgi-bin on your web hosting. Make sure you set the file permissions for the script to <strong>755<\/strong>. Follow our <a href=\"\/support\/articles\/how-to-change-file-permissions-via-ftp\" target=\"blank\" rel=\"noopener noreferrer\">How to change file permissions via FTP<\/a> guide for instructions on how to do this.<\/p>\n<p>Now you&#8217;re ready to test your feedback form. Load your feedback form in your browser, http:\/\/www.domain.com\/feedback_form.html, fill the form in and submit it. If everything works you should receive an email containing what you just entered in the form.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Form handling in Perl can be a very involved process. Below is a step-by-step for creating a simple feedback form. A visitor to your website fills this out and the information is emailed to you. Create the web form First we need to create a simple HTML form, to start with we&#8217;ll keep the form [&hellip;]<\/p>\n","protected":false},"author":1,"comment_status":"closed","ping_status":"open","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[15],"ht-kb-tag":[5],"class_list":["post-23","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-creating-a-new-website","ht_kb_tag-web-hosting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to create an email form with Perl - LCN.com<\/title>\n<meta name=\"description\" content=\"How to create an email form with Perl at LCN.com.Follow this step-by-step guide to create a simple Perl email form for your website.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create an email form with Perl - LCN.com\" \/>\n<meta property=\"og:description\" content=\"How to create an email form with Perl at LCN.com.Follow this step-by-step guide to create a simple Perl email form for your website.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/\" \/>\n<meta property=\"og:site_name\" content=\"Customer Support Guides - LCN\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/lcndotcom\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-04T15:12:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.lcn.com\/support\/wp-content\/uploads\/support-facebook-feed.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@lcndotcom\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/\",\"url\":\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/\",\"name\":\"How to create an email form with Perl - LCN.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.lcn.com\/support\/#website\"},\"datePublished\":\"2020-02-12T10:36:13+00:00\",\"dateModified\":\"2020-03-04T15:12:16+00:00\",\"description\":\"How to create an email form with Perl at LCN.com.Follow this step-by-step guide to create a simple Perl email form for your website.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.lcn.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to create an email form with Perl\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.lcn.com\/support\/#website\",\"url\":\"https:\/\/www.lcn.com\/support\/\",\"name\":\"Customer Support Guides - LCN\",\"description\":\"- LCN\",\"publisher\":{\"@id\":\"https:\/\/www.lcn.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.lcn.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.lcn.com\/support\/#organization\",\"name\":\"LCN.com\",\"url\":\"https:\/\/www.lcn.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.lcn.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.lcn.com\/support\/wp-content\/uploads\/2020\/02\/ZF_-hcc3.jpg\",\"contentUrl\":\"https:\/\/www.lcn.com\/support\/wp-content\/uploads\/2020\/02\/ZF_-hcc3.jpg\",\"width\":461,\"height\":461,\"caption\":\"LCN.com\"},\"image\":{\"@id\":\"https:\/\/www.lcn.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/lcndotcom\",\"https:\/\/x.com\/lcndotcom\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create an email form with Perl - LCN.com","description":"How to create an email form with Perl at LCN.com.Follow this step-by-step guide to create a simple Perl email form for your website.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/","og_locale":"en_US","og_type":"article","og_title":"How to create an email form with Perl - LCN.com","og_description":"How to create an email form with Perl at LCN.com.Follow this step-by-step guide to create a simple Perl email form for your website.","og_url":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/","og_site_name":"Customer Support Guides - LCN","article_publisher":"https:\/\/www.facebook.com\/lcndotcom","article_modified_time":"2020-03-04T15:12:16+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.lcn.com\/support\/wp-content\/uploads\/support-facebook-feed.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_site":"@lcndotcom","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/","url":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/","name":"How to create an email form with Perl - LCN.com","isPartOf":{"@id":"https:\/\/www.lcn.com\/support\/#website"},"datePublished":"2020-02-12T10:36:13+00:00","dateModified":"2020-03-04T15:12:16+00:00","description":"How to create an email form with Perl at LCN.com.Follow this step-by-step guide to create a simple Perl email form for your website.","breadcrumb":{"@id":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-perl\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.lcn.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to create an email form with Perl"}]},{"@type":"WebSite","@id":"https:\/\/www.lcn.com\/support\/#website","url":"https:\/\/www.lcn.com\/support\/","name":"Customer Support Guides - LCN","description":"- LCN","publisher":{"@id":"https:\/\/www.lcn.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.lcn.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.lcn.com\/support\/#organization","name":"LCN.com","url":"https:\/\/www.lcn.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.lcn.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.lcn.com\/support\/wp-content\/uploads\/2020\/02\/ZF_-hcc3.jpg","contentUrl":"https:\/\/www.lcn.com\/support\/wp-content\/uploads\/2020\/02\/ZF_-hcc3.jpg","width":461,"height":461,"caption":"LCN.com"},"image":{"@id":"https:\/\/www.lcn.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/lcndotcom","https:\/\/x.com\/lcndotcom"]}]}},"_links":{"self":[{"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":2,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb\/23\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb\/23\/revisions\/390"}],"wp:attachment":[{"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb-category?post=23"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb-tag?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}