【问题标题】:how to send emails with PHPmailer from WordPress Rest API end point如何从 WordPress Rest API 端点使用 PHPmailer 发送电子邮件
【发布时间】:2016-05-26 23:45:38
【问题描述】:
function my_send_email($name,$email, $current_user, $status, $subject, $Message) {
    //most of the code here is from https://github.com/PHPMailer/PHPMailer/tree/master/examples

    require_once ABSPATH . WPINC . '/class-phpmailer.php'; 
    require_once ABSPATH . WPINC . '/class-smtp.php';

    // build message body`enter code here`
   $body = '
    <html>
        <body>
        <h1>My Email</h1>
        </body>
    </html>
    ';

    try {
        //Create a new PHPMailer instance
        $mail = new PHPMailer;

        //Tell PHPMailer to use SMTP
        $mail->isSMTP();

        //Enable SMTP debugging
        $mail->SMTPDebug = 2;

        //Ask for HTML-friendly debug output
        $mail->Debugoutput = 'html';

        //Set the hostname of the mail server
        $mail->Host = "mail.example.com";

        //Set the SMTP port number - likely to be 25, 465 or 587
        $mail->Port = 25;

        //Whether to use SMTP authentication
        $mail->SMTPAuth = true;

        //Username to use for SMTP authentication
        $mail->Username = "contact@example.com";

        //Password to use for SMTP authentication
        $mail->Password = "1234";

        //Set who the message is to be sent from
        $mail->setFrom('contact@example.com', 'sender_name');

        //Set an alternative reply-to address
        $mail->addReplyTo('alternative_contact@example.com', 'alternative_contact_name');

        //Set who the message is to be sent to
        $mail->addAddress($email, $name);
        //Set the subject line
        $mail->Subject = 'PHPMailer SMTP test';
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        //$mail->msgHTML(file_get_contents(dirname(__FILE__) .'/contents.html'));
        $mail->MsgHTML($body);
        //Replace the plain text body with one created manually
        $mail->AltBody = 'This is a plain-text message body';

        $success=$mail->send();
        //send the message, check for errors
        if ($success) {
            return array('mailError' => false, 'message' => 'email sent! ' . $mail->ErrorInfo , 'body'=>$body);
        } else {
            return array('mailError' => true, 'message' => 'email error! ' . $mail->ErrorInfo , 'body'=>$body);
        }
    } catch (phpmailerException $e) {
        return array('mailError' => false, 'message' => 'email error! ' . $e->errorMessage() , 'body'=>$body);  //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        return array('mailError' => false, 'message' => 'email error! ' . $e->getMessage() , 'body'=>$body); //Boring error messages from anything else!
    }

}

function my_register_endpoints(){
    register_rest_route(

        'my_namespace/v1',
        '/abc/',
        array(
            'methods' => 'POST',
            'callback' => 'my_end_point',
            'args' => array(
                    .....
            ),
            'permission_callback' => function (WP_REST_Request $request) {

                if(!is_user_logged_in()){
                .....
                }
                return true;

            }
        )

    );
}

add_action('rest_api_init','my_register_endpoints'); 

//endpoint
function my_end_point(WP_REST_Request $request){

        global $current_user;
        $sender = wp_get_current_user();
        $shortMsg=$request['shortMessage'];
        $subject="Some text";

        $email_resualt=my_send_email("reciver_name","reciver_email",$sender,$status,$subject,$shortMsg);

        if($email_resualt['mailError']==false){

            process.. $itemes, $item ..    
            $message='email sent!';
            return array('message' => $message,'items' => $items, 'item'=>$item);

        } else {

             return new WP_Error('email error',__('some message','email-error'),$request['id']);

        }   

}

我正在使用 angularJS $http.post 调用 API 端点,电子邮件已发送,我可以在收件箱中看到它们,但由于某种原因,我收到了未定义的响应:

Chrome 控制台上的相关错误: 1) SyntaxError: 位置 0 处 JSON 中的意外标记 S

2) TypeError: Cannot read property 'message' of undefined

我在客户端代码中使用 response.message,但如上所述,但由于响应为空,我收到错误。

我的 WordPress PHP 代码中有其他端点,它们看起来相同,但不包括使用 PHPmailer 发送电子邮件并且工作正常。甚至这个没有发送电子邮件功能的端点也可以正常工作。

我尝试调试了两天,但仍然不确定我的代码中的问题在哪里?

【问题讨论】:

  • 检查开发工具网络中的实际请求并查看发送的确切内容。注意returnecho不一样
  • 谢谢,也许我忘了提,但我确实调试了客户端和服务器端代码,我在客户端使用了 chrome 开发人员工具并在我的 $http 回调中得到 response = undefined,我使用了 sublime和 XDebuge 调试 PHP 代码并到达 my_end_point 行: return new WP_Error('email error',__('some message','email-error'),$request['id']); .这似乎返回 response.message 但客户端显示 undefined 。

标签: angularjs wordpress api rest phpmailer


【解决方案1】:

我发现了问题所在,在我的发送电子邮件功能中,我有以下几行:

//开启SMTP调试

$mail->SMTPDebug = 2;

$mail->Debugoutput = 'html';

而且似乎因为调试已打开,所以在我评论这两行之后,来自我的端点的响应是匹配 angular 来处理,响应结构良好,以便 angular 能够解析它。

解决它的提示在以下帖子Syntax error: Unexpected number at Object.parse, PHP to AngularJS

【讨论】:

    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2021-01-24
    相关资源
    最近更新 更多