PHP表单根据是或否变量向不同的收件人发送电子邮件

PHP表单根据是或否变量向不同的收件人发送电子邮件,php,forms,email,phpmailer,Php,Forms,Email,Phpmailer,我有一个包含4个问题的表格,每个问题可以选择是或否。提交表单时,我想使用PHPMailer发送一封电子邮件。有7个人可能需要抄送,但仅当适用于他们的问题选择了“是”时才需要抄送 问题1: 如果是,抄送收件人1 问题2: 如果是,抄送收件人2、收件人3 问题3: 如果是,抄送收件人4、收件人5、收件人6、收件人7 问题4: 如果是,抄送收件人6、收件人7 目前,我正在使用下面的switch语句,这很有效,但我总共有16个案例。有没有一种我没有想到的更简单的方法 switch (true) {

我有一个包含4个问题的表格,每个问题可以选择是或否。提交表单时,我想使用PHPMailer发送一封电子邮件。有7个人可能需要抄送,但仅当适用于他们的问题选择了“是”时才需要抄送

问题1: 如果是,抄送收件人1

问题2: 如果是,抄送收件人2、收件人3

问题3: 如果是,抄送收件人4、收件人5、收件人6、收件人7

问题4: 如果是,抄送收件人6、收件人7

目前,我正在使用下面的switch语句,这很有效,但我总共有16个案例。有没有一种我没有想到的更简单的方法

switch (true) {
    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('recipient1@example.edu', 'recipient2@example.edu', 'recipient2@example.edu', 'recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('recipient1@example.edu', 'recipient2@example.edu', 'recipient2@example.edu', 'recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('recipient1@example.edu', 'recipient2@example.edu', 'recipient2@example.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'No'):
    sendmail('recipient1@example.edu', 'recipient2@example.edu', 'recipient2@example.edu');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('recipient1@example.edu', 'recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('recipient1@example.edu', 'recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('recipient1@example.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'No' and $Question4 === 'No'):
    sendmail('recipient1@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('recipient2@example.edu', 'recipient2@example.edu', 'recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('recipient2@example.edu', 'recipient2@example.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'No'):
    sendmail('recipient2@example.edu', 'recipient2@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('recipient2@example.edu', 'recipient2@example.edu', 'recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'No' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('recipient6@example.edu', 'recipient7@example.edu');
    break;

    case ($Question1 === 'No' and $Question2 === 'No' and $Question3 === 'No' and $Question4 === 'No'):
    sendmail();
    break;

    default:
sendmail();

    }



function sendmail($cc, $cc2, $cc3, $cc4, $cc5, $cc6, $cc7){
$mail             = new PHPMailer;
more PHPMailer code here
}

我个人不喜欢这种
切换(true)
方法。 (顺便说一句,这本好书)总是建议我们使用可读代码,因此您可以这样翻译:

switch (true) {
    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('recipient1@example.edu', 'recipient2@example.edu', 'recipient2@example.edu', 'recipient4@example.edu', 'recipient5@example.edu@goodwin.edu', 'recipient6@example.edu', 'recipient7@example.edu');
    break;
...
为此:

class AnswersEnum
{
    const SCENARIO_1 = ['Yes', 'Yes', 'Yes', 'Yes']; //use meaningful names here
    const SCENARIO_2 = ['Yes', 'Yes', 'Yes', 'No'];
    const SCENARIO_3 = ['Yes', 'Yes', 'No', 'No'];
    ...
}
OtherClass.php

....
$answers = [$Question1, $Question2, $Question3, $Question4];

switch($answers) {
    case AnswersEnum::SCENARIO_1:  //use a meaningful name here
        sendMailToScenario1recipients();
        break;
    case AnswersEnum::SCENARIO_2:  //use a meaningful name here
        sendMailToScenario2recipients();
        break;
    ....

private function sendMailToScenario1recipients()
{
    sendmail(...);
}

这是一种方法。因为我看不到您的整个项目,所以很难说哪种解决方案更好。

我没有包括您的所有案例,但您可以在这里看到,通过将每个电子邮件收件人添加到注释中@Alejandro C提到的数组中,您可以创建您的电子邮件列表。根据需要更改条件和电子邮件列表添加项

$mailer = phpmailer();
$emailList = array();

if($Question1 == "Yes") {
    $emailList[] = "recipient1@example.edu";
    $emailList[] = "recipientY@example.edu";
}
if($Question2 == "Yes") {
    $emailList[] = "recipientX@example.edu";
    $emailList[] = "recipientXY@example.edu";
}
if($Question3 == "Yes") {
    $emailList[] = "recipientXX@example.edu";
}
if($Question4 == "Yes") {
    $emailList[] = "recipientXXX@example.edu";
    $emailList[] = "recipientYXY@example.edu";
}
$emailList = array_unique($emailList);

foreach($emailList as $val) {
    $mailer->addCC($val);
}

// make sure you have added all the other necessary info to $mailer
if(!$mailer->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

以下是另一种方法:

// Define answers as an array
$answers = [
    'Q1' => 'Yes', 
    'Q2' => 'No', 
    'Q3' => 'No', 
    'Q4' => 'Yes',
];

// Define subscribers (this could be database driven)
$subscribers = [
    'Q1' => ['recipient1@example.edu'],
    'Q2' => ['recipient2@example.edu', 'recipient3@example.edu'],
    'Q3' => ['recipient4@example.edu', 'recipient5@example.edu', 'recipient6@example.edu', 'recipient7@example.edu'],
    'Q4' => ['recipient6@example.edu', 'recipient7@example.edu']
];

// Determine list of recipients
$recipients = [];
foreach ($answers as $question => $answer) {
    if ($answer == 'Yes') {
        $recipients = array_unique(array_merge($recipients, $subscribers[$question]));
    }
}

// Send mail however you planned on sending it
sendmail($recipients);

与Jeremy的答案非常相似,但添加了表单和答案过滤器

<?php

$questions = [
    'Q1' => 'Do you like pigs?',
    'Q2' => 'Is Percy a good name for a pig?',
    'Q3' => 'Would you eat a pig?'
];

$yes_people = [
    'Q1' => ['foo@example.com'],
    'Q2' => ['bar@example.com', 'baz@example.com'],
    'Q3' => ['big@example.com', 'fat@example.com', 'baz@example.com'],
];

if($_SERVER['REQUEST_METHOD'] == 'POST') {

    $answers = [];

    foreach(array_keys($questions) as $key)
        $answers[$key] = isset($_POST[$key]) ? $_POST[$key] : null;

    function answer_filter($item) {
        return in_array($item, ['Yes', 'No']) ? $item : 'Invalid';
    }

    $answers = array_map('answer_filter', $answers);

    // Build message content here.

    $recipients = [];
    foreach($answers as $key => $value)
        if($value == 'Yes')
            $recipients = array_unique(array_merge($recipients, $yes_people[$key]));

    var_dump($recipients);
}
?>
<form method="POST" action="">
    <?php foreach($questions as $key => $question) { ?>
        <p><?= $question ?></p>
        <input type="radio" name="<?= $key ?>" value="Yes">Yes
        <input type="radio" name="<?= $key ?>" value="No">No
    <?php } ?>
    <br/>
    <input type="submit" value="Go">
</form>


而是建立一系列电子邮件。那么你只需要4个条件。一旦构建了数组,就发送电子邮件。类常量中的数组?您可以在PHP 5.6+中执行此操作。这是一个很好的方法,但请确保在创建后过滤掉重复的电子邮件地址。是的@Progrock,array_unique($emailList)将删除您的重复项。我尝试了此方法,但似乎不起作用。还尝试添加$emailList=内爆(“,”,$emailList);sendmail($emailList);数组包含电子邮件,但PHPMailer似乎没有将其识别为抄送。@CB81 PHPMailer抄送是通过$mail->addCC('s)添加的cc@example.com'); 在使用$mail->send()之前。。。在github上查看phpmailer文档:和