PHP Email Form | BootstrapMade (2024)

PHP Email Form is simple and easy to use PHP script for sending the data submitted by web HTML forms (like contact forms) to your email inbox. The library is created by the BootstrapMade team and available in the paid versions of templates published on BootstrapMade.com. It works out of the box with the included contact forms in our templates.

Table of contents

  1. Setting up the PHP Email Form as a contact form
  2. Adding multiple receiving email addresses
  3. Spam protection
  4. Adding attachment to your form
  5. Adding "Accept terms/privacy policy" checkbox to your form
  6. Advanced Usage and Customization of the PHP Email Form
  7. Customizing the error messages
  8. Requirements

1. Setting up the PHP Email Form as a contact form

We do provide contact forms in all our templates. The PHP Email Form works out of the box with our contact forms. You just need to configure your receiving email address if you have PHP support on your hosting.

  1. If you already started working with the free version of the template: Download the pro version from your member area, uzip it and copy the /assets/vendor/php-email-form/php-email-form.php to the /assets/vendor/php-email-form/ folder of your working project.
  2. Edit /forms/contact.php and replace [emailprotected] with your email address and you’re done!

PHP Email Form uses the PHP mail() function for mailing by default. The /forms/contact.php also comes with a configuration for SMTP, in case your hosting doesn’t allow sending emails with the PHP mail() function. The SMTP configurations are commented. You need to uncomment the SMTP configuration lines and add your credentials.

2. Adding multiple receiving email addresses

You can also add multiple receiving email addresses for the submitted forms via cc and bcc methods. Edit and add the below code to your forms/contact.php just before
the line echo $contact->send();

$contact->cc = array('[emailprotected]', '[emailprotected]');$contact->bcc = array('[emailprotected]', '[emailprotected]');

3. Spam protection

We use AJAX method of form submission by default which stops most of the spam bots. You can add extra spam protection to your forms by using the below 2 methods.

1. Spam Protection with honeypot method
Honeypot method is a very clever method of spam protection. You just add an extra field to your form, which is hidden for your users and not filled while submitting it. Spam bots fills it thinking that it’s a standard required field and alert us it’s activity. If the honeypot field is filled in, we can confidently reject the form as spam. To use the honeypot method:

First, edit your form and add an extra filed, e.g:

<div class="form-group d-none"><input type="text" class="form-control" name="first_name"><div class="validate"></div></div>

Then, edit your /forms/contact.php and add the below code just before the line echo $contact->send();

$contact->honeypot = $_POST['first_name'];

You can change the filed name first_name as you wish.

2. Spam Protection with Google reCaptcha
Google reCaptcha is the most popular form submission protection in the world. PHP Email Form comes with built in support for the latest version 3.0 of Google reCaptcha. Follow the below steps to enable Google reCaptcha protection for your form.

1. Go to Google reCapthca administration website, set-up your website and obtain your keys (site key and secret key)

2. Add the below line to your web page in the footer part along with the other script inclusions.

<script src="https://www.google.com/recaptcha/api.js?render=Your_reCAPTCHA_site_key"></script>

Replace Your_reCAPTCHA_site_key with the site key you obtained in the step 1

3. Add data-recaptcha-site-key to your form tag, e.g:

<form action="forms/contact.php" method="post" role="form" class="php-email-form" data-recaptcha-site-key="Your_reCAPTCHA_site_key">

Replace Your_reCAPTCHA_site_key with the site key you obtained in the step 1

4. Edit your /forms/contact.php and add the below code just before the line echo $contact->send();

$contact->recaptcha_secret_key = 'Your_reCAPTCHA_secret_key';

Replace Your_reCAPTCHA_secret_key with the secret key you obtained in the step 1

You’re done. Your form now should be protected with the Google reCaptcha service!

4. Adding attachment to your form

You can also add a file upload filed to your form. You'll receive the user submitted file as an attachment to your email.

1. Add a file filed to your form, e.g:

<div class="form-group mt-3"> <input class="form-control" type="file" name="resume"></div>

2. Edit your /forms/contact.php and add the below code just before the line echo $contact->send();

$contact->add_attachment('resume', 20, array('pdf', 'doc', 'docx', 'rtf'));

The first value (resume) of the add_attachment method is the name of the file field, as set in the step 1.

The second value (20) is the maximum allowed file size in MB

The third value array('pdf', 'doc', 'docx', 'rtf') is an array of allowed file extensions

