表单提交在URL中显示带有php文件的空白页面

时间:2017-08-22 23:44:52

标签: php phpmailer

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

它正在使用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>

这是&#34;即将到来的form.php&#34;中的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);

&GT;

我也通过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

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

这是使用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-&gt; From和$ mail-&gt; To方法。