0

I have a contact form and I am using the below PHP Script to get emails from my contact form. I want to send a copy of the same email to the sender email also. Can anyone help me?. Thank you!

<?php 
$errors = '';
$myemail = 'chocolatehills_adventurepark@yahoo.com, chocolatehills88@yahoo.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
    empty($_POST['email']) || 
    empty($_POST['subject']) ||
    empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$subject = $_POST['subject']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Message from: $name";
    $email_body = "New message received. ".
    " Here are the details:\n \n    NAME: $name \n 
    SUBJECT: $subject \n
    EMAIL ADD: $email_address \n 
    MESSAGE: $message"; 

    $headers = "From: $email_address\n"; 
    //$headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-us.php');
} 
?>

3 Answers3

3

You can use cc n header

$headers .= 'Cc: somebody@domain.com' . "\r\n";

More idea

Community
  • 1
  • 1
Lal krishnan S L
  • 1,684
  • 1
  • 16
  • 32
0

You can also try to add another mail function like this

mail($email_address,$email_subject,$email_body,$headers);
  • Its better to add cc in the $headers variable. but if you add my line of code underneath the existing mail function it also works – Robin Woudstra Jun 10 '15 at 14:54
0

http://php.net/manual/en/function.mail.php

Examples of using CC and BCC.

Example #4 Sending HTML email

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
Drazisil
  • 3,070
  • 4
  • 33
  • 53