我已经设置了一个 PHP 电子邮件表单,一切正常。但是,我不确定如何将不同的主题选择发送到不同的电子邮件地址。谁能帮帮我?谢谢。
HTML:
<label><strong>Subject:</strong></label>
<select name="subject" size="1">
<option value="General Feedback">General Feedback</option>
<option value="Book Information">Book Information</option>
<option value="Business Inquiries">Business Inquiries</option>
<option value="Website Related">Website Related</option>
</select>
PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "The Official Website of Ricky Tsang <ricky@rickytsang.ca>";
$email_subject = $_REQUEST['subject'];
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['full_name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$full_name = $_POST['full_name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message= $_POST['message'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The e-mail you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$full_name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered doee not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Full Name: ".clean_string($full_name)."\n";
$email_message .= "E-mail: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_from = $full_name.'<'.$email_from.'>';
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting The Official Website of Ricky Tsang. We will be in touch with you very soon.
<?php
}
?>
我可以这样建议吗:
HTML 片段
如果需要,您可以使用下面数组中的选项构造select
。
<label>
<strong>Subject:</strong>
</label>
<select name="subject" size="1">
<option value="1">General Feedback</option>
<option value="2">Book Information</option>
<option value="3">Business Inquiries</option>
<option value="4">Website Related</option>
</select>
PHP 代码段
<?php
$subjects = array(
1 => array(
'to' => 'user@example.org',
'subject' => 'General Feedback'
),
2 => array(
'to' => 'user@example.net',
'subject' => 'Book Information'
),
3 => array(
'to' => 'user@example.com',
'subject' => 'Business Inquiries'
),
4 => array(
'to' => 'anotheruser@example.org',
'subject' => 'Website Related'
)
);
$email_to = ! empty($subjects[$_REQUEST['subject']]['to']) ? $subjects[$_REQUEST['subject']]['to'] : 'default@example.com';
$email_subject = ! empty($subjects[$_REQUEST['subject']]['subject']) ? $subjects[$_REQUEST['subject']]['subject'] : 'Unknown subject';
我是一名优秀的程序员,十分优秀!