我有一个PHP代码来检索访问者提交的信息。
如何在访问者离开每个空字段时收到的电子邮件中添加“无响应”等短语?
提前感谢您的帮助!
我想收到这样的邮件:
名字:大卫
名称:测试
Téléphone:没有回复
消息:测试消息
<?php
if(isset($_POST) && isset($_POST['form3_firstname']) && isset($_POST['form3_name']) && isset($_POST['form3_email']) && isset($_POST['form3_telephone']) && isset($_POST['form3_message'])) {
extract($_POST);
if(!empty($form3_firstname) && !empty($form3_name) && !empty($form3_email) && !empty($form3_message)) {
$to = 'XXXXXX@gmail.com'; // My real email
$subject = 'Contact from the site';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
$headers .= 'Reply-To:'.$form3_email. "\r\n";
$message = '<html><body>';
$message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$message .= '<table>';
$message .= '<tr><td colspan="2"><p>MESSAGE</p></td></tr>';
$message .= '<tr><td>Firstname :</td><td>'.$form3_firstname.'</td></tr>';
$message .= '<tr><td>Name :</td><td>'.$form3_name.'</td></tr>';
$message .= '<tr><td>Email :</td><td>'.$form3_email.'</td></tr>';
$message .= '<tr><td>Telephone :</td><td>'.$form3_telephone.'</td></tr>';
$message .= '<tr><td>Message :</td<td>'.stripslashes($form3_message).'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo "Form sent";
} else {
echo "Form not sent";
}
} else {
echo "You have not filled in the field";
}
}
?>
答案 0 :(得分:2)
你可以这样做。让我们假设只需要电子邮件和消息。
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// set required fields to NULL when empty, otherwise provide some default text
$fname = isset($_POST['form3_firstname']) && trim($_POST['form3_firstname']) != '' ? $_POST['form3_firstname'] : 'n/a';
$name = isset($_POST['form3_name']) && trim($_POST['form3_name']) != '' ? $_POST['form3_name'] : 'n/a';
$email = isset($_POST['form3_email']) && filter_var($_POST['form3_email'], FILTER_VALIDATE_EMAIL) ? $_POST['form3_email'] : null;
$phone = isset($_POST['form3_telephone']) && trim($_POST['form3_telephone']) != '' ? $_POST['form3_telephone'] : 'n/a';
$message = isset($_POST['form3_message']) && trim($_POST['form3_message']) != '' ? $_POST['form3_message'] : null;
// proceed if required fields are complete
if (isset($email, $message)) {
// ...
}
}