提问者:小点点

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


我正在尝试用phpmailer通过电子邮件发送表单数据。我受够了。由于某些原因,我无法加载资源:服务器响应状态为500(内部服务器错误)。

我有两页。索引。php和sendit。php。索引。php有一个引导模式框(发送给用户电子邮件的表单数据)。用户填写表单后,单击保存。数据将保存到数据库中,并调用sendMail函数执行发送操作。sendMail函数对sendit进行ajax调用。php并发送数据

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

需要帮助吗

提前谢谢。守则:

这是索引。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}";
}



?>

共1个答案

匿名用户

500错误意味着您在该文件上有一个服务器错误。

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

 $_POST('name')

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