你如何使用gmail验证PHP联系表格? [英] How do you use authentication with gmail for a PHP contact form?

查看:156
本文介绍了你如何使用gmail验证PHP联系表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我工作的代码;然而,我想合并一种方法来使用我的gmail帐户进行smtp认证,但我无法弄清楚...帮助吗?

This is the code that i have working; however i want to incorporate a way to have an smtp authentication using my gmail account but I can't figure it out...help?

<?php

if(isset($_POST['email'])) {

    $email_to = "jfk003@lvc.edu";
    $email_subject = "Website Inquire";


    function died($error) {
        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['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";
  if (preg_match($email_exp, $email_from)) {
    echo "Email address is valid.";
  }
  else 

    {
        echo "Email address is <u>not</u> valid.";
  }
    $string_exp = "/^[a-zA-Z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do 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 .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";



$headers = 'From: '.$email_from."\n".
'Reply-To: '.$email_from."\n".
'X-Mailer: PHP/'.phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

} 

?>


推荐答案

This is a complete working example.    
<?php
    // this is for troubleshooting errors 
    // set the 1 to 0 to turn off (must set to 0 when in production mode or when you run this live 
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
?><?php
    if(isset($_POST['email'])) {
        $email_to       = $_POST['email'];
        $email_subject  = "E-mail from website visitor.";

        // try adding a useful/relevent message to errors output to users
        function died($error) {
            echo "We are very sorry, but there were error(s) found with the form you submitted:\n\t ";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }

        // validation expected data exists
        // since all post are submit, check if they are empty or equal to an empty string
        if(isset($_POST['email'])){
            if($_POST['email'] == "" || $_POST['first_name'] == "" || $_POST['last_name'] == "" || $_POST['telephone'] == "" || $_POST['comments'] == ""){
                died('Please fill out the entire form');
            }
        }

        $first_name = $_POST['first_name']; // required
        $last_name  = $_POST['last_name']; // required
        $email_from = $_POST['email']; // required
        $telephone  = $_POST['telephone']; // not required
        $comments   = $_POST['comments']; // required

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
        $string_exp = "/^[A-Za-z .'-]+$/";

        if(!preg_match($email_exp,$email_from))
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        if(!preg_match($string_exp,$first_name))
            $error_message .= 'The First Name you entered does not appear to be valid.<br />';
        if(!preg_match($string_exp,$last_name))
            $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
        if(strlen($comments) < 2)
            $error_message .= 'The Message you entered do 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 .= "First Name: ".clean_string($first_name)."\n";
        $email_message .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Telephone: ".clean_string($telephone)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\n";

        // create email headers
        // changed up your email a bit
        $headers = "From: $email_from\n";
        $headers.= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        mail($email_to, $email_subject, $email_message, $headers);
        header('Location: http://www.cnn.com');
        exit();
    }
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    Firstname:<br>
    <input type="text" name="first_name"><br>
    Lastname:<br>
    <input type="text" name="last_name"><br>
    Email:<br>
    <input type="text" name="email"><br>
    Phone Number:<br>
    <input type="text" name="telephone"><br>
    Comments:<br>
    <input type="textarea" name="comments"><br><br>
    <input type="submit" value="Submit">
</form>

这篇关于你如何使用gmail验证PHP联系表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