dwinnbrown February 2016

Adding Cc to PHP mail

So I am mailing the contents of a form to a client and he would like the person who sent the form to be Cc'd in.

I have done some research and it appears I need to use the header code to set the from, subject and cc but my code is set up differently - please see below:

<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
$recipient = "email@mydomain.com";
$subject = "More information regarding $relatedproduct";

$mailheader = "From: $email \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

header('Location: ' . $_SERVER['HTTP_REFERER']);
?>

Would it be possible to do it like this?

<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";


$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";


// Mail it
mail($to, $subject, $formcontent, $headers);
?>

Answers


Egg February 2016

php.net/mail

Yes, as you have it:

...
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthday@example.com' . "\r\n";
mail($to, $subject, $content, $headers);
?>

Incidentally, please sanitise your POST variables before you inject them (e.g. $to = $_POST['email'];).

As @Rhopercy says in their comment, perhaps an email library will help you as it takes care of most things for you. Take a look at PHPMailer or SwiftMail.


Atif Tariq February 2016

Should be like this:

    $relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

$headers = "From: $email\r\nReply-To: $email";
$headers .= 'Cc: test@test.com\r\n';
$headers .= 'Bcc: test@test.com\r\n';
$headers .= "Return-Path: <info@premierinspectiontn.com>\r\n";

$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";

// Mail it
mail($to, $subject, $formcontent, $headers);

Post Status

Asked in February 2016
Viewed 1,568 times
Voted 9
Answered 2 times

Search




Leave an answer