Home / Admin / How to Programmatically Attach a File to Email Notifications
Duplicate Snippet

Embed Snippet on Your Site

How to Programmatically Attach a File to Email Notifications

This code snippet uses the wpforms_emails_mailer_get_attachments filter to attach files to email notifications.

<10
Code Preview
php
<?php
add_filter( 'wpforms_emails_mailer_get_attachments', function( $attachments, $mailer ) {
    // Get the uploads directory path.
    $upload_dir = wp_upload_dir();
   // Add your custom file path(s)
    $file_url = '/path/to/your/file.pdf';
    
    // Convert URL to local file path.
    $file_path = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $file_url );
    
    // Check if the file exists on the server.
    if ( file_exists( $file_path ) ) {
        $attachments[] = $file_path;
    }
    
    return $attachments;
}, 10, 2 );

Comments

Add a Comment