<?php
if (isset($_POST["sendMessage"])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['last-name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from= 'info@address.com';
$to = 'some@gmail.com';
$subject = 'Message from Contact Demo ';
$txt= 'demo';
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
$body = "From: First-Name: $firstName\n Last-Name: $lastName\n
E-Mail: $email\n Phone: $phone\n Message: $message";
$errFirstName='';$errLastName='';$errEmail='';$errPhone='';$errMessage='';
// If there are no errors, send the email
if (mail ($to, $subject,$txt,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
?>
我正在尝试进行邮件验证,但它无法正常工作。这里有一些文本会给出成功消息或错误信息。但它什么都没有显示。我无法理解我的错。请帮助别人。
答案 0 :(得分:1)
在您的实时服务器上试用此代码
$firstName = "sunny";
$lastName = "khatri";
$email = '******@gmail.com';
$phone = '********';
$message ='Hello this is test';
$from= '*****@gmail.com';
$to = '***@gmail.com';
$subject = 'Message from Contact Demo ';
$txt= 'demo';
$headers = "From: ******@*****.com" . "\r\n" .
"CC: ********@live.in";
$body = "From: First-Name: $firstName\n Last-Name: $lastName\n
E-Mail: $email\n Phone: $phone\n Message: $message";
$errFirstName='';$errLastName='';$errEmail='';$errPhone='';$errMessage='';
// If there are no errors, send the email
if (mail ($to, $subject,$txt,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
echo $result;
答案 1 :(得分:0)
只需使用PHP手册中的默认功能:
<?php
$email_a = 'joe@example.com';
$email_b = 'bogus';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_a) email address is considered valid.\n";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_b) email address is considered valid.\n";
} else {
echo "This ($email_b) email address is considered invalid.\n";
}
?>
答案 2 :(得分:0)
首先,您必须使用此Php功能检查电子邮件是否有效
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
然后如果它无效则给出错误 其他
if (mail ($to, $subject,$txt,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
试试..