Advanced Gravity Forms: Custom Gravity Forms notification emails

Posted by Lloyd
Last updated

When a customer interacts with our site in any way, we get a notification email generated by our gravity form, which looks like this:

bay.a - contact form

You’ll notice that unlike the default Gravity Forms notification, we’ve “branded” this to our site.

Branded gravity forms notification emails are useful for a number of reasons, especially as notification emails can also be sent to the sender as well as the recipient¬†– so on your contact form, you’re able to notify both your site and the person who sent the message.

Ensuring that all interactions that the customer has with your site/brand are consistent and at the same level of quality is a no brainer – and it matters, down to things as “small” as a branded message confirmation.

How to do it:

1. In your theme’s “functions.php” file, usually found in /wp-content/themes/YOUR-THEME-NAME/functions.php, just place the following code:

function email_header(){
	return "<img width='181' height='46' align='right' alt='".get_bloginfo('name')."' src='http://yoursite.com/path/to/image.png' style='width: 181px; height: 46px; top: 0em; right: 0em;' />



";
}
function email_footer(){
	return "

Thanks,
".get_bloginfo('name');
}
//Append header + footer to all emails
add_filter( 'gform_notification', 'form_notification_email', 10, 3 );
function form_notification_email($notification, $form, $entry) {
	$notification['message'] = email_header()."Dear ".get_bloginfo('name').",

Form submission:

".$notification['message']."

Please review suitability.";
	$notification['message'] .= email_footer();
	return $notification;
}

Now, you’ll notice

http://yoursite.com/path/to/image.png

in the code above. This will obviously have to be replaced by the path to an image you upload, to be shown in the email’s header. I usually recommend manually uploading the image to your theme’s directory, rather than the WordPress Media area (I treat the “Media” area as a place for post / page related transitory media, and place more permanent things in the theme folder).

You’ll also have to replace the image’s dimensions with ones that are proportional to those of your image.

And that’s it! – The

get_bloginfo('name')

prints your site’s name – but you could also hard-code this if you prefer.

But why have I split the whole thing into separate functions?

The first reason is just neatness and wanting to modularise everything, ever, but the second is because..

Notifications can actually be tailored to each of your Gravity Forms.

For example, if I create the following form:

gravity-forms-get-id

We can see it has an ID of 5 (1. in the image above). If we wish to limit the branded notification to (or just want a different notification for) this form, we can easily do this by modifying our code above to this:

function email_header(){
	return "<img width='181' height='46' align='right' alt='".get_bloginfo('name')."' src='http://yoursite.com/path/to/image.png' style='width: 181px; height: 46px; top: 0em; right: 0em;' />



";
}
function email_footer(){
	return "

Thanks,
".get_bloginfo('name');
}
add_filter( 'gform_notification_5', 'form_notification_email_5', 10, 3 );
function form_notification_email_5($notification, $form, $entry) {
	$notification['message'] = email_header()."Dear ".get_bloginfo('name').",

Form submission from the contact form:

".$notification['message']."

Please review suitability.";
	$notification['message'] .= email_footer();
	return $notification;
}

The most important change to note is that we’ve changed the general, catch-all filter

gform_notification

to

gform_notification_5

, which only targets notifications from the form with the ID of 5 (taken from our form above).

This is useful as we can change the content of the message(s) based on the form submitted. This is also why the modularisation of the

email_header()

and

email_footer()

functions comes into play:

We can include the same email header & email footer, whilst having unique ‘body’ content. Here’s an example of giving a different notification to two different forms (one with an ID of 5, and one with an ID of 6):

function email_header(){
	return "<img width='181' height='46' align='right' alt='".get_bloginfo('name')."' src='http://yoursite.com/path/to/image.png' style='width: 181px; height: 46px; top: 0em; right: 0em;' />



";
}
function email_footer(){
	return "

Thanks,
".get_bloginfo('name');
}
add_filter( 'gform_notification_5', 'form_notification_email_5', 10, 3 );
function form_notification_email_5($notification, $form, $entry) {
	$notification['message'] = email_header()."Dear ".get_bloginfo('name').",

Form submission from the contact form:

".$notification['message']."

Please review suitability.";
	$notification['message'] .= email_footer();
	return $notification;
}

add_filter( 'gform_notification_6', 'form_notification_email_6', 10, 3 );
function form_notification_email_6($notification, $form, $entry) {
	$notification['message'] = email_header()."Dear ".get_bloginfo('name').",

Form submission from the quote request form:

".$notification['message']."

Please review suitability.";
	$notification['message'] .= email_footer();
	return $notification;
}

This can be repeated for however many custom notifications you’d like to create.

That’s all there is to it!

If you like what you’ve read, or have any questions about gravity forms custom notification emails, just let me know below and I’ll do my best to answer your question!

2 Comments

Dennis

Why use the “, 10, 2” hook arguments here? Gravity Form docs have it as “, 10, 3”.

Any differences?

Reply
Lloyd

Hello Dennis!
Apologies for the slow reply.

I only use $notification and $form so have put 10,2 – but if you wanted to use $notification, $form, and $entry then you could use three.

10,3 is probably safest though. I will update the article to 10,3.
Thanks!

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *