PHPmailer:无法加载资源:服务器响应状态为 500(内部服务器错误)

PHPmailer: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

我正在尝试使用 phpmailer 通过电子邮件发送表单数据。我被它困住了。 出于某种原因,我收到 无法加载资源:服务器响应状态为 500(内部服务器错误)

我有 2 页。 index.php 和 sendit.php。 index.php 有一个 bootstrap 模态框(要发送到用户电子邮件的表单数据)。 用户填写表格,点击保存。数据将存入数据库,并调用sendMail函数进行发送操作。 sendMail 函数对 sendit.php 进行 ajax 调用并发送数据

当我在 sendit.php 中对数据数组中的数据进行硬编码时,它将起作用。当我将其更改为 $_POST['name'] 时,不起作用。

需要帮助

提前致谢。 代码:

这是 index.php 片段代码

$('#btnSave').on('click', function() {
      // some code
      // the ajax call
      $.ajax({
          type: "POST",
          url: 'addEvent.php',
          data: thedata,
          success: function(d) {
            if (d == 'sucess') {
            
              // data to send to client email
              data = {
                "name": $('#inpName').val(),
              }
              sendMail(data); // call the function sendMail.
              resetForm(); // reset the form
            } else if (d === 'false') {
              //show some message
            },
            error: function(error) {
              alert(error);
            }
          });
      }
      
 // the sendMail function
  function sendMail(theData)
        {
          $.ajax({
            type: "POST",
            url: "sendit.php",
            data: theData
          })
        }

这是 sendit.php

的片段代码

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require '../sendmail/mailer/autoload.php';

//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'myusername@gmail.com';                     //SMTP username
    $mail->Password   = 'thepassword';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('client email adres', 'client name');
    
   
    $data=[
    lastName=>$_POST('name'),
    ];

// the message body
$body='
<!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Afspraak kaart</title>
        </head>
        <body>
            <div class="afspraak-container">
                <h2 class="afspraal-head">
                    Afspraak kaart
                </h2>
                <hr>
                <div class="afspraak-body">
                    <p>Mr/Mevr '.$data[lastName].'</p>

                    <p>Bedankt voor uw afspraak bij het Kadaster en Openbare Registers.
                    </p>
                    <p>
                        Uw afspraak gegevens:
                    </p>
                    <div class="afspraak-card">
                        <div class="afspraak-card-head">
                            24 mei 2021,10:20 AM
                        </div>
                        <ul>
                            <li>Afspraak volgnr: 101</li>
                            <li>Naam: Joel Goncalves de Freitas</li>
                            <li>Product: Meetbrief</li>
                        </ul>
                    </div>
                </div>
                <footer>

                </footer>
            </div>
        </body>
    </html>';

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'test';
    $mail->Body    = $body;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}



?>

500 错误表示该文件出现服务器错误。

PHP 代码在形式上是正确的 - 至少如果你 运行 足够新 PHP - 但转念一想,这是错误的:

 $_POST('name')

代码将尝试调用 $_POST 函数,然后崩溃。应该是 $_POST['name'].