5. Adding "Accept terms/privacy policy" checkbox to your form

Here is an example code in case you need to add a checkbox to your form and require your users to accept your terms or privacy policy before submitting the form.

First, add the below code to your form, just before the submit button:

<div class="form-check form-group ps-0"> <input id="privacy-policy" type="checkbox" name="privacy" value="accept" required> <label class="form-check-label ps-1" for="privacy-policy"> Accept our <a href="terms.html">terms of service</a> and <a href="privacy.html">privacy policy</a> </label></div>


Then, edit your /forms/contact.php and add the below code just before the line echo $contact->send();

if($_POST['privacy'] !='accept') { die('Please, accept our terms of service and privacy acy policy');}

6 Advanced Usage and Customization of the PHP Email Form

The PHP Email Form can also be used for emailing any forms with unlimited custom inputs. It also comes with integrated SMTP support that allows you to send emails without a local mail server.

You can just duplicate the forms/contact.php to a new file and use it as a starting point, e.g: myform.php
You also need to set the action property to your new form to the newly created myform.php, e.g:
<form action="forms/myform.php" method="post" class="php-email-form">

Below is a full documentation of each line of the forms/contact.php and how you can customize them for your needs. First, below is the full content of the contact.php. Scroll down for detailed explanation.

<?php/*** Requires the "PHP Email Form" library* The "PHP Email Form" library is available only in the pro version of the template* The library should be uploaded to: vendor/php-email-form/php-email-form.php* For more info and help: https://bootstrapmade.com/php-email-form/*/// Replace [emailprotected] with your real receiving email address$receiving_email_address = '[emailprotected]';if( file_exists($php_email_form = '../assets/vendor/php-email-form/php-email-form.php' )) {include( $php_email_form );} else {die( 'Unable to load the "PHP Email Form" Library!');}$contact = new PHP_Email_Form;$contact->ajax = true;$contact->to = $receiving_email_address;$contact->from_name = $_POST['name'];$contact->from_email = $_POST['email'];$contact->subject = $_POST['subject'];// Uncomment below code if you want to use SMTP to send emails. You need to enter your correct SMTP credentials/*$contact->smtp = array('host' => 'example.com','username' => 'example','password' => 'pass','port' => '587');*/$contact->add_message( $_POST['name'], 'From');$contact->add_message( $_POST['email'], 'Email');$contact->add_message( $_POST['message'], 'Message', 10);echo $contact->send();?>
// Replace [emailprotected] with your real receiving email address$receiving_email_address = '[emailprotected]';

Here we set the receiving email address variable, which we will later use when setting up the PHP Email Form class properties. It’s not required to do it this way, we just moved this configuration at the top in the contact.php since it’s the only configuration setting that requires editing for the default contact form.

if( file_exists($php_email_form = '../assets/vendor/php-email-form/php-email-form.php' )) {include( $php_email_form );} else {die( 'Unable to load the "PHP Email Form" Library!');}

Here we check if the PHP Email Form library exists and then include it. The script will end execution if php-email-form.php library file doesn’t exists.

$contact = new PHP_Email_Form;

Here we initiate the PHP Email Form and assign it to $contact variable. You can change the $contact to your own.

$contact->ajax = true;

We use the AJAX method of posting the form in our contact form. This setting checks if the post method is really coming from an AJAX call and outputs error if isn’t. The ajax property is false by default

$contact->to = $receiving_email_address;$contact->from_name = $_POST['name'];$contact->from_email = $_POST['email'];$contact->subject = $_POST['subject'];

Al of these four properties to from_name from_email subject are required and need to be set up.

The to property is the receiving email address of the form. You can set it directly from here as $contact->to = '[emailprotected]'; or use the $receiving_email_address variable as we do.

The from_name property is the email sender name. In our contact form we capture the input value with name “name”. You can set it directly from here as $contact->from_name = 'Custom Name'; or other input value with a different name, e.g: $contact->from_name = $_POST['sender_name'];

The from_email property is the sender email address. In our contact form we capture the input value with name “email”. You can set it directly from here as $contact->from_email = '[emailprotected]'; or other input value with a different name, e.g: $contact->from_email = $_POST['email_address'];

The subject property is the email subject. In our contact form we capture the input value with name “subject”. You can set it directly from here as $contact->subject = 'My Subject'; or other input value with a different name, e.g: $contact->subject = $_POST['form_subject'];

