Wordpress PHP Server脚本发送电子邮件

时间:2014-02-27 04:59:59

标签: php ajax wordpress

我正在尝试使用服务器脚本来使用wp_mail()并向用户发送电子邮件。我这是在密码重置例程中执行此操作,并从用户端使用Ajax调用脚本。

我从服务器脚本中收到以下错误:

Fatal error: Call to undefined function wp_mail() in /var/www/abc-php/pwd.php on line 57

我似乎无法找到问题的答案,所以也许我的问题不正确。我正在做什么?

我的服务器脚本(pwd.php)包含:

<?php

    $setFromId = "info@abc.com.au";
    $setFromName = "Info @ abc";
    $replyToId = "info@abc.com.au";
    $replyToName = "Info @ abc";

    $sendToEmail = base64_decode($_GET["ste"]);   //the URL in AJAX call contains base64 encoded data
    $resetPwd = base64_decode($_GET["rp"]);
    $resetSalt = base64_decode($_GET["rs"]);
    $param = $_GET["var"];

    $username = "xxxxxxxxx"; 
    $password = "xxxxxxxx";   
    $host = "localhost";
    $database = "xxxxxxxxx";
    $con = mysqli_connect($host, $username, $password, $database);

    // Check connection
    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    } else {
        switch ($param) {
            case "PWDRES":                                              // Query 1: Current Logged In Partner Details 
                $mysqli = new mysqli($host, $username, $password, $database);
                if (mysqli_connect_errno()) {
                    echo '<p>Unable to connect to the server at this time to update and send password. Please contact SMA for help or try again.</p>';
                } else {

                    $myquery = "UPDATE abc_users
                                SET `password` = SHA2('" . $resetPwd . $resetSalt . "', 512),
                                    `salt_value` = SHA2('" . $resetSalt . "', 512)
                                WHERE email_user_id = '" . $sendToEmail . "'";

                    mysqli_query($mysqli, $myquery);

                    if($mysqli->affected_rows >= 1) {

                        $headers = 'Reply-To: <' . $replyToName . '> ' . $replyToId;
                        $subject = "Password Reset - Confidential";
                        $body = "Subject: " . $subject . "<br/><br/>Your new password is: <br/><strong>" . $resetPwd . "</strong><br/><br/>Please ensure you reset this password next time you log in.";                        
                        $mail = wp_mail($sendToEmail, $subject, $body, $headers);
                        echo "Password was successfully reset and sent to " . $sendToEmail;

                    } else if($mysqli->affected_rows == 0) {
                        echo "No user id row was updated in the process of resetting password. Please try again.";
                    } else if($mysqli->affected_rows < 0) {
                        echo "There was an SQL error updating the user id password table. Please contact SMA for help.";
                    };
                };
            break;

                    case "OTHER1"
                    etc, etc, etc...
        default;

        };
    };

    unset($con);    
    unset($myquery); 
    unset($mysqli); 

?>

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您需要在文件中加入wp-load.php ....

只需将其置于文件的顶部即可。

require_once( dirname(__FILE__) . '/wp-load.php' );

答案 1 :(得分:0)

问题是你没有包含Wordpress,所以根本就没有使用它(而且它不包括它的库)。

另外,这与问题无关,但您的代码存在缺陷: 任何用户都可以定义电子邮件的收件人。