与CC的PHP联系表

时间:2012-10-23 17:34:20

标签: php forms

  

可能重复:
  PHP Mail, CC Field

我的任务是在客户网站上的表单中添加CC。我真正需要的是将表单提交给指定为theemail@address.com的收件人,并将CC(碳复制)提交到另一个电子邮件地址。

这是我明智的PHP,位于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;
        }
    }
?>

4 个答案:

答案 0 :(得分:1)

要添加CC,请添加此标题

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

答案 1 :(得分:1)

更改

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

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

详细了解mail()

答案 2 :(得分:0)

$headers .= "CC: sombodyelse@noplace.com\r\n";

答案 3 :(得分:0)

您可以查看手册example:

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

另外,php使用filter_var提供电子邮件验证

  filter_var('bob@example.com', FILTER_VALIDATE_EMAIL);