Change the “from” email address dynamically with Ninja Forms

Here is a great little snippet that we find quite useful here at the WP Ninjas.

By default all emails are sent from the address you set in the form settings of Ninja Forms. As an administrator, when someone fills out our contact form it comes from that email address. So if we are on the go and get an email that we want to reply to and we click reply it will create a new email to… you guessed it…us. Well that’s not very useful, especially on a smart phone. Trying to copy and paste the correct email is just a painful and now unnecessary step.

Just create a custom plugin for all your Ninja Forms modifications and add this little snippet.

add_action( 'ninja_forms_email_admin', 'ninja_forms_change_from_address' );
function ninja_forms_change_from_address(){
   global $ninja_forms_processing;
   if( 1 == $ninja_forms_processing->get_form_ID() ) {
	$user_email = $ninja_forms_processing->get_field_value( 2 );
	$user_name = $ninja_forms_processing->get_field_value( 1 );
	$user_email = $user_name." <".$user_email.">";
	$ninja_forms_processing->update_form_setting( 'email_from', $user_email );
   }
}

On line 4, 5, & 6 you will want to change the 1, 2 & 1 to your form ID and email / name field IDs respectively from your Ninja Form.

Because we are hooking into the the ninja_forms_email_admin action this will only change the from email address for the emails that your administrators receive when a user submits your form.

Now when you select reply in your favorite email editor the to field will automatically be filled with the user who submitted the form in the first place.

I hope you find this useful.