{"id":48,"date":"2020-02-12T10:36:19","date_gmt":"2020-02-12T10:36:19","guid":{"rendered":"https:\/\/www.lcn.com\/support\/knowledge-base\/how-to-create-an-email-form-with-php\/"},"modified":"2021-06-02T09:00:17","modified_gmt":"2021-06-02T09:00:17","slug":"how-to-create-an-email-form-with-php","status":"publish","type":"ht_kb","link":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/","title":{"rendered":"How to create an email form with PHP"},"content":{"rendered":"<p>PHP was designed for making interactive web pages and mixing functionality with HTML. Form handling in PHP is quite a simple process. Here is a step-by-step guide 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=\"\/feedback_form.php\" 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>This form will send two parameters to our PHP script, <i>email_address<\/i> and <i>feedback<\/i>. Save this file as <i>feedback_form.html<\/i> and upload it to the web folder on your hosting.<\/p>\n<h3>Create the form script<\/h3>\n<p>First we receive the data from our form and store it in two PHP variables, <i>$email_address<\/i> and <i>$feedback<\/i>.<\/p>\n<pre class=\"prettyprint linenums\">&lt;?php\n$email_address = $_POST['email_address'];\n$feedback = $_POST['feedback'];<\/pre>\n<h3>Filtering user submitted data<\/h3>\n<p>Whenever you write a PHP 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 PHP 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 a functions:<\/p>\n<pre class=\"prettyprint linenums\">function filter_email_header($form_field) {  \nreturn preg_replace('\/[nr|!\/&lt;&gt;^$%*&amp;]+\/','',$form_field);\n}<\/pre>\n<p>The filter function removes special characters which could be used to trick our script into sending spam and is applied to the $email_address data. We&#8217;ll place the two functions at the bottom of our script.<\/p>\n<p>Now we&#8217;ll call the filter function to clean up our user submitted email address:<\/p>\n<pre class=\"prettyprint linenums\">$email_address  = filter_email_header($email_address);<\/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 that a PHP script can use to send email. This can be done using the PHP in-built mail function:<\/p>\n<pre class=\"prettyprint linenums\">$headers = \"From: $email_addressn\";\n$sent = mail('you@domain.com', 'Feedback Form Submission', $feedback, $headers);<\/pre>\n<p>Make sure you set your email address on line 2.<\/p>\n<h3>Thank the user for their feedback<\/h3>\n<p>Finally, when a user submits your form lets show a page thanking them for their feedback:<\/p>\n<pre class=\"prettyprint linenums\">if ($sent) {\n\n?&gt;&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;\n\n&lt;?php\n\n} else {\n?&gt;&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Something went wrong&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Something went wrong&lt;\/h1&gt;\n&lt;p&gt;We could not send your feedback. Please try again.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n&lt;?php\n}\n?&gt;<\/pre>\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\">&lt;?php\n\n#Receive user input\n$email_address = $_POST['email_address'];\n$feedback = $_POST['feedback'];\n\n#Filter user input\nfunction filter_email_header($form_field) {  \nreturn preg_replace('\/[nr|!\/&lt;&gt;^$%*&amp;]+\/','',$form_field);\n}\n\n$email_address  = filter_email_header($email_address);\n\n#Send email\n$headers = \"From: $email_addressn\";\n$sent = mail('you@domain.com', 'Feedback Form Submission', $feedback, $headers);\n\n#Thank user or notify them of a problem\nif ($sent) {\n\n?&gt;&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;\n&lt;?php\n\n} else {\n\n?&gt;&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Something went wrong&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Something went wrong&lt;\/h1&gt;\n&lt;p&gt;We could not send your feedback. Please try again.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n&lt;?php\n}\n?&gt;<\/pre>\n<p>Save this script as <i>feedback_form.php<\/i> and upload it to the root of your web hosting on your web hosting.<\/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. If not, try checking out our <a href=\"\/support\/articles\/troubleshooting-common-php-issues\" target=\"blank\" rel=\"noopener noreferrer\">Troubleshooting common PHP issues<\/a> guide.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP was designed for making interactive web pages and mixing functionality with HTML. Form handling in PHP is quite a simple process. Here is a step-by-step guide 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 [&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-48","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 PHP - LCN.com<\/title>\n<meta name=\"description\" content=\"How to create an email form with PHP.Follow this guide to create a simple PHP mail form using this simple, easy to follow guide.\" \/>\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-php\/\" \/>\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 PHP - LCN.com\" \/>\n<meta property=\"og:description\" content=\"How to create an email form with PHP.Follow this guide to create a simple PHP mail form using this simple, easy to follow guide.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/\" \/>\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=\"2021-06-02T09:00:17+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=\"3 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-php\/\",\"url\":\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/\",\"name\":\"How to create an email form with PHP - LCN.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.lcn.com\/support\/#website\"},\"datePublished\":\"2020-02-12T10:36:19+00:00\",\"dateModified\":\"2021-06-02T09:00:17+00:00\",\"description\":\"How to create an email form with PHP.Follow this guide to create a simple PHP mail form using this simple, easy to follow guide.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/#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 PHP\"}]},{\"@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 PHP - LCN.com","description":"How to create an email form with PHP.Follow this guide to create a simple PHP mail form using this simple, easy to follow guide.","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-php\/","og_locale":"en_US","og_type":"article","og_title":"How to create an email form with PHP - LCN.com","og_description":"How to create an email form with PHP.Follow this guide to create a simple PHP mail form using this simple, easy to follow guide.","og_url":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/","og_site_name":"Customer Support Guides - LCN","article_publisher":"https:\/\/www.facebook.com\/lcndotcom","article_modified_time":"2021-06-02T09:00:17+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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/","url":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/","name":"How to create an email form with PHP - LCN.com","isPartOf":{"@id":"https:\/\/www.lcn.com\/support\/#website"},"datePublished":"2020-02-12T10:36:19+00:00","dateModified":"2021-06-02T09:00:17+00:00","description":"How to create an email form with PHP.Follow this guide to create a simple PHP mail form using this simple, easy to follow guide.","breadcrumb":{"@id":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.lcn.com\/support\/articles\/how-to-create-an-email-form-with-php\/#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 PHP"}]},{"@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\/48","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=48"}],"version-history":[{"count":2,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb\/48\/revisions"}],"predecessor-version":[{"id":1548,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb\/48\/revisions\/1548"}],"wp:attachment":[{"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb-category?post=48"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.lcn.com\/support\/wp-json\/wp\/v2\/ht-kb-tag?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}