{"id":3612,"date":"2019-02-13T17:47:32","date_gmt":"2019-02-13T12:17:32","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=3612"},"modified":"2026-10-07T14:55:14","modified_gmt":"2026-10-07T09:25:14","slug":"create-contact-form-with-php","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/create-contact-form-with-php\/","title":{"rendered":"How to Create a Contact Form with PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Post Date:<\/strong> February 13, 2019<br><strong>Last Updated:<\/strong> October 07, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A contact form allows website visitors to send enquiries or messages directly to the website owner.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, we will explain how to create a simple contact form using HTML and PHP. The form will collect the visitor&#8217;s name, email address, and telephone number and send the submitted information to a specified email address.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before creating the contact form, make sure you have the following:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PHP-Enabled Web Server<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Your website must be hosted on a server with PHP support.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mail Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The server must have a properly configured mail-sending service. Depending on your hosting environment, PHP&#8217;s <code>mail()<\/code> function may require additional mail server configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic PHP and HTML Knowledge<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Basic knowledge of HTML forms and PHP will help you understand and customize the example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Contact Form<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create an HTML file, such as <code>contact.html<\/code>, and add the following form:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;form name=\"contactform\" method=\"post\" action=\"send_form_email.php\">\n    &lt;table width=\"1000px\">\n        &lt;tr>\n            &lt;td valign=\"top\">\n                &lt;label for=\"first_name\">First Name *&lt;\/label>\n            &lt;\/td>\n            &lt;td valign=\"top\">\n                &lt;input type=\"text\" name=\"first_name\" maxlength=\"50\" size=\"30\" required>\n            &lt;\/td>\n        &lt;\/tr>\n\n        &lt;tr>\n            &lt;td valign=\"top\">\n                &lt;label for=\"email\">Email Address *&lt;\/label>\n            &lt;\/td>\n            &lt;td valign=\"top\">\n                &lt;input type=\"email\" name=\"email\" maxlength=\"80\" size=\"30\" required>\n            &lt;\/td>\n        &lt;\/tr>\n\n        &lt;tr>\n            &lt;td valign=\"top\">\n                &lt;label for=\"telephone\">Telephone Number&lt;\/label>\n            &lt;\/td>\n            &lt;td valign=\"top\">\n                &lt;input type=\"tel\" name=\"telephone\" maxlength=\"30\" size=\"30\">\n            &lt;\/td>\n        &lt;\/tr>\n\n        &lt;tr>\n            &lt;td colspan=\"2\" style=\"text-align:center\">\n                &lt;input type=\"submit\" value=\"Submit\">\n            &lt;\/td>\n        &lt;\/tr>\n    &lt;\/table>\n&lt;\/form>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The form submits the entered information to <code>send_form_email.php<\/code> using the <code>POST<\/code> method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the PHP Email Handler<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file named:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">send_form_email.php\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following PHP code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n$email_to = \"example@email.com\";\n$email_subject = \"Enquiry Form\";\n\n$first_name = trim($_POST['first_name'] ?? '');\n$email_from = trim($_POST['email'] ?? '');\n$telephone = trim($_POST['telephone'] ?? '');\n\nif ($first_name === '' || $email_from === '') {\n    exit('Please complete all required fields.');\n}\n\nif (!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {\n    exit('Please enter a valid email address.');\n}\n\n$first_name = htmlspecialchars($first_name, ENT_QUOTES, 'UTF-8');\n$telephone = htmlspecialchars($telephone, ENT_QUOTES, 'UTF-8');\n\n$email_message  = \"First Name: \" . $first_name . \"\\n\";\n$email_message .= \"Email: \" . $email_from . \"\\n\";\n$email_message .= \"Telephone: \" . $telephone . \"\\n\";\n\n$headers  = \"From: \" . $email_from . \"\\r\\n\";\n$headers .= \"Reply-To: \" . $email_from . \"\\r\\n\";\n$headers .= \"X-Mailer: PHP\/\" . phpversion();\n\nif (mail($email_to, $email_subject, $email_message, $headers)) {\n    echo \"Thank you for contacting us.\";\n} else {\n    echo \"Unable to send your message. Please try again later.\";\n}\n\n?>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">example@email.com\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">with the email address where you want to receive the enquiries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Configure the Email Address<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Update the following line:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$email_to = \"example@email.com\";\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$email_to = \"sales@domain.com\";\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The submitted form details will then be sent to the configured address.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Test the Contact Form<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open the contact form in your browser and enter sample information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First Name: John<\/li>\n\n\n\n<li>Email Address: <a href=\"mailto:john@example.com\">john@example.com<\/a><\/li>\n\n\n\n<li>Telephone Number: +1 1234567890<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Click <strong>Submit<\/strong> and verify that the message is received at the configured email address.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Verify Server Mail Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the form submits successfully but the email is not received, the issue may be related to the server&#8217;s mail configuration rather than the PHP form itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check your server&#8217;s mail configuration and mail logs, and verify that outbound email is permitted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production websites, using an authenticated SMTP service is generally more reliable than relying directly on PHP&#8217;s <code>mail()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Security Considerations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A production contact form should include additional protections against abuse.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Validate User Input<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Always validate submitted data on the server side. Do not rely only on HTML validation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Protect Against Spam<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Consider adding CAPTCHA or another anti-spam mechanism to prevent automated submissions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid Email Header Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Never directly trust user-supplied values in email headers. Validate email addresses before using them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use SMTP for Production<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For reliable transactional email delivery, consider using an authenticated SMTP service rather than depending entirely on the server&#8217;s PHP mail configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Creating a basic PHP contact form requires an HTML form to collect visitor information and a PHP script to process the submission and send the email.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The example above provides a simple starting point that can be customized for business enquiries, support requests, lead-generation forms, and other website communication requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production environments, additional security, spam protection, validation, and reliable SMTP configuration should be implemented.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use this PHP contact form on any website?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use it on websites hosted on servers that support PHP and have a properly configured email-sending service.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why is the form not sending emails?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The PHP code may be working correctly while the server&#8217;s mail service is not configured properly. Check your mail server configuration and email logs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I change the recipient email address?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Change the value of <code>$email_to<\/code> in the PHP script:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$email_to = \"your-email@example.com\";\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Can I add more fields to the form?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. You can add fields such as company name, subject, message, location, or other information and process them in the PHP script.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use PHP <code>mail()<\/code> for a production website?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">While <code>mail()<\/code> can be used in simple environments, an authenticated SMTP service is generally recommended for production websites because it provides better control over email delivery and authentication.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How can I protect the contact form from spam?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can add CAPTCHA, rate limiting, server-side validation, honeypot fields, and other anti-spam controls depending on the requirements of your website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-and-use-php-composer-on-centos-7\/\">How to Install and Use PHP Composer on CentOS 7<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-phppgadmin-in-the-remote-server-linux\/\">How to install phpPgAdmin in the remote server \u2013 Linux<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Need Help with PHP Contact Forms?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Need help creating or configuring a PHP contact form for your website? Our team can assist with PHP development, form validation, SMTP configuration, email delivery, spam protection, security, troubleshooting, and ongoing website maintenance. Get in touch with our team <a href=\"https:\/\/pheonixsolutions.com\/contact?utm_source=chatgpt.com\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: February 13, 2019Last Updated: October 07, 2026 Introduction A contact form allows website visitors to send enquiries or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[556,273],"class_list":["post-3612","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-contact-form","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-Wg","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3612"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3612\/revisions"}],"predecessor-version":[{"id":12161,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3612\/revisions\/12161"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}