【问题标题】:PHP form sends emails to different recipients based on yes or no variablesPHP 表单根据是或否变量向不同的收件人发送电子邮件
【发布时间】:2017-04-01 08:43:27
【问题描述】:

我有一个包含 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
}

【问题讨论】:

  • 建立一个电子邮件数组。那么你只需要4个条件。构建阵列后发送电子邮件。

标签: php forms email phpmailer


【解决方案1】:

我个人不喜欢这种switch(true) 方法。 Clean Code(顺便说一句好书)总是建议我们使用可读的代码,所以你可以这样翻译:

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(...);
}

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

【讨论】:

  • 类常量中的数组?
  • 你可以在 PHP 5.6+ 中做到这一点
【解决方案2】:

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

$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';
}

【讨论】:

  • 这是一个很好的方法,但请确保您过滤掉重复的电子邮件地址。
  • 是的@Progrock,array_unique($emailList) 将删除您的重复项。
  • 我试过了,但它似乎不起作用。还尝试添加 $emailList = implode(' , ',$emailList);发送邮件($电子邮件列表);该数组包含电子邮件,但 PHPMailer 似乎没有将它们识别为抄送。
  • @CB81 phpmailer CC 由 $mail->addCC('cc@example.com') 添加;在使用 $mail->send()... 之前,请查看 phpmailer 文档:phpmailer.github.io/PHPMailer 和 github github.com/PHPMailer/PHPMailer
【解决方案3】:

这是另一种方法:

// 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);

【讨论】:

    【解决方案4】:

    很像 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      相关资源
      最近更新 更多