【问题标题】:WordPress 500 Error with wp_mailwp_mail 出现 500 错误
【发布时间】:2017-10-25 14:08:34
【问题描述】:

我试图将数据从 AngularJS 发送到 PHP 文件,并使用 PHP 发送带有 wp_mail 的电子邮件,但我收到 500 错误,我似乎无法弄清楚我哪里出错了。我似乎以正确的格式将数据发送到 PHP 文件,但我只是得到一个错误响应。

AngularJS 调用

$scope.sendEmail = function() {
            if ($scope.resultsTitle) {
                $scope.resultsTitle = $scope.resultsTitle;
            }
            else if ($scope.resultsTitle == "") {
                $scope.resultsTitle = "Survey Not Completed";
            }
              $scope.userData = {
                "userEmail": $scope.email,
                "todaysDate": $scope.date,
                "timeNow": $scope.time,
                "question1": $scope.question1Answer.toString(),
                "question2": $scope.question2Answer.toString(),
                "question3": $scope.question3Answer.toString(),
                "question4": $scope.question4Answer.toString(),
                "question5": $scope.question5Answer.toString(),
                "question6": $scope.question6Answer.toString(),
                "question7": $scope.question7Answer.toString(),
                "question8": $scope.question8Answer.toString(),
                "question9": $scope.question9Answer.toString(),
                "question10": $scope.question10Answer.toString(),
                "endResult": $scope.resultsTitle
              };
              $log.info($scope.userData);


                $http({
                      method: 'POST',
                      url: 'mailer.php',
                      data: $scope.userData
                  })
                  .then(function successCallback(response) {
                            $log.log("Sent to PHP file!");
                            $log.log(response);
                    }, function errorCallback(response) {
                            $log.log("Data not sent to the php file!")
                            $log.warn(response);
                    });
        };

PHP 邮件程序文件:

<?php
 $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $user_email = $request->userEmail;
    $todays_date = $request->todaysDate;
    $time = $request->timeNow;
    $question1 = $request->question1;
    $question2 = $request->question2;
    $question3 = $request->question3;
    $question4 = $request->question4;
    $question5 = $request->question5;
    $question6 = $request->question6;
    $question7 = $request->question7;
    $question8 = $request->question8;
    $question9 = $request->question9;
    $question10 = $request->question10;
    $endResult = $request->endResult;


$message = "
    <html>
        <head>
            <title>The Title</title>
        </head>
        <body>
            <p>Thank you.</p>
            <table>
                <tr>
                    <th>Your Email</th>
                    <th>Today's Date</th>
                    <th>The Current Time</th>
                    <th>Question 1</th>
                    <th>Question 2</th>
                    <th>Question 3</th>
                    <th>Question 4</th>
                    <th>Question 5</th>
                    <th>Question 6</th>
                    <th>Question 7</th>
                    <th>Question 8</th>
                    <th>Question 9</th>
                    <th>Question 10</th>
                    <th>End Decision</th>
                </tr>
                <tr>
                    <td>$user_email</td>
                    <td>$todays_date</td>
                    <td>$time</td>
                    <td>$question1</td>
                    <td>$question2</td>
                    <td>$question3</td>
                    <td>$question4</td>
                    <td>$question5</td>
                    <td>$question6</td>
                    <td>$question7</td>
                    <td>$question8</td>
                    <td>$question9</td>
                    <td>$question10</td>
                    <td>$endResult</td>
                </tr>
            </table>
        </body>
    </html>
";

    echo ($user_email, 'The Subject', $message);
    return wp_mail($user_email, 'The Subject', $message);

?>

【问题讨论】:

  • 请注意,我在本地版本中使用 MAMP,在 prod 版本中使用 Windows IIS7。
  • 您需要检查您的 error_log 文件。或启用 display_errors 以查看问题所在。
  • 检查问题的简单方法是切换代码段,我会从 $postdata 注释到 echo 的末尾,然后输入您的电子邮件和随机消息,看看是不是在职的。如果它发送电子邮件,则问题出在注释块中,如果没有,则在您尝试发送电子邮件时出现问题。也许一些错误日志可以像上面建议的那样为我们提供更多帮助!
  • 另外作为旁注,PHP 脚本不应以return 结尾。如果你真的想的话,最好的做法是回显。也许做一些类似$result = wp_mail($user_email...);的事情,然后在$result上做一个if/else,如果是真的你echo 'success';,当是假的echo 'something went wrong';或类似的东西! :D
  • 非常感谢你们!打开PHP错误,发现我的代码哪里出错了。

标签: php angularjs wordpress email


【解决方案1】:

全部,

解决方案最终在邮件中进行了调整:

Changed return wp_mail to wp_mail
Deleted echo wp_mail

Added $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' ); to the top of the file

【讨论】:

  • 我无法理解 answer 中的任何内容。请编辑您的答案,以更好地解释您是如何修复它的。您确切地编辑了哪个文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多