0

Possible Duplicate:
PHP Mail, CC Field

I have the task of adding a CC to a form on a clients website. All I really need is for the form to submit to the recipient specified as theemail@address.com and be CC'ed (Carbon Copied) to another email address.

Here's what I have PHP wise, at the top of the document above the doctype (HTML 5)

<?php
    if(isset($_POST['submit'])) {

        if(trim($_POST['contactname']) == '') {
            $hasError = true;
        } else {
            $name = trim($_POST['contactname']);
        }

        if(trim($_POST['subject']) == '') {
            $hasError = true;
        } else {
            $subject = trim($_POST['subject']);
        }


        if(trim($_POST['email']) == '')  {
            $hasError = true;
        } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
            $hasError = true;
        } else {
            $email = trim($_POST['email']);
        }

        if(trim($_POST['message']) == '') {
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $comments = stripslashes(trim($_POST['message']));
            } else {
                $comments = trim($_POST['message']);
            }
        }

        if(!isset($hasError)) {
            $emailTo = 'theemail@address.com';
            $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
            $headers = 'From: Sigma Web Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

            mail($emailTo, $subject, $body, $headers);
            $emailSent = true;
        }
    }
?>
Community
  • 1
  • 1
pixelator
  • 689
  • 1
  • 7
  • 12

4 Answers4

1

To add CC then add this header

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

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
1

Change

$headers = 'From: Sigma Web Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

to

$headers = 'From: Sigma Web Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email. "\r\n";
$headers .= 'Cc: '.$ccemail; // $ccemail is carbon copying email address

see more about mail()

LIGHT
  • 5,604
  • 10
  • 35
  • 78
  • Thanks Prakash, worked a treat, but needed your version of before and after, as I said not really a PHP guy. – pixelator Oct 23 '12 at 17:50
0
$headers .= "CC: sombodyelse@noplace.com\r\n";
Anthony
  • 648
  • 1
  • 7
  • 22
0

You can check out the manual example:

 $headers .=  "\r\n".'Cc: yourccemail@yourdomain.com' . "\r\n";

Also, php provides email validation with filter_var

  filter_var('bob@example.com', FILTER_VALIDATE_EMAIL);
janenz00
  • 3,315
  • 5
  • 28
  • 37