Last Updated on February 15, 2023 by Roshan Parihar
In this tutorial, learn how contact form send to multiple emails in PHP. You can add as many recipient email addresses as you want to send emails to them.
When you have multiple recipients who want to receive contact form data via email, you can use this type of contact form on your website.
Looking to create contact forms to send emails to multiple recipients without coding? Create beautiful-looking contact forms with JotForm.
You can send emails to the website, owner, team members, and others. It can help your business to handle queries faster and increase performance.
So, let’s find out how you can create a contact form to send users’ submitted data to multiple email addresses using PHP.
How to Create Contact Form Send to Multiple Emails in PHP (Step-by-step)
Here is the step-by-step process on it:-
Step 1: Create Contact Form Design
HTML Page Code
First, Create the basic structure of the HTML page. Let me help you with the example HTML page code given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Contact Form</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- place CSS code here --> </head> <body> <!-- place Form HTML code here --> </body> </html> |
Contact Form HTML Code Structure
After that, create a design for the contact form with input fields required to collect information about users. We need basic information like Name, Email, and, Message.
The ‘Name’ field is used to send an email with personalization (like Hi Rohan). The ‘Email’ field is useful to reach users via email and communication with them. The ‘Message’ field is useful to collect queries from the customers for which they want to contact us.
The overall structure of the contact form is as shown in the HTML code below. Place the below form code to the above HTML page code location indicated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<div class="main-contact-form"> <form action="#" method="POST"> <label for="Name">Your Name</label> <input type="text" name="name" class="name" placeholder="Enter name" required> <label for="Email">Your Email</label> <input type="email" name="email" class="email" placeholder="Enter email" required> <label for="message">Message</label> <textarea name="message" class="message" placeholder="Enter your message" rows="5" required></textarea> <input name="submit" type="submit" value="Submit"> </form> <br> <!-- Place PHP Code here --> </div> |
CSS Code to Make Better Contact Form Design
Also, the above HTML code is not enough to create a better design of the contact form. You have to add some CSS code that can improve your contact form fields and button design. Place the below CSS code to the HTML page code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<style> input[type='text'],input[type='email'], textarea{ width: 100%; padding: 10px; border-radius: 3px; border: 1px solid #ccc; margin-top: 10px; margin-bottom: 20px; } input[type='text']:focus,input[type='email']:focus, textarea:focus{ border: 1px solid #5db6db; box-shadow: 0 0 10px #b9eaff; outline: none !important; } input[type='submit']{ background: rgb(39,160,210); color: #fff; border: none; padding: 10px 20px; cursor: pointer; } .main-contact-form{ max-width: 400px; margin: 0 auto; background: #e9e9e9; padding: 20px 45px 20px 25px; border-radius: 5px; } </style> |
Now, your contact form design is ready to add to your website as shown in the image below.
But, before adding it to your website, you have to add some PHP code to send contact form data via emails to multiple recipients.
Step 2: PHP Code to Send Contact Form Data Via Emails to Multiple Recipients
To send the contact form data to multiple email addresses, you have to just add comma-separated email addresses inside the $to
variable. After that, pass the variable to the mail()
function as given in the PHP code below.
You have to first collect the form data using $_POST
global variable. After that, you can validate the email address using the regular expression and preg_match()
function.
If you want to make every field required for users to fill, you have already added required
attribute to each field in the HTML code above. So, there is no need to make fields required using extra PHP code.
isset()
function to execute the PHP code only when your users fill out the form and click the submit button. After that, use the mail()
as given in the PHP code below to send emails. The first variable of the function should contain multiple comma-separated variables. If you want to send emails using PHPMailer Gmail SMTP, you can read our post on Contact Form Using PHPMailer Gmail SMTP to Send Emails.
Let’s move further to send emails to multiple recipients using mail()
. See further code of the PHP given below as each line are well commented to help you easily understand them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php if(isset($_POST['submit'])){ //Get form data $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; //validation for email $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if (!preg_match($email_exp, $email)) { echo '<br><span style="color:red;">The Email address you entered is not valid.</span>'; exit; } $to = "[email protected], [email protected], [email protected], [email protected]"; //recipient email address $subject = "Contact Form Submitted by ".$name; //Subject of the email //Message content to send in an email $message = "Name: ".$name."<br>Email: ".$email."<br> Message: ".$message; //Email headers $headers = "From:".$email."\r\n"; $headers .= "Reply-To:".$email."\r\n"; //$headers .= "X-Mailer: PHP/".phpversion(); // Set content-type header for sending HTML email data $headers .= "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; //Send email $sentmail = mail($to, $subject, $message, $headers); //Check the result if(!$sentmail){ echo "<p class='error_message'>Message could not sent.</p>"; }else{ echo "<p class='success_message'>Message has been sent successfully.</p>"; } } ?> |
Explanation of the PHP Code to Send Multiple Emails
Let’s understand each variable to send emails using PHP code:-
$to
: You can place multiple recipient email addresses in comma separation to send emails to them.$subject
: The main subject of the email.$message
: The main content body of the email or query the customer’s businesses want to receive.$headers
:'From'
: the email address of the website owner that should be domain specific.'CC'
: the email address of the second person who wants to receive emails."Reply-To'
: the email address of the user who fills out the contact form.
mail()
: It is the built-in function of PHP to send emails. It required 4 variables to pass as an argument. The 4 variables are$to
,$subject
,$message
, and$headers
.
If you have any queries regarding the above tutorial, please comment below. I will reply to help it.
You May Also Like to Read