// Uncomment below code if you want to use SMTP to send emails. You need to enter your correct SMTP credentials/*$contact->smtp = array('host' => 'example.com','username' => 'example','password' => 'pass','port' => '587');*/

PHP Email Form uses the PHP mail() function for mailing by default. You can use SMTP if your hosting doesn’t support the PHP mail() function or you can prefer the SMPT. To use SMTP, just uncomment the above code and add your credentials.

$contact->add_message( $_POST['name'], 'From');$contact->add_message( $_POST['email'], 'Email');$contact->add_message( $_POST['message'], 'Message', 10);

Here we prepare and compose the message content that will be included in the email body with using the add_message() method, which is a part of PHP Mail Form. It can be used unlimited times based on your needs. As you can see, we use it 3 times in our contact.php. The formatted message that you’ll receive in your email will be as an example below:
Email: Sender Name
From: [emailprotected]
Message: The message text

add_message() method accepts 3 parameters. The first one is the message text, the second one is a title/label and the third one is length check number (it will output error if the provided message text characters count is under the set length).

echo $contact->send();

It will output a text message “OK” if the email is sent successfully or an error message if the email is not sent for some reason.

7. Customizing the error messages

You can also customize the returned error messages by the PHP Email Form. Here are the available error message properties and their default values.

$contact->invalid_to_email = 'Email to (receiving email address) is empty or invalid!';$contact->invalid_from_name' = 'From Name is empty!';$contact->invalid_from_email' = 'Email from: is empty or invalid!';$contact->invalid_subject' = 'Subject is too short or empty!';$contact->short' = 'is too short or empty!'; // If the length check number is set and the provided message text is under the set length in the add_message() method call$contact->ajax_error' = 'Sorry, the request should be an Ajax POST'; // If ajax property is set true and the post method is not an AJAX call

8. Requirements

PHP Email Form requires at last PHP version 5.5 in your hosting server. Your hosting should allow you to send emails with using the PHP’s mail() function. You can use SMTP method if the mail() function is not supported.

PHP Email Form | BootstrapMade (2024)

FAQs

What is PHP email form? ›

PHP Email Form is simple and easy to use PHP script for sending the data submitted by web HTML forms (like contact forms) to your email inbox. The library is created by the BootstrapMade team and available in the paid versions of templates published on BootstrapMade.com.

Can you send emails with PHP? ›

PHP's built-in mail() function is one of the simplest ways to send emails directly from the web server itself. It just takes three mandatory parameters: the email address, email subject and message body—and sends it to the recipient.

How can I get form data from email in PHP? ›

Follow these steps to send the HTML form submission directly to an email address:
  1. Create a contact form using HTML and CSS.
  2. Host HTML form on live web hosting server to write PHP script.
  3. Open your VS Code editor.
  4. Connect VS Code editor to live web server using SFTP extension.
  5. Change your file extension from .
28 Dec 2021

Can I send HTML form data to email? ›

There is no feature in HTML to send the form submission directly to an email address. What about “mailto” ? Using mailto: You can set the action field of the form as 'mailto'. In this case, the web browser invokes the email client to send the form submission to the email address specified.

How do I send a mail form? ›

Send emails from a HTML Contact Form - YouTube

Does PHP mail need SMTP? ›

Three, the PHP mail function () sends emails from your website which may cause issues with deliverability due to security concerns such as suspicion of spam and blacklisting. PHP mail() also does not usually allow you to send mail using an SMTP server externally, and it does not support SMTP authentication.

Does PHP mail use SMTP? ›

PHP mailer uses Simple Mail Transmission Protocol (SMTP) to send mail. On a hosted server, the SMTP settings would have already been set. The SMTP mail settings can be configured from “php. ini” file in the PHP installation folder.

Can I send email from localhost PHP? ›

The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.

How do I send data from email to form? ›

Blog
  1. Create an HTML form. Well, we have already created a lot of HTML forms in our other articles. ...
  2. Add CSS. Now, create a file called style. ...
  3. Add the PHP code. Now it's time to add some PHP code with the main PHP file so that we can receive the email when a user submits this contact form!
12 Oct 2020

How can I send an email response after a form submission? ›

How to Send Confirmation Emails to Users after Contact Form...
  1. Create a WordPress Form.
  2. Set up a Confirmation Email.
  3. Send to Email Address.
  4. Adjust the Email Subject.
  5. Set From Name.
  6. Set From Email.
  7. Adjust the Reply-To.
  8. Create the Message.
