【发布时间】: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个条件。构建阵列后发送电子邮件。