CB81
CB81

Reputation: 77

PHP form sends emails to different recipients based on yes or no variables

I have a form that contains 4 questions, yes or no can be selected for each question. When the form is submitted I want to send out one email using PHPMailer. There are 7 people who possibly need to be cc'd but only if yes is selected for the question that applies to them.

Question 1: If yes, cc recipient1

Question 2: If yes, cc recipient2, recipient3

Question 3: If yes, cc recipient4, recipient5, recipient6, recipient7

Question 4: If yes, cc recipient6, recipient7

Currently I'm using the switch statement below, which works, but I have a total of 16 cases. Is there an easier way to do this that I'm not thinking of?

switch (true) {
    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'No'):
    sendmail('[email protected]', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('[email protected]', '[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'Yes' and $Question2 === 'No' and $Question3 === 'No' and $Question4 === 'No'):
    sendmail('[email protected]');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('[email protected]', '[email protected]', '[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'No' and $Question4 === 'No'):
    sendmail('[email protected]', '[email protected]');
    break;

    case ($Question1 === 'No' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]', '[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'No' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'No' and $Question2 === 'No' and $Question3 === 'Yes' and $Question4 === 'No'):
    sendmail('[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;

    case ($Question1 === 'No' and $Question2 === 'No' and $Question3 === 'No' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]');
    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
}

Upvotes: 1

Views: 70

Answers (4)

Chris O
Chris O

Reputation: 689

I didn't include all your cases, but you can see here that by adding each email recipient to an array as mentioned by @Alejandro C in the comments you can create your email list. Change conditionals and emailList additions as needed.

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

if($Question1 == "Yes") {
    $emailList[] = "[email protected]";
    $emailList[] = "[email protected]";
}
if($Question2 == "Yes") {
    $emailList[] = "[email protected]";
    $emailList[] = "[email protected]";
}
if($Question3 == "Yes") {
    $emailList[] = "[email protected]";
}
if($Question4 == "Yes") {
    $emailList[] = "[email protected]";
    $emailList[] = "[email protected]";
}
$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';
}

Upvotes: 1

Progrock
Progrock

Reputation: 7485

Much like Jeremy's answer, but with the addition of a form and answer filter.

<?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' => ['[email protected]'],
    'Q2' => ['[email protected]', '[email protected]'],
    'Q3' => ['[email protected]', '[email protected]', '[email protected]'],
];

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>

Upvotes: 0

Jeremy Harris
Jeremy Harris

Reputation: 24579

Here is another approach:

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

// Define subscribers (this could be database driven)
$subscribers = [
    'Q1' => ['[email protected]'],
    'Q2' => ['[email protected]', '[email protected]'],
    'Q3' => ['[email protected]', '[email protected]', '[email protected]', '[email protected]'],
    'Q4' => ['[email protected]', '[email protected]']
];

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

Upvotes: 1

Felippe Duarte
Felippe Duarte

Reputation: 15131

I personally don't like this switch(true) approach. The Clean Code (nice book by the way) always suggest us to use readable code, so you can translate like this:

switch (true) {
    case ($Question1 === 'Yes' and $Question2 === 'Yes' and $Question3 === 'Yes' and $Question4 === 'Yes'):
    sendmail('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]@goodwin.edu', '[email protected]', '[email protected]');
    break;
...

To this:

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

This is one approach. As I can't see your entire project, isn't easy to say which solution is better.

Upvotes: 0

Related Questions