在REST调用中忽略phpmaeller_init

时间:2019-03-11 作者:GigiSan

出于测试目的,我设置了一个mailtrap(虚拟SMTP服务器)来捕获本地站点内外的所有邮件。为此,我在主题的functions.php 像这样:

function mailtrap($phpmailer) {
    $phpmailer->isSMTP();
    $phpmailer->Host = \'smtp.mailtrap.io\';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 2525;
    $phpmailer->Username = \'**************\';
    $phpmailer->Password = \'**************\';
}
add_action(\'phpmailer_init\', \'mailtrap\');
到目前为止,钩子就像一个魔咒,仍然适用于所有正面功能。

问题是,我现在不得不公开一些RESTAPI端点来服务相关的移动应用程序,其中一些将发送电子邮件。这些消息与通常的

wp_mail( $to_email, $subject, $message, $headers);
令我惊讶的是,我注意到那些邮件没有被mailtrap抓到,结果却向一个我本不应该写信的邮箱发送垃圾邮件。

所以我想知道,REST API结构是否忽略了functions.php?有没有一种方法可以挂接mailtrap,以便它也可以处理API调用?

提前感谢您的建议,祝您愉快!

EDIT:

正如所建议的那样,我将详细介绍所有涉及的文件和调用,以便更清楚地了解情况。

在里面functions.php 对于包含端点定义的脚本,我有mailer钩子和include:

<?php

require(\'functions/rest-api-utils.php\');

(...)

function mailtrap($phpmailer) {
   $phpmailer->isSMTP();
    $phpmailer->Host = \'smtp.mailtrap.io\';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 2525;
    $phpmailer->Username = \'316fb924a7c634\';
    $phpmailer->Password = \'e96e24567d9a75\';
}
add_action(\'phpmailer_init\', \'mailtrap\');
在中rest-api-utils.php 我有端点定义和回调:

<?php
add_action( \'rest_api_init\', function () {
    $api_namespace = "mobileapi";
    $api_version = "1";
    register_rest_route( "$api_namespace/v$api_version", \'/requestassistance/\', array(
    \'methods\' => \'POST\',
    \'callback\' => \'mobileapi_requestassistance\',
    \'args\' => array(
      \'serial\' => array(
            \'required\' => TRUE,
            \'type\' => \'string\',
      ),
      \'request\' => array(
            \'required\' => TRUE,
            \'type\' => \'string\',
      ),
      \'point\' => array(
            \'required\' => TRUE,
            \'type\' => \'string\',
      ),
    ),
    \'permission_callback\' => function () {
        return mobileapi_check_customer();
    }
));

function mobileapi_check_customer() {
    return (is_user_logged_in() && get_field(\'is_customer\', wp_get_current_user()));
}

function mobileapi_requestassistance(WP_REST_Request $request) {
    $response = new WP_REST_Response();

    $error = custarea_validation(); // returns false
    if ($error) {
            return new WP_Error( \'mobileapi_requestassistance_error\', \'Error.\', array( \'status\' => 403 ) );
    }

    // Send request mail
    mobileapi_notify_point();

    $response->set_data("");
    $response->set_status(200);
    return $response;
}

function mobileapi_notify_point() {
    $user = wp_get_current_user();

    $subject = __(\'Assistance request\', \'theme\');
    $headers = array();

    add_filter( \'wp_mail_content_type\', function( $content_type ) {return \'text/html\';});

    (... addresses and body composition ...)

    wp_mail( $to_email, $subject, $message, $headers);

    // Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
    remove_filter( \'wp_mail_content_type\', \'set_html_content_type\' );
}

1 个回复
最合适的回答,由SO网友:GigiSan 整理而成

我发现了“问题”。

我安装了另一个邮件提供商(邮戳)作为插件,这是我错误地将其引入本地站点的。这优于任何其他邮件配置。

我停用了插件,现在我的挂钩工作正常了。