Programmatically Attaching a File to Email Notifications

Would you like to automatically attach a file to your WPForms email notifications? By default, email notifications can include uploaded files, but sometimes you may want to programmatically attach a specific file such as a PDF, terms sheet, or custom document.

In this tutorial, we’ll show you how to use the wpforms_emails_mailer_get_attachments filter to attach files to email notifications.

Adding the Code Snippet

To attach a file programmatically, you’ll need to add a custom code snippet to your site. If you need help adding code, please see our tutorial on adding custom PHP snippets.

Here’s an example that attaches a PDF file:

Customizing the Snippet

You can adjust this snippet for your needs:

  1. File Path: Replace /path/to/your/file.pdf with the actual file URL in line 5.
  2. Multiple Files: To attach more than one file, repeat the $attachments[] = $file_path; line for each file.
  3. Conditional Logic: If you only want the file attached for a specific form, add a check for $mailer->get( 'form_data' )['id'].

And that’s it! You’ve now set up WPForms to automatically attach custom files to your email notifications.

Reference Filter

wpforms_emails_mailer_get_attachments