php - 电子邮件联系表单中的 PHP 开关未发送到正确的地址
问题描述
我正在创建一个 HTML 联系表单,该表单使用 php 根据用户所做的选择向 6 个不同的人发送电子邮件。我发送电子邮件的代码,但它只发送到默认值。我不确定需要改变什么才能使开关正常工作。sendTo 正在从 HTML 页面中提取。我把代码放在下面。
// Check if the form has been posted
if (isset($_POST['submit'])) {
// The email address the email will be sent to
switch ($_POST['$sendTo'])
{
case "membership":
$sendTo = "email1";
break;
case "performance":
$sendTo = "email2";
break;
case "quartets":
$sendTo = "email3";
break;
case "president":
$sendTo = "email4";
break;
case "regional":
$sendTo = "email5";
break;
case "website":
$sendTo = "email6";
break;
default:
$sendTo = "email6";
}
// The email subject
$subject = "Contact Form Submission";
// Set the from and reply-to address for the email
$headers = 'From: Organization <webmaster@organization.com>' . "\r\n" .
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Build the body of the email
$mailbody = "You have received an email from the POB website contact form.\n\n"
. "Name: " . $_POST['name'] . "\n"
. "Email: " . $_POST['email'] . "\n"
. "Heard From: " . $_POST['howhear'] . "\n"
. "Message:\n" . $_POST['message'];
// Send the email
mail($sendTo, $subject, $mailbody, $headers);
// Go to the thank you page
header("location: thankyou.shtml");
exit;
}
<p><strong>Who do you want to contact?</strong><br/>
<select name="sendTo">
<option value="none">Select a Contact</option>
<option value="performance">Performance Manager</option>
<option value="membership">Membership Chair</option>
<option value="president">President</option>
<option value="quartets">Quartet Promotion Chair</option>
<option value="regional">Regional Events Coordinator</option>
<option value="website">Website</option>
</select>
</p>
如果用户在 HTML 页面上选择会员主席,它应该发送到会员电子邮件。其他情况也一样。
解决方案
您必须使用表单提交有关类型(成员资格、性能等)的信息,并在 switch 案例中使用此值。假设该变量简称为“sendTo”。(编辑变量名)
switch ($_POST["sendTo"])
{
case "membership":
$sendTo = "email1";
break;
case "performance":
$sendTo = "email2";
break;
case "quartets":
$sendTo = "email3";
break;
case "president":
$sendTo = "email4";
break;
case "regional":
$sendTo = "email5";
break;
case "website":
$sendTo = "email6";
break;
default:
$sendTo = "email6";
}
推荐阅读
- css - React Framer Motion 在颜色过渡之间
- c++ - 无法在 CMake 中链接静态库
- html - 显示切片之间的间隙和空白的 Outlook html 签名
- c++ - 头文件中定义继承类构造函数语法错误的类构造函数
- node.js - res.send(200) 是否与 res.status(200).send('OK') 相同,是否与 res.sendStatus(200) 相同;
- javascript - 在提交按钮后刷新dom后如何获取元素内部文本?
- flutter - 参数更改时如何刷新当前路由?
- linux - 使用带有自定义事件的`perf stat`时如何保留默认事件
- python - python exe的MAP文件
- docker - L 标志在 yarn dev 命令中的作用