添加电子邮件验证到现有的php文件

时间:2015-02-13 19:10:26

标签: php validation email

如何为现有的php文件添加电子邮件验证?

PHP文件:

    <?php
if (count($_POST)) {

    $email   = 'receivermail';
    $subject = 'our subject';
    $message = 'messageOK';
    $error   = 'messageNOTOK';
    $charset = 'utf-8';
    $head    = "MIME-Version: 1.0\r\n" . "Content-Type: text/plain; charset=$charset\r\n" . "Content-Transfer-Encoding: 8bit";
    $body    = '';
    foreach ($_POST as $name => $value) {
        if (is_array($value)) {
            for ($i = 0; $i < count($value); $i++) {
                $body .= "$name=" . (get_magic_quotes_gpc() ? stripslashes($value[$i]) : $value[$i]) . "\r\n";
            }
        } else
            $body .= "$name=" . (get_magic_quotes_gpc() ? stripslashes($value) : $value) . "\r\n";
    }
    echo mail($email, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head) ? $message : $error;
}
?>

另一个html文件中的表单:

<form method="post" action="name_of_the.php" name="ourform" >

1 个答案:

答案 0 :(得分:0)

查看filter_var函数

http://php.net/manual/en/function.filter-var.php

filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)