0

I know this question is partially concerns a third-party plugin, BackWPup, which is off-topic, but in general, in terms of WordPress and PHP: is it possible to hook into the wp_mail function so can I append text to the plugin's PHP generated email?

The email is sent by BackWPUp, a WordPress database backup plugin, and the email contains a success or fail message about the backup file. In my case, the email will go to a general email support address, and may be received by someone who may not fully familiar with how to deal with the email. I need to be able to add more instructional and explanatory text to the email, preferably at the beginning of the email.

And I need to add that text by using an external php function, because if I modify the plugin, the changes will be overwritten when the plugin is updated; so I need to add the function to either in the theme's functions.php file, or make my own simple WordPress plugin.

The wp_mail function in the plugin that is used appears to be:

wp_mail(
$this->job['mailaddresslog'],
$subject,
file_get_contents( $this->logfile ),
$headers
);

is it possible to hook into the $messageand add additional text? It has been replaced with file_get_contents in the function. Do I need to hook into file_get_contents?

https://developer.wordpress.org/reference/functions/wp_mail/

Edit 7/28/19

I added a check for the subject line, as all emails sent had the additional text appended. Now, only BackWPUp emails get the additional text.

add_filter( 'wp_mail', 'wpse343761_filter_message' );
function wpse343761_filter_message( $atts ) {

    if ( stripos( $atts['subject'], 'BackWPup')) {

    if ( ! empty( $atts['message'] ) ) {
        $atts['message'] .= 'Your additional text';
    }
}
    return $atts;
}

New questions on modifying message: how can I use strip_tags on the message to convert the default html email to non-html email? Adding strip_tags($atts['message']); before the return doesn't do anything.

And, how can the "additional text message" be added to the beginning of the email? It's added to the end right now.

  • how you know they use php mail() function. – Bhautik Jul 26 at 16:47
  • I don't know if Swiftmailer uses phpmail. – BlueDogRanch Jul 26 at 17:11
  • 1
    The plugin authors may be able to tell you – Andy Macaulay-Brook Jul 26 at 17:46
  • Can you post the contents of the email that is received by the user?? – ChristopherJones Jul 26 at 19:11
  • 1
    if you found wp_mail() function then you can use hook or filter to change you body content. – Bhautik Jul 27 at 4:59
1

Looking at the source of the wp_mail() function, there's a filter right near the top:

$atts = apply_filters( 
    'wp_mail', 
    compact( 'to', 'subject', 'message', 'headers', 'attachments' )
);
// Expanded for clarity.

(Aside: compact() is a PHP function that creates an array from a set of arguments. In this case, it's making an array of the $to, $subject, $message, $headers, and $attachments parameters passed to wp_mail().)

...and then a little further on:

if ( isset( $atts['subject'] ) ) {
    $subject = $atts['subject'];
}

So yes, you can filter the message. Something like this should do the trick:

add_filter( 'wp_mail', 'wpse343761_filter_message' );
function wpse343761_filter_message( $atts ) {
    if ( ! empty( $atts['message'] ) ) {
        // If there's already a message, add to it.
        $atts['message'] .= 'Your additional text';
    } else {
        // If there's not a message set yet, set one.
        $atts['message'] = 'Your desired message';
    }
    return $atts;
}

Note: This code is untested.

Edit: In response to your update And, how can the "additional text message" be added to the beginning of the email?:

Change this line:

$atts['message'] .= 'Your additional text';

to:

$atts['message'] = 'Your additional text' . $atts['message'];

And to your strip_tags() usage question, you can do this:

$atts['message'] = strip_tags( $atts['message'] );

on the line before you return. (You can't just use strip_tags( $atts['message'] ) because that doesn't assign the stripped string to anything.)

  • Thanks, that works. I added a check for the subject line, since all admin emails were getting the additional text. Do you know how I can use strip_tags to convert the html email to plain text? I edited my question. – BlueDogRanch Jul 28 at 15:26
  • See my updates. – Pat J Jul 28 at 15:37
  • Thanks! Works great. – BlueDogRanch Jul 29 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.