Wicked Satan
Wicked Satan

Reputation: 21

How to send form data to multiple email addresses using PHP?

I am a web designer, and dont really know much about PHP. I have a form, and I want the values to be sent to three email addresses.

Here is my HTML:

<form id="player" method="post" action="process.php">

    <label for="name">Your Name</label>
    <input type="text" name="name" title="Enter your name" class="required">

    <label for="phone">Daytime Phone</label>
    <input type="tel" name="phone" class="required">

    <label for="email">Email</label>
    <input type="email" name="email" title="Enter your e-mail address" class="required email">


    <input type="submit" name="submit" class="button" id="submit" value="I'd like to join Now" />

</form>

I have somehow found a PHP code that should send the data to ONE email address only, but I dont even know if it works or not.

Here is the code for that:

<?php
// Get Data 
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);

// Send Message
mail( "[email protected]", "Contact Form Submission",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
"From: Forms <[email protected]>" );
?>

Thanks

Upvotes: 0

Views: 23622

Answers (4)

helper
helper

Reputation: 1

use

$to = "[email protected]"
$to .= ", [email protected]";

this will help you to create multiple recipient.

Upvotes: 0

Michał Wojciechowski
Michał Wojciechowski

Reputation: 2490

The mail function (which is used in the code that you posted) allows you to specify multiple recipients. See the PHP documentation of that function for details: http://php.net/manual/en/function.mail.php

Edit: You basically need to replace the "[email protected]" part with a list of addresses, separated by commas, e.g.:

mail("[email protected],[email protected],[email protected]", ...

Upvotes: 3

Ahmad Hajjar
Ahmad Hajjar

Reputation: 1853

Add headers

 <?php
 // Get Data 
 $name = strip_tags($_POST['name']);
 $email = strip_tags($_POST['email']);
 $phone = strip_tags($_POST['phone']);
 $url = strip_tags($_POST['url']);
 $message = strip_tags($_POST['message']);

 $headers .="From: Forms <[email protected]>";
 $headers .="CC: Mail1 <[email protected]>";
 $headers .=", Mail2 <[email protected]>";

 // Send Message
 mail( "[email protected]", "Contact Form Submission",
 "Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
  $headers );
 ?>

Upvotes: 4

Martin Bean
Martin Bean

Reputation: 39389

You should be able to separate email addresses with commas in the first parameter of the mail() function, i.e.

mail('[email protected], [email protected]', $subject, $message, $headers);

Or sepcific CC and optionally BCC addresses as per Ahmad's answer.

Upvotes: 4

Related Questions