-2

我是 php 新手。我正在开发一个简单的联系我们表格。我想根据选择的选项向不同的收件人发送电子邮件,即。如果用户选择职业选项,则应将邮件发送至career@xyz.com& ,而职业邮件应发送至other@xyz.com。但是即使用户选择不同的选项,我也只会收到一封电子邮件。请帮忙。

下面是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>
4

1 回答 1

2

我认为这里不需要函数。它增加了不必要的复杂性,IMO。我只需使用一条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.";

?>

希望这可以帮助!

于 2013-08-26T12:14:11.093 回答