Suppose you approve a user and want to attach a document to the email. This is easy to do just by adding a filter to core wp_mail function.

function nua_include_attachment( $attributes ) {
    if ( strpos( $attributes['subject'], 'Registration Approved' ) !== 0 ) {
        // add an attachment
        $attachments = array( WP_CONTENT_DIR . '/uploads/support.zip' );
        $attributes['attachments'] = $attachments;
    }

    return $attributes;
}
add_filter( 'wp_mail', 'nua_include_attachment' );

A few things to note.

if ( strpos( $attributes['subject'], 'Registration Approved' ) !== 0 ) {

None of the code will execute if the subject has been modified from the default. This would need to be modified to match part of the subject.

$attachments = array( WP_CONTENT_DIR . '/uploads/support.zip' );

Point to the correct path of the file you would like attached. Since this is an array, you can add more than one file.

Leave a Reply

  • (will not be published)