17 Jun 2022

How do I use Formsubmit io? ›

Just 3 easy steps
  1. Point your form to our server url. Set your form's action -attribute to our server url and specify a unique token generated from your email or your email itself. ...
  2. Confirm your email address. Go to your website and submit the form once or visit the url in your browser. ...
  3. You are all set to go! That's it!

Where do I put PHP code in HTML? ›

You can add PHP tags to your HTML Page. You simply need to enclose the PHP code with the PHP starts tag <? php and the PHP end tag ?>. The code wrapped between these two tags is considered to be PHP code, and it will be executed on the server-side before the requested file is sent to the client browser.

How do I create an HTML email? ›

How to create an HTML email
  1. Open an application where you can type HTML code. ...
  2. Begin your HTML document type. ...
  3. Create the body and main table. ...
  4. Design the email template structure and header. ...
  5. Create the content area. ...
  6. Change the style of the email template footer. ...
  7. Style the text. ...
  8. Test the email.
2 Nov 2021

How do I send an HTML email? ›

There are three ways you can load the Gmail Compose window with your custom HTML.
  1. Copy/paste the rendered HTML into the Gmail Compose window.
  2. Paste your HTML code into the Gmail Compose window using Chrome's Developer Tools.
  3. Use a Chrome extension to add an HTML editor to the Gmail Compose box.
30 Aug 2021

What is full form email? ›

e-mail, in full electronic mail, messages transmitted and received by digital computers through a network.

How do I create a working form in HTML? ›

How to Create an HTML Contact Form from Scratch
  1. Choose an HTML editor.
  2. Create a new file with the .HTML extension.
  3. Create a new file with the .PHP extension.
  4. Generate the PHP code to capture form data.
  5. Create your HTML contact form.
22 Dec 2020

How do I send an HTML email in Gmail? ›

Let's Insert HTML into Gmail
  1. Grab the code that you saved as an HTML file and open it in your browser of choice. ...
  2. From there, you simply need to copy the HTML as it has rendered in the browser.
  3. Paste that into your new Gmail compose window.
  4. Press send, and then you're all done.
12 Nov 2020

How many emails can PHP mail send? ›

can send more than 10000 mail without using smtp or other mail server? No it cannot send even 1 email if you don't provide it either of them. If you don't provide an external smtp then you have to have a mail server installed on the machine itself. Once that is done, 10k mails are not a problem.

Is PHP mail secure? ›

This is perfectly secure; there is no way a hacker could manipulate who the E-mail gets sent to; PHP is server-side code. Thus, $email_to = "email@email.com"; cannot get manipulated from the form itself, as it is hard-coded into your PHP.

How configure SMTP in PHP? ›

Writing the PHP Code to Send Email using Gmail SMTP
  1. Step 1: Download PHPMailer library from this github link. ...
  2. Step 2: Writing the PHP Code to make an SMTP connection. ...
  3. Step 3: Include packages and files for PHPMailer and SMTP protocol: ...
  4. Step 4: Initialize PHP Mailer and set SMTP as mailing protocol:
9 Aug 2022

How can I send mail faster in PHP? ›

There are several ways to send e-mail messages in PHP.
...
E-mail delivery methods
  1. PHP mail() function. ...
  2. SMTP server relay. ...
  3. Sending urgent messages by doing direct delivery to the destination SMTP server. ...
  4. Sendmail program.

How can I tell if PHP email is enabled? ›

to check if it is sending mail as intended; <? php $email = "youremail@gmail.com"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>

How do I send via SMTP? ›

When you send emails with an SMTP service provider, follow these five simple steps:
  1. Step 1: Gather and enter the information. This is where you get everything together to use the SMTP. ...
  2. Step 2: Access the SMTP interface. ...
  3. Step 3: Add a new SMTP. ...
  4. Step 4: Authenticate your account. ...
  5. Step 5: Create an email address.
18 May 2022

How do I setup an SMTP server? ›

And here's the standard procedure of SMTP configuration, in four steps:
  1. Select the voice “Account Settings” in your mail client, generally in the “Tools” menu.
  2. Choose the “Outgoing server (SMTP)” voice:
  3. Push the “Add…” button in order to set a new SMTP. A popup window will appear:
  4. Now simply fill the voices as follows:

Can we send mail from localhost? ›

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost. For example, you can configure C:\xampp\php\php. ini and c:\xampp\sendmail\sendmail.

How do I setup a local mail server? ›

