wp mail - phpmailer_init ignored on REST calls

admin2025-06-02  3

For testing purposes I've set up a mailtrap (dummy SMTP server) to catch all mail in and out of my local site. To do so, I hooked its configuration in the theme's functions.php like this:

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');

The hook worked like a charm so far, and still does for all front-facing functionalities.

Problem is, I now had to expose some REST API endpoints to serve an associated mobile app, some of which will send emails. The messages are sent with the usual

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

To my surprise, I noticed those emails not getting catched by mailtrap and ended up spamming a mailbox I wasn't supposed to write to.

So I wonder, does the REST API structure ignore functions.php? Is there a way I can hook mailtrap so that it will work on API calls aswell?

Thanks in advance for any advice and have a nice day!

EDIT:

As suggested, I will detail all files and calls involved for more clarity.

In functions.php I have both the mailer hook and the include for the script containing the endpoint definitions:

<?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');

In rest-api-utils.php I have the endpoint definition and callbacks:

<?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 -- 
    remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}

For testing purposes I've set up a mailtrap (dummy SMTP server) to catch all mail in and out of my local site. To do so, I hooked its configuration in the theme's functions.php like this:

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');

The hook worked like a charm so far, and still does for all front-facing functionalities.

Problem is, I now had to expose some REST API endpoints to serve an associated mobile app, some of which will send emails. The messages are sent with the usual

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

To my surprise, I noticed those emails not getting catched by mailtrap and ended up spamming a mailbox I wasn't supposed to write to.

So I wonder, does the REST API structure ignore functions.php? Is there a way I can hook mailtrap so that it will work on API calls aswell?

Thanks in advance for any advice and have a nice day!

EDIT:

As suggested, I will detail all files and calls involved for more clarity.

In functions.php I have both the mailer hook and the include for the script containing the endpoint definitions:

<?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');

In rest-api-utils.php I have the endpoint definition and callbacks:

<?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/ticket/23578
    remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
Share Improve this question edited Mar 11, 2019 at 13:17 GigiSan asked Mar 11, 2019 at 11:47 GigiSanGigiSan 2452 silver badges11 bronze badges 3
  • 1 No, the REST API does not ignore functions.php. Are these REST API endpoints yours? Or a plugin's? If they're yours then what's the code you've used to register them? – Jacob Peattie Commented Mar 11, 2019 at 11:56
  • @JacobPeattie yes, the endpoint is mine, I included all the relevant files and calls. Thanks for the suggestion. – GigiSan Commented Mar 11, 2019 at 13:18
  • @JacobPeattie I just realized it was silly to think it could be ignored since the endpoints are ultimately defined in functions.php, even if indirectly. :) But still, I don't know why the mailer would behave differently. – GigiSan Commented Mar 11, 2019 at 13:24
Add a comment  | 

1 Answer 1

Reset to default 0

I found the "issue".

I had another mailing provider (Postmark) installed as a plugin, which I brought in my local site by mistake. That prevailed over any other mailing configuration.

I deactivated the plugin and now my hook works fine.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748819285a313988.html

最新回复(0)