Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Created March 13, 2015 15:37
Show Gist options
  • Save kjohnson/50c55745fd63bdbf21fc to your computer and use it in GitHub Desktop.
Save kjohnson/50c55745fd63bdbf21fc to your computer and use it in GitHub Desktop.
Add an Attachment to Email Notifications in Ninja Forms
<?php
/*
Plugin Name: Ninja Forms Email Attachment
*/
function add_my_attachment( $attachments ) {
$attachments[] = 'server_path_to_my_file';
return $attachments;
}
add_filter( 'nf_email_notification_attachments' , 'add_my_attachment' );
@flyersjoe
Copy link

How do you use this?

@devang-patel
Copy link

But it will add attachment to all email notifications, What if we just want to send attachment to specific form?

Thanks in advance.

@jat-247
Copy link

jat-247 commented Mar 12, 2018

Per the author, this code is old and is for NFv2.9.x.

For NinjaForms 3.x instead, use
apply_filters( 'ninja_forms_action_email_attachments', $attachments, $data, $settings )
The second parameter, $data, is the form data, where you can check the form ID if you want to do this for a specific form.

@ColumbiaWebDev
Copy link

This might be obvious to some, but it wasn't to me and had a hard time finding the data object values. So just in case someone else finds this via Google search like I did:

// attach file to only certain form(s) -- the ID is the same as the # in the shortcode

if ($data['form_id'] == 2) {

    $attachments[] = "server_path_to_file";
    return $attachments;
}

@mynameiskyle
Copy link

Still pretty confused here. Would it be possible for someone paste the entire snippet to make this work?

Also, does this go into the functions.php file?

Thanks in advance.

@Joziee
Copy link

Joziee commented Oct 3, 2018

Finally got it right. Here is my code placed at the bottom of the functions.php page

add_filter( 'ninja_forms_action_email_attachments', function( $attachments, $data, $settings ) {
// Append file path.
$attachments[] = get_parent_theme_file_path( 'STANDARD-TERMS-AND-CONDITIONS.pdf' );
return $attachments;
}, 10, 3 );

@papazetis
Copy link

papazetis commented Oct 19, 2018

add_filter( 'ninja_forms_action_email_attachments', function( $attachments, $data, $settings ) { if ($data['form_id'] == 11) { $upload_dir = wp_upload_dir(); $attachments[] = $upload_dir['basedir'] . '/2018/10/the_file_you_want_to_attach.pdf'; return $attachments; } }, 10, 3 );

But still this filter will send the attachment in each email on that form.
I ve got two email actions. How can I select the attachment to go only to the first email?

@ps-PKrueger
Copy link

Has there been an answer to the above questions, or is this just not possible?

@csgaraglino
Copy link

I as well have this working and I as well only want it sent to the person who filled out the form, not the site admin.

@dolader
Copy link

dolader commented Apr 20, 2020

Hi i came across this issue and even it's from november 2019 maybe i can help some other people.
First, it's my first time that i'am using Ninja Forms. I always use Gravity Form more Developer friendly i think but it's no free plugin.

I had the same problem, two e-mail actions one for the site-owner and the other for the user/subscriber
It is not difficult to exclude the site-owner e-mail.
In this (ninja_forms_action_email_attachments) hook and it's method takes three parameters.
The $setting parameter is an array with values for the form, use that to write a conditional and thats it.

Below a piece of code to get the idea. It's based on the action label in this case the email-type action.

if ($data['form_id'] == 3 ) {
    if ( $settings['type'] == 'email' && $settings['label'] != 'Admin email' ) {
          $attachments = array( WP_CONTENT_DIR  . '/uploads/blablabla.pdf');

        return $attachments;

}
}

@ouija
Copy link

ouija commented Oct 31, 2021

Just also wanted to mention to others who my enqire, to get the post_id for the submission, it's stored under $data['actions']['save']['sub_id'] (but you need to have "store submission" enabled)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment