phpmailer_init ignored on REST calls
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.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}