【问题标题】:How do you use authentication with gmail for a PHP contact form?您如何使用 gmail 的身份验证来获取 PHP 联系表单?
【发布时间】:2011-08-01 03:11:53
【问题描述】:

这是我正在工作的代码;但是我想结合一种方法来使用我的 gmail 帐户进行 smtp 身份验证,但我无法弄清楚...帮助?

<?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);

} 

?>

【问题讨论】:

  • 我正要说我认为这段代码包含了曾经设想过的每一个 PHP 电子邮件反模式,但是你缺少标头注入。该死。这也是a duplicate of at least a few of the 200 questions 通过 Gmail 的 SMTP 服务器从 PHP 发送邮件。

标签: php forms smtp gmail smtp-auth


【解决方案1】:
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>

【讨论】:

  • 您可以更改标题以将用户重新定位到您的登录脚本或主页而不是 cnn。
【解决方案2】:

我认为使用 gmail;谷歌更改了身份验证方法,我能够首先按照以下步骤成功进行身份验证: Video link 这是允许外部应用程序的两步过程

您需要生成应用程序密码。

然后为您的 PHP 应用程序使用该密码。

【讨论】:

    【解决方案3】:

    您可以使用 PHPMailer http://sourceforge.net/projects/phpmailer/,这是您使用 GMail 发送电子邮件的方法,但 HTML 和 CSS 在这里对您没有多大帮助。

    function email($to, $subject, $body){
        require_once("class.phpmailer.php");
    
        $mail = new PHPMailer();
    
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465;
        $mail->Username = "name@example";
        $mail->Password = "123456";
    
        $mail->SetFrom("name@example.com", "Name Example");
    
        $mail->Subject = $subject;
        $mail->Body = $body;
    
        $mail->AddAddress($to);
        $mail->Send();
        unset($mail);
    }
    

    【讨论】:

      【解决方案4】:

      您想使用支持 SMTP 身份验证的现代 PHP 邮件库,例如 SwiftMailer

      sending mail to a SSL/TLS SMTP serversending mail with a username/password combo 上查看Google's instructions on which servers to use 和SwiftMailer 的文档。

      在更新您的代码以使用 SwiftMailer 时,您应该放弃电子邮件验证正则表达式的可恶,它排除了许多完全有效的本地部分和域。 building the message using method calls 强加的理智直外套将解决您代码中的大多数其他潜在问题,例如奇怪的 clean_string 函数——您不需要它(我不确定您是什么认为它在这里,但它并没有真的,呃,使字符串更安全)。

      【讨论】:

      • 我不明白我应该用这个 swiftmailer 的东西做什么。我下载了,但我不太了解 php 或与之相关的任何内容。我只知道css和html。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      相关资源
      最近更新 更多