How do I configure a local SMTP server?
  1. You will need your Windows installation CD.
  2. Use Add or Remove Programs in the Windows Control Panel to launch Add/Remove Windows Components.
  3. Select Internet Information Services (IIS) and then click Details.
  4. Check SMTP Service and then click OK.
25 Aug 2022

How do I send data from email to form? ›

Blog
  1. Create an HTML form. Well, we have already created a lot of HTML forms in our other articles. ...
  2. Add CSS. Now, create a file called style. ...
  3. Add the PHP code. Now it's time to add some PHP code with the main PHP file so that we can receive the email when a user submits this contact form!
12 Oct 2020

How do I use Formsubmit io? ›

Just 3 easy steps
  1. Point your form to our server url. Set your form's action -attribute to our server url and specify a unique token generated from your email or your email itself. ...
  2. Confirm your email address. Go to your website and submit the form once or visit the url in your browser. ...
  3. You are all set to go! That's it!

How can I send an email response after a form submission? ›

How to Send Confirmation Emails to Users after Contact Form...
  1. Create a WordPress Form.
  2. Set up a Confirmation Email.
  3. Send to Email Address.
  4. Adjust the Email Subject.
  5. Set From Name.
  6. Set From Email.
  7. Adjust the Reply-To.
  8. Create the Message.
17 Jun 2022

How can be file uploaded in PHP explain with an example? ›

PHP File Upload
  1. Configure The "php.ini" File. First, ensure that PHP is configured to allow file uploads. ...
  2. Check if File Already Exists. Now we can add some restrictions. ...
  3. Limit File Size. The file input field in our HTML form above is named "fileToUpload". ...
  4. Limit File Type. ...
  5. Complete Upload File PHP Script.

How do I send an HTML email? ›

There are three ways you can load the Gmail Compose window with your custom HTML.
  1. Copy/paste the rendered HTML into the Gmail Compose window.
  2. Paste your HTML code into the Gmail Compose window using Chrome's Developer Tools.
  3. Use a Chrome extension to add an HTML editor to the Gmail Compose box.
30 Aug 2021

How do I create an HTML email? ›

How to create an HTML email
  1. Open an application where you can type HTML code. ...
  2. Begin your HTML document type. ...
  3. Create the body and main table. ...
  4. Design the email template structure and header. ...
  5. Create the content area. ...
  6. Change the style of the email template footer. ...
  7. Style the text. ...
  8. Test the email.
2 Nov 2021

How do you insert an email in HTML? ›

How to make an email link in HTML
  1. Open your HTML file and choose where to insert your email link.
  2. Type in the anchor tag "a href=" after the "<" symbol to show a link in your HTML code.
  3. Include the "mailto:" tag after the "=" to send the link to an email address.

Is Getform IO free? ›

You don't need to pay or even provide your bank card details to start using Getform. Once you sign up, you automatically get a Free plan with a full range of features.

Is FormSubmit Co free? ›

FormSubmit is a free service. This is the only way we are making a profit from it.

What is form addEventListener? ›

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target. Common targets are Element , or its children, Document , and Window , but the target may be any object that supports events (such as XMLHttpRequest ).

How do I power an automated email form? ›

Steps to send an email based on Form response using Microsoft Flow
  1. Step 1: Create a Microsoft Form. ...
  2. Step 2: Create a flow in Microsoft Flow. ...
  3. Step 3: When a new response is submitted. ...
  4. Step 4: Get response Details. ...
  5. Step 5: Send a feedback email to the technology instructor. ...
  6. Step 6: Run the Flow.
27 Nov 2021

How do I send an automated email response? ›

Try it!
  1. Select File > Automatic Replies. ...
  2. Select Send automatic replies.
  3. If you don't want the messages to go out right away, select Only send during this time range.
  4. Choose the dates and times you'd like to set your automatic reply for.
  5. Type in a message. ...
  6. Select OK.

What is an automated email? ›

Automated email, also referred to as triggered email or behavior-driven email, is any message automatically sent from your email service provider (ESP) in direct response to an individual user's specific actions made (or not made) on your website or web app.

What is PHP full form? ›

What is PHP? PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language. PHP scripts are executed on the server. PHP is free to download and use.

What is $_ files in PHP? ›

PHP $_FILES

The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.

Where does PHP store uploaded files? ›

php stores all temporary files, that includes uploaded files, in the temporary files directory as specified in the php. ini.

Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 5891

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.