使用phpmailer和更高版本提交表单时无法获取数据

时间:2019-05-27 11:35:51

标签: forms gmail phpmailer

简单的PHP脚本可捕获用户表单输入并向我发送电子邮件。

使用phpmailer脚本提交空表单,即接收没有用户填写数据的电子邮件。没有SMTP错误,页面被提交并重定向到新页面。要求请帮助。有时收到的电子邮件还包含用户填写的数据表,不确定为什么吗?

<?php

date_default_timezone_set('Etc/UTC');
 use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
require './PHPMailer/src/Exception.php';



try {
$mail = new PHPMailer(true);

    $mail->SMTPDebug = 0;
     $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';  
    $mail->SMTPAuth = true;  
    $mail->Username = "XXXXX@gmail.com";  
    $mail->Password = "XXXXX";  
    $mail->SMTPSecure = 'tls';  
    $mail->Port = 587;  



    $mail->setFrom('XXXXX@gmail.com', 'Mr Singh');
    $mail->addReplyTo('XXXXX@gmail.com', 'Mr Singh');
    $mail->addAddress('XXXXX@gmail.com', 'Mr Singh');



    $mail->Subject = 'PHPMailer Exceptions test';
$mail->isHTML(false);


        $mail->Body = <<<EOT
Email: {$_POST['name']}
Email: {$_POST['email']}
Mobile: {$_POST['mobile']}
EOT;

    $mail->send();
    header("Refresh:1; contact.html");
} catch (Exception $e) {
    echo $e->errorMessage();  
} catch (\Exception $e) { //The leading slash means the Global PHP Exception class will be caught
    echo $e->getMessage();  
}

?>

2 个答案:

答案 0 :(得分:0)

如果此脚本完全运行过 ,则无论是否提交任何数据,它都会发送一封电子邮件。将其包装为仅在提交数据后才发送消息的条件,例如:

if (isset(_POST['email'])) {
   try {
        $mail = new PHPMailer(true);
...

您可以对此检查进行更彻底的了解,例如,验证是否提交了有效的电子邮件地址,其他字段包含您期望的内容,依此类推,但是您应该了解一般的想法。

答案 1 :(得分:0)

谢谢,只有在声明和它现在可以工作的情况下才能保留。能够使用使用Gmail SMTP服务。对于Gmail,必须在Gmail中启用安全性较低的应用程序,并更改一次密码。

<?php

date_default_timezone_set('Etc/UTC');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
require './PHPMailer/src/Exception.php';


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

    $mail = new PHPMailer(true);

    $mail->SMTPDebug = 0;

    //Set the hostname of the mail server
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
    $mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
    $mail->Username = "XXXXX@gmail.com"; // Your Gmail address.
    $mail->Password = "XXXXX"; // Your Gmail login password or App Specific Password.
    $mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
    $mail->Port = 587; // Which port to use, 587 is the default port for TLS security.


    //Set who the message is to be sent from
    $mail->setFrom('XXXXXgmail.com', 'Mr Singh');
    //Set an alternative reply-to address
    $mail->addReplyTo('XXXXX@gmail.com', 'Mr Singh');
    //Set who the message is to be sent to
    $mail->addAddress('YYYYY@gmail.com', 'Mr Singh');


    $mail->Subject = 'Customer Enquiry Form';


    $mail->isHTML(false);


        $mail->Body = <<<EOT
        Email: {$_POST['name']}
        Email: {$_POST['email']}
        Mobile: {$_POST['mobile']}

EOT;

        $mail->send();
        header("Refresh:1; contact.html");


}  else {
    echo "Your form is not submitted yet please call for booking";
}
?>

HTML代码

<form class="form-horizontal" id="submit" action="PHPMailer.php" method="post">
.......    ** HTML CODE of Form  .......
<input value="submit" name="submit" type="submit">