简体   繁体   English

WordPress PHP服务器脚本发送电子邮件

[英]Wordpress PHP Server Script to send email

I am trying to get a server script to use wp_mail() and send an email to a user. 我正在尝试获取服务器脚本以使用wp_mail()并将电子邮件发送给用户。 I am doing this as part of a password reset routine, and calling the script with Ajax from the user's side. 我将其作为密码重置例程的一部分,并从用户方面使用Ajax调用脚本。

I am getting the following error from my server script: 我从服务器脚本中收到以下错误:

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

I cannot seem to find an answer to my question, so maybe I am asking incorrectly. 我似乎无法找到问题的答案,所以也许我在问错。 Is what I am doing possible? 我有可能做什么吗?

My server script (pwd.php) contains: 我的服务器脚本(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); 

?>

Any help is appreciated. 任何帮助表示赞赏。

You need to include wp-load.php in your file.... 您需要在文件中包含wp-load.php

Just put this in top of your file. 只需将其放在文件顶部即可。

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

The problem is you are not including Wordpress, so you are not using it at all (and it isn't including its libraries). 问题是您不包括Wordpress,因此根本不使用它(并且不包括其库)。

Plus, this is unrelated to the question, but you have a weakness in your code : Any users can define the recipient of the email. 另外,这与问题无关,但是您的代码有一个缺点:任何用户都可以定义电子邮件的收件人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM