用PHP验证电子邮件?

用PHP验证电子邮件?,php,Php,我正在尝试进行邮件验证,但它不起作用。这里有一些文本将给出一条成功消息或错误消息。但它没有显示任何内容。我不明白我的错。请帮助他人。只需使用PHP手册中的默认函数: <?php if (isset($_POST["sendMessage"])) { $firstName = $_POST['firstName']; $lastName = $_POST['last-name']; $email = $

我正在尝试进行邮件验证,但它不起作用。这里有一些文本将给出一条成功消息或错误消息。但它没有显示任何内容。我不明白我的错。请帮助他人。

只需使用PHP手册中的默认函数:

     <?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>';
        }
    }

?>

首先,您必须使用此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";
   }
?>
如果无效,则给出一个错误 否则

if(邮件($to、$subject、$txt、$headers)){
$result='谢谢!我会与您联系';
}否则{
$result='抱歉,发送邮件时出错。请稍后再试';
}

试试看..

在您的实时服务器上试试这段代码

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>';
        }
$firstName=“sunny”;
$lastName=“khatri”;
$email='******@gmail.com';
$phone='********';
$message='您好,这是测试';
$from='******@gmail.com';
$to='***@gmail.com';
$subject='联系人演示信息';
$txt='demo';
$headers=“From:******@********.com”。“\r\n”。
“抄送:******@live.in”;
$body=“From:First Name:$firstName\n Last Name:$lastName\n
电子邮件:$email\n Phone:$Phone\n Message:$Message”;
$errFirstName=''$errLastName=''$errEmail=''$errPhone=''$errMessage='';
//如果没有错误,请发送电子邮件
if(邮件($to、$subject、$txt、$headers)){
$result='谢谢!我会与您联系';
}否则{
$result='抱歉,发送邮件时出错。请稍后再试';
}
回声$结果;

我在这里没有看到任何邮件验证代码?如果有,您可以指定结果或错误吗?您是德国人吗?你应该用谷歌搜索“will”这个词在英语中的意思。除此之外,尽量保持变量名的恒定性<代码>姓氏和
姓氏
——这是一种糟糕的做法。此外,可能会编辑您的问题,删除您的电子邮件地址,并替换为一些随机的东西。。。不要认为把你的电子邮件地址张贴在公共公告板上是个好主意。另外,通过“$errFirstName=”,您想要实现什么$errLastName=“”;`那么..@AlivetoDie认为他指的是最后的
if-else
条件-如果消息已发送,则验证…@Twinfriends谢谢你的建议。
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>';
        }
    $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;