【问题标题】:PHPmailer: Failed to load resource: the server responded with a status of 500 (Internal Server Error)PHPmailer:加载资源失败:服务器响应状态为 500(内部服务器错误)
【发布时间】:2021-07-27 05:36:34
【问题描述】:

我正在尝试使用 phpmailer 发送表单数据。我被它困住了。 由于某种原因,我得到加载资源失败:服务器响应状态为 500(内部服务器错误)

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

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

需要帮助

提前致谢。 代码:

这是 index.php sn-p 代码

$('#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 的 sn-p 代码

<?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}";
}



?>

【问题讨论】:

    标签: php ajax email phpmailer


    【解决方案1】:

    500 错误表示该文件存在服务器错误。

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

     $_POST('name')
    

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

    【讨论】:

    • 是的,我运行重新发送的 php。当我在 sendit.php 页面中对数据进行硬编码时,它就可以工作了。并在我的收件箱中接收电子邮件。
    • 对不起。我没有注意到 - 你确实有一个错误。固定答案
    • 休息后,我打开stackoverflow寻求反馈,我读到了错字。该死的,就是这样。我一直在努力寻找解决方案,我没有注意到错字。谢谢大家
    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 2018-01-22
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多