ulgm_mail_to
Filters the recipient email address for outgoing emails before sending, allowing modification of the 'To' field.
add_filter( 'ulgm_mail_to', $callback, 10, 1 );
Description
Filters the recipient email address for emails sent by the plugin. Allows developers to modify the default 'to' address, for example, to redirect emails to a different user or implement custom recipient logic before sending.
Usage
add_filter( 'ulgm_mail_to', 'your_function_name', 10, 1 );
Parameters
-
$to(mixed) - This parameter holds the recipient(s) of the email, which can be a single email address, an array of email addresses, or other mixed types depending on how the filter is applied.
Return Value
The filtered value.
Examples
<?php
/**
* Example of using the 'ulgm_mail_to' filter hook to modify the recipient of an email.
*
* This function demonstrates how to intercept the 'to' recipient in emails sent
* via the ULGmail Mailer (presumably) and potentially redirect or modify it.
* For instance, you might want to send a copy of all outgoing emails to a specific
* administrative address for logging or debugging purposes.
*/
add_filter(
'ulgm_mail_to',
function ( $to_recipients ) {
// If the original $to is an array of recipients, we can iterate through them.
// If it's a single string, we'll handle that case.
if ( is_array( $to_recipients ) ) {
// Add an administrative address to receive a copy of all emails.
// Ensure we don't add duplicates if it's already present.
$admin_bcc_address = '[email protected]';
if ( ! in_array( $admin_bcc_address, $to_recipients ) ) {
// For BCC, you'd typically prepend 'BCC:' to the address if the
// wp_mail function handles it directly, or if the plugin expects
// it. For simplicity here, we'll just add it to the array.
// Depending on the plugin's internal handling, you might need
// to return an array like ['[email protected]', 'BCC:[email protected]'].
// Assuming ULGmail handles it by adding a BCC header later or
// expects it in the 'to' array for specific purposes.
$to_recipients[] = $admin_bcc_address;
}
} elseif ( is_string( $to_recipients ) && ! empty( $to_recipients ) ) {
// If it's a single recipient, wrap it in an array for consistent handling
// or add the BCC address directly if the plugin supports it that way.
// For this example, we'll assume we're modifying the primary recipient
// or adding a BCC if that's how the plugin interprets it.
// A more robust solution might involve inspecting the context of the email
// if possible, but without more info, we'll perform a general modification.
// Let's assume for this example, we want to send *all* emails to a
// specific testing address if a certain condition is met (e.g., in staging).
// For demonstration, we'll just redirect everything to a test address.
$test_recipient = '[email protected]';
// In a real scenario, you'd check a condition:
// if ( defined('WP_ENV') && WP_ENV === 'staging' ) {
// return $test_recipient;
// }
// If not redirecting entirely, you might add a BCC. The exact method
// depends on how `ulgm_mail_to` is expected to be used.
// For now, let's assume we might want to prepend a BCC to the existing recipient.
// This is a simplified example. The actual handling of BCC might be different.
// A more common way to add BCC is via the $headers parameter, which is
// filtered separately by 'ulgm_mail_headers'.
// For the sake of demonstrating `ulgm_mail_to` specifically, let's
// consider a scenario where we want to ensure a specific user always gets emails.
$specific_user_email = '[email protected]';
if ( $to_recipients !== $specific_user_email ) {
// If the primary recipient isn't the important user, add them.
// This might create duplicates if the important user is already the recipient.
// Again, checking for existence is good practice.
if ( strpos( $to_recipients, $specific_user_email ) === false ) {
$to_recipients .= ', ' . $specific_user_email;
}
}
}
// Always return the modified (or original) recipient(s).
return $to_recipients;
},
10, // Priority: default is 10
1 // Accepted args: $to_recipients is the only parameter passed to the filter.
);
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/helpers/shared-functions.php:915
public static function wp_mail( $to, $subject, $body, $headers = array(), $attachment = array() ) {
$body = do_shortcode( $body );
$subject = do_shortcode( $subject );
$to = apply_filters( 'ulgm_mail_to', $to );
$subject = apply_filters( 'ulgm_mail_subject', html_entity_decode( $subject ) );
$body = apply_filters( 'ulgm_mail_body', html_entity_decode( $body ) );
$headers = apply_filters( 'ulgm_mail_headers', $headers );
$attachment = apply_filters( 'ulgm_mail_attachment', $attachment );
return wp_mail( $to, $subject, $body, $headers, $attachment );
}