admin管理员组

文章数量:1418361

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?

/

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.

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/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.

Share Improve this question edited Jul 29, 2019 at 15:55 BlueDogRanch asked Jul 26, 2019 at 16:42 BlueDogRanchBlueDogRanch 1705 silver badges25 bronze badges 8
  • how you know they use php mail() function. – Bhautik Commented Jul 26, 2019 at 16:47
  • I don't know if Swiftmailer uses phpmail. – BlueDogRanch Commented Jul 26, 2019 at 17:11
  • 1 The plugin authors may be able to tell you – Andy Macaulay-Brook Commented Jul 26, 2019 at 17:46
  • Can you post the contents of the email that is received by the user?? – ChristopherJones Commented Jul 26, 2019 at 19:11
  • 1 if you found wp_mail() function then you can use hook or filter to change you body content. – Bhautik Commented Jul 27, 2019 at 4:59
 |  Show 3 more comments

1 Answer 1

Reset to default 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.)

本文标签: phpHow can I hook into the wpmail function used by BackWPup