You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP根据HTML表单部门选择发送邮件逻辑问题求助

问题:表单部门选择不生效,所有回电请求邮件均发送至sales@domain.co.uk

我有一个带部门选择的回电请求HTML表单,希望通过PHP根据用户选择的部门将请求发送到对应邮箱,但当前无论选哪个部门,邮件都会发送至sales@domain.co.uk,怀疑PHP的if语句存在问题。以下是我的代码:

HTML表单代码

<form action="js/call.php" method="post"  class="contactForm">
                    
<div class="row">
<div class="form-group col-6">
<input type="text" class="form-control" id="name" name="name" required maxlength="50" placeholder="Name" >
</div>

<div class="form-group col-6">
<input type="text" class="form-control" id="phone" name="phone" required maxlength="50" placeholder="Contact Number">
</div>
</div>

<div class="form-group col-12">
<select id="department-chosen"  class="form-control" name="department-chosen" required>
<option hidden value="">Please select a department</option>
   <option value="sales">Bike Sales</option>
  <option value="service">Workshop</option>
  <option value="parts">Parts</option>
  <option value="clothing">Clothing / Accessories</option>
</select></div>

<div><input type="text" name="reference" id="reference"   tabindex="-1" autocomplete="off" > </div>

<!-- START CAPTCHA -->
<br>

<div class="float-end"><button type="submit" class="btn btn-af1" name="text" value="Send">
          <i class="pe-1 fa fa-paper-plane"></i>Send</button>
</div> 
 </form>    

PHP代码

<?php 
// if the reference field is not empty
if(!empty($_POST['reference'])) die();
// if the test field is empty
if(isset($_POST['reference']) && $_POST['test'] == ''){

if($_POST['department-chosen']  == 'sales') $to = "sales@domain.co.uk";
elseif($_POST['department-chosen']  == 'service') $to = "service@domain.co.uk";
elseif($_POST['department-chosen']  == 'parts') $to = "parts@domain.co.uk";
else $to = "clothing@domain.co.uk";

             
 $from = $_REQUEST['phone'] ; 
 $name = $_REQUEST['name'] ;
 $subject = "Call back requested from website"; 
 
 $fields = array(); 
 $fields["name"] = "Name";
 $fields["phone"] = "Phone"; 
 $fields["department-chosen"] = "Department"; 
 $fields["reference"] = "reference";
 

 $body = "We have received the following 'call back' request:

"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s
",$b,$_REQUEST[$a]); } 
 

 
 $send = mail($to, $subject, $body); 
 if($send) 
 {header( "Location: https://www.domain.co.uk/thankyou.htm" );} 
 




 else 
 {print "We encountered an error sending your mail, please notify service@domain.co.uk"; } 
 }
 ?> 

问题分析与修复方案

核心问题

  1. 无效的条件判断:PHP代码中$_POST['test']对应的字段在表单中不存在(表单里只有名为text的提交按钮),这会导致条件isset($_POST['reference']) && $_POST['test'] == ''在部分PHP环境中无法正常触发,或因未定义变量产生警告,进而导致后续部门判断代码未执行。若之前代码中$to默认值为sales@domain.co.uk,就会出现所有邮件都发往该地址的情况。
  2. $from字段不合法:邮件的From地址设置为用户输入的电话号码,不符合SMTP规范,可能导致邮件被拒收或进入垃圾邮箱。

修复后的PHP代码

<?php
// 反垃圾邮件:如果机器人填充了隐藏的reference字段,直接终止脚本
if (!empty($_POST['reference'])) {
    die();
}

// 确保表单已提交且department-chosen字段存在
if (isset($_POST['department-chosen'])) {
    // 用数组映射部门和邮箱,更清晰易维护
    $departmentEmails = [
        'sales' => 'sales@domain.co.uk',
        'service' => 'service@domain.co.uk',
        'parts' => 'parts@domain.co.uk',
        'clothing' => 'clothing@domain.co.uk'
    ];

    // 获取目标邮箱,若部门值不在映射中,默认使用sales邮箱(可按需调整)
    $to = isset($departmentEmails[$_POST['department-chosen']]) 
        ? $departmentEmails[$_POST['department-chosen']] 
        : 'sales@domain.co.uk';

    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $department = $_POST['department-chosen'];
    $subject = "Call back requested from website";

    // 构建邮件内容,移除无用的reference字段
    $body = "We have received the following 'call back' request:\n\n";
    $body .= sprintf("%20s: %s\n", "Name", $name);
    $body .= sprintf("%20s: %s\n", "Phone", $phone);
    $body .= sprintf("%20s: %s\n", "Department", $department);

    // 设置合法的From地址,使用网站官方邮箱
    $headers = "From: Website Call Back <no-reply@domain.co.uk>\r\n";
    $headers .= "Reply-To: $name <$phone>\r\n"; // 可选:设置回复到用户电话(部分邮箱支持)

    // 发送邮件
    $send = mail($to, $subject, $body, $headers);

    if ($send) {
        header("Location: https://www.domain.co.uk/thankyou.htm");
        exit(); // 跳转后终止脚本,避免后续代码执行
    } else {
        print "We encountered an error sending your mail, please notify service@domain.co.uk";
    }
} else {
    // 若department-chosen字段未提交,提示错误
    print "Please select a valid department.";
}
?>

修复说明

  1. 移除无效的$_POST['test']判断:只保留针对department-chosen字段的存在性检查,确保部门判断代码能正常执行。
  2. 使用数组映射部门与邮箱:相比if-else链,数组映射更简洁,后续新增部门只需修改数组即可,降低出错概率。
  3. 修正From字段:使用合法的网站邮箱作为发件地址,符合SMTP规范,提升邮件送达率。
  4. 优化邮件内容:移除无用的reference字段,让邮件内容更清晰。
  5. 添加exit()跳转后终止脚本:避免header跳转后后续代码继续执行。

内容的提问来源于stack exchange,提问作者Jan Cooper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 18:35:07