php电子邮件表单:发送到多个地址并从输入中排除数据

php电子邮件表单:发送到多个地址并从输入中排除数据,php,forms,email,Php,Forms,Email,我有一个非常简单的电子邮件联系人表单,我希望A)表单字段中的输入被发送到多个地址,B)除了一个收件人之外,所有收件人中都会排除一些数据 我已经解决了A部分;然而,B部分被证明是困难的,因为我只是一个中级网页设计师,在编码方面绝对不是一个专家。请在下面查找源代码: <?php if(isset($_POST['email'])) { $email_to = "firstemail@email.com,secondemail@email.com,thirdemail@email.com";

我有一个非常简单的电子邮件联系人表单,我希望A)表单字段中的输入被发送到多个地址,B)除了一个收件人之外,所有收件人中都会排除一些数据

我已经解决了A部分;然而,B部分被证明是困难的,因为我只是一个中级网页设计师,在编码方面绝对不是一个专家。请在下面查找源代码:

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

$email_to = "firstemail@email.com,secondemail@email.com,thirdemail@email.com";
$email_subject = "Your email subject line";

function died($error) {
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['business_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}



$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$business_name = $_POST['business_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}

if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}

if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}

$email_message = "Form details below.\n\n";


function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}



$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Business Name: ".clean_string($business_name). "\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

Thank you for contacting us. We will be in touch with you very soon.


<?php

}
?>

感谢您联系我们。我们将很快与您联系。
同样,发送到多个地址是可行的,但我基本上希望第一封电子邮件接收表单中的所有数据,所有其他电子邮件地址接收电子邮件中除电子邮件地址行以外的所有内容


谢谢大家!

将$email_分解为“,”,然后迭代数组以构建内容并发送

foreach($aMails as $sMail) {
    if($sMail == 'important@example.com') {
        //do the content stuff here
    }
    @mail($sMail, $email_subject, $email_message, $headers);
}

将$email_分解为“,”,然后迭代数组以构建内容并发送

foreach($aMails as $sMail) {
    if($sMail == 'important@example.com') {
        //do the content stuff here
    }
    @mail($sMail, $email_subject, $email_message, $headers);
}

作为一个经常因为电子邮件地址“无效”而被拒绝的人,我觉得有必要指出,你检查电子邮件的正则表达式是不正确的。使用
filter\u var($email,filter\u VALIDATE\u email)
代替。alex,你的电子邮件中的哪些模式通常会导致拒绝?manueloto加号。作为一个经常因为电子邮件地址“无效”而被拒绝的人,我觉得有必要指出你检查电子邮件的正则表达式是不正确的。改为使用
filter\u var($email,filter\u VALIDATE\u email)
。alex,您的电子邮件中的哪些模式通常会导致拒绝?@Manueloto加号。我很清楚(以防我弄糟),您编写的代码片段会覆盖我代码顶部的$email\u代码行,对吗?然后“//do the content stuff here”意味着将电子邮件中应该包含的所有内容放到我希望接收所有信息的电子邮件中?//do the content stuff here”意味着只做对特殊电子邮件地址重要的事情。当然,你不想一次又一次地为每一个内容做你需要的每一件事……我很清楚(万一我把它弄坏了),你写的代码片段会覆盖我代码顶部的$email_to代码行,对吗?然后“//do the content stuff here”意味着将电子邮件中应该包含的所有内容放到我希望接收所有信息的电子邮件中?//do the content stuff here”意味着只做对特殊电子邮件地址重要的事情。当然,你不想一次又一次地为每一个内容做你需要的事情。。。