我是 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> </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" /> </div> </td>
<td> </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;" />
</div></td>
<td> </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" /> </div> </td>
<td> </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;" />
</div></td>
<td> </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" />
</div></td>
<td> </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>
</div></td>
<td> </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> </div> </td>
<td> </td>
</tr>
<tr>
<td align="left" valign="top" style="padding-left:142px; padding-top:20px;">
<div class="contact">
<label> </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> </td>
</tr>
</table>
</form>