一个PHP表单,根据选择的选项向不同的收件人发送电子邮件


A PHP form that sends emails to different recipients based on options selected

我是php的新手。我正在开发一个简单的联系我们表格。在这个表格中,我想根据选择的选项向不同的收件人发送电子邮件。如果用户选择了职业选项,那么邮件应该发送到career@xyz.com&对于除职业以外的其他人,应将邮件发送到CCD_ 2。但我只收到一封电子邮件,即使用户选择了不同的选项。请帮忙。

以下是PHP代码

<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];
$interested = $_POST['interested'];
$message = $_POST['message'];
$formcontent="From: $name 'n Company Name: $company 'n Phone: $phone 'n Country Name: $country 'n Interested: $interested 'n Message: $message";
function emailswitch( $key ) {
    $to = array(
        'Career' => 'career@xyz.com'
    );
    $default = 'other@xyz.com';
    return (!empty($to[$key]))?$to[$key]:$default;
}
$to = emailswitch( $subject );
$subject = "Enquiry from Website";
$mailheader = "From: $email 'r'n";
mail($to, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! <br /> We will get in touch with you as soon as possible.";
?>

HTML是

<form name="frm" id="frm" method="POST" action="mail01.php">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="contact-table">
  <tr>
    <td class="contact-heading">Send a <span style="color:#171717;">Message</span></td>
    <td>&nbsp;</td>
    <td width="43%" rowspan="9" align="center" valign="top" style="padding-top:30px;"><img src="images/contact-us-new01.jpg" alt="Company Profile"></td>
    </tr>
  <tr>
    <td width="43%" align="left" valign="middle">
    <div class="contact"><label>Name</label>
    <input type="text" name="name" id="name" style="width:200px; float:right;" class="validate[required,custom[alphaspace]] for_obj" />&nbsp;   </div>    </td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td width="43%" align="left" valign="middle"><div class="contact margin_1line">
        <label>Company Name</label>
        <input type="text" name="company" id="company" style="width:200px; float:right;" />
      &nbsp; </div></td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td align="left" valign="middle">
    <div class="contact margin_1line"><label>Email</label>
    <input type="text" name="email" id="email" style="width:200px; float:right;" class="validate[required,custom[email]] for_obj" />&nbsp;  </div>    </td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td align="left" valign="middle"><div class="contact margin_1line">
        <label>Phone</label>
        <input type="text" name="phone" id="phone" style="width:200px; float:right;" />
      &nbsp; </div></td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td align="left" valign="middle">
    <div class="contact margin_1line">
      <label>Country Name</label>
        <input type="text" name="country" id="country" style="width:200px; float:right;" class="validate[required,custom[alphaspace]] for_obj" />
      &nbsp; </div></td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td align="left" valign="middle"><div class="contact margin_1line">
        <label>Interested</label>
        <select type="text" name="interested" id="interested" style="width:210px; height:28px; border:#dddddd 1px solid; float:right;" class="validate[required,custom[alphaspace]] for_obj">
          <option value="Others">Others</option>
          <option value="Product">Product</option>
          <option value="Career">Career</option>
          <option value="Information">Information</option>
        </select>
      &nbsp; </div></td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td>
    <div class="contact margin_1line"><label for="message">Message</label>
    <textarea name="message" id="message" class="validate[required] for_obj" rows="8" cols="10" style="width:200px; float:right;"></textarea>&nbsp; </div>    </td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td align="left" valign="top" style="padding-left:142px; padding-top:20px;">
    <div class="contact">
    <label>&nbsp;</label>
    <input type="submit" value="Send" class="butt custom_font" />
    <input type="reset" value="reset" class="butt custom_font" style=" float:right;" />
    </div></td>
    <td>&nbsp;</td>
    </tr>
</table>
</form>

我认为这里不需要函数。这增加了不必要的复杂性。我只需要使用if语句来检查所选的表单输入,并相应地设置$to变量。

if($interested == 'Career') {
    $to = 'career@xyz.com';
}
else {
    $to = 'other@xyz.com';
}

$to变量将根据表单选项进行设置,稍后可以在mail()函数中使用。

完整代码:

<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];
$interested = $_POST['interested'];
$message = $_POST['message'];
$formcontent="From: $name 'n Company Name: $company 'nPhone: 
$phone 'n Country Name: $country 'nInterested: $interested 'n Message: $message";
if($interested == 'Career') { //if career was selected
    $to = 'career@xyz.com';
}
else { //other options
    $to = 'other@xyz.com'; 
}
$subject = "Enquiry from Website";
$mailheader = "From: $email 'r'n";
mail($to, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! <br /> We will get in touch with you as soon as possible.";
?>

希望这能有所帮助!