Php email send copy to sender

2.9k views Asked by At

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 = '[email protected], [email protected]';//<-----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

There are 3 answers

1
Lal krishnan S L On

You can use cc n header

$headers .= 'Cc: [email protected]' . "\r\n";

More idea

1
Robin Woudstra On

You can also try to add another mail function like this

mail($email_address,$email_subject,$email_body,$headers);
0
Drazisil On

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

Examples of using CC and BCC.

Example #4 Sending HTML email

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";