【问题标题】:Form Submit displaying blank page with php file in the URL表单提交在 URL 中显示带有 php 文件的空白页面
【发布时间】:2017-08-22 23:44:52
【问题描述】:

我似乎无法在网上找到解决方案或答案。

它使用 Bootstrap 和 PHPmailer。

以下是我的表单 HTML:

<div class="row">
          <div class="col-md-8 col-md-offset-2">
            <div class="row">
              <form name="coming-soon-form" role="form" autocomplete="off" class="m-t-15" id="coming-soon-form" action="_lib/coming-soon-form.php" method="post">
              <div class="row">
                <div class="col-sm-6">
                  <div class="form-group form-group-default required">
                    <label class="control-label">First name</label>
                    <input type="text" id="name" name="name" class="form-control" required>
                  </div>
                </div>
                <div class="col-sm-6">
                  <div class="form-group form-group-default">
                    <label class="control-label">Last name</label>
                    <input type="text" id="last-name" name="last-name" class="form-control" required>
                  </div>
                </div>
              </div>
              <div class="form-group form-group-default">
                <label class="control-label">Email</label>
                <input type="email" id="email" name="email" placeholder="abc@123.com" class="form-control" required>
              </div>
              <div class="sm-p-t-10 clearfix">

                <input type="submit" class="btn btn-complete font-montserrat all-caps fs-12 pull-right xs-pull-left" value="Submit">
              </div>
              <div class="clearfix"></div>
            </form>
            </div>
          </div>
        </div>

这是“coming-soon-form.php”文件中的 php 脚本:

<?php 

require 'phpmailer/PHPMailerAutoload.php';

// CONFIG YOUR FIELDS
//============================================================
$name =     filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email =    filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);


// CONFIG YOUR EMAIL MESSAGE
//============================================================
$message = '<p>The following request was sent from: </p>';
$message .= '<p>Name: ' . $name . '</p>';
$message .= '<p>Email: ' . $email . '</p>';
$message .= '<p>Message: ' . $formMessage .'</p>';

// CONFIG YOUR MAIL SERVER
//============================================================
$mail = new PHPMailer;
$mail->isSMTP();                                    // Enable SMTP authentication
$mail->SMTPAuth = true;                             // Set mailer to use SMTP
//Sign up with MAIL GUN
$mail->Host = 'smtp.gmail.com';                // Specify main and backup server (this is a fake name for the use of this example)             

$mail->Username = 'email@gmail.com';                  // SMTP username
$mail->Password = 'password';                         // SMTP password
$mail->SMTPSecure = 'tls';                          // Enable encryption, 'ssl' also accepted                                   
$mail->Port = 587;                        

$mail->From = $email;
$mail->FromName = $name;
$mail->AddReplyTo($email,$name);
$mail->to('email@gmail.com');
$mail->addAddress('email@gmail.com');  // Add a recipient

$mail->WordWrap = 50;                               // Set word wrap to 50 characters
$mail->isHTML(true);                                // Set email format to HTML

$mail->Subject = 'Contact request';
$mail->Body    = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    $data['error']['title'] = 'Message could not be sent.';
    $data['error']['details'] = 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

$data['success']['title'] = 'Message has been sent';

echo json_encode($data);

?>

我还通过 jquery-validation 进行了验证,效果很好。

但是,一旦填写并提交了表单,它就无法正常执行、发送电子邮件或在页面上显示任何错误或成功消息。相反,它只是加载一个空白页面,URL 是 www.domainnamehere.com/_lib/coming-soon-form.php

我还使用验证器验证了所有 php。

是什么导致表单提交问题???

以下是错误日志信息:

[23-Aug-2017 03:25:02 UTC] PHP Warning: Version warning: Imagick was compiled against Image Magick version 1650 but version 1654 is loaded. Imagick will run but may behave surprisingly in Unknown on line 0 [23-Aug-2017 03:25:02 UTC] PHP Fatal error: Uncaught Error: Call to undefined method PHPMailer::to() in /home4/anabasi2/public_html/_lib/coming-soon-form.php:34 Stack trace: #0 {main} thrown in /home4/anabasi2/public_html/_lib/coming-soon-form.php on line 34

【问题讨论】:

  • 在 php 文件顶部添加error_reporting(E_ALL); ini_set('display_errors', '1');,然后让我们知道它的内容。
  • lib 的路径很可能是错误的。
  • 我已经通过将路径指向同一文件夹中的图像来检查路径并且它可以工作。
  • @LawrenceCherone 这里是错误日志 [23-Aug-2017 03:25:02 UTC] PHP 警告:版本警告:Imagick 是针对 Image Magick 版本 1650 编译的,但已加载版本 1654。 Imagick 将运行,但在第 0 行 [23-Aug-2017 03:25:02 UTC] 上的未知中可能会出现令人惊讶的行为 PHP 致命错误:未捕获的错误:调用 /home4/anabasi2/public_html/ 中未定义的方法 PHPMailer::to() _lib/coming-soon-form.php:34 堆栈跟踪:#0 {main} 在第 34 行的 /home4/anabasi2/public_html/_lib/coming-soon-form.php 中抛出
  • 好了,to() 不是有效方法,请使用 addAddress()

标签: php phpmailer


【解决方案1】:

以下行不是有效的 PHPMailer 方法:

$mail-&gt;to('email@gmail.com');

下面这行是添加收件人的正确方法。

$mail-&gt;addAddress('email@gmail.com');

【讨论】:

    【解决方案2】:

    这是使用 PHPMailer 发送电子邮件的正确方法:

    $mail = new PHPMailer;
    //$mail->SMTPDebug = 3;               // Enable verbose debug output
    $mail->isSMTP();                 // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  
    $mail->SMTPAuth = true;                              
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                           
    $mail->Port = 587;                                   
    
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');
    
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    $mail->isHTML(true);                                  
    
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    

    请考虑一下您对 $mail->From 和 $mail->To 方法的使用。

    【讨论】: