PostmarkApp Headers for Gravity Forms

Last updated 2 years ago

PHP
/**
 * Postmark Headers for Gravity Forms.
 *
 * @param  [type] $email          Email.
 * @param  [type] $message_format Message Format.
 * @param  [type] $notification   Notification.
 */
function postmark_headers_for_gravityforms( $email, $message_format, $notification, $entry ) {
	$entry_id = $entry['id'] ?? '';
	$form_id  = $entry['form_id'] ?? '';

	if ( $entry_id ) {
		$email['headers']['X-PM-Metadata-gf-entry-id'] = 'X-PM-Metadata-gf-entry-id: ' . $entry_id;
	}

	if ( $form_id ) {
		$email['headers']['X-PM-Metadata-gf-form-id'] = 'X-PM-Metadata-gf-form-id: ' . $form_id;
	}

	$email['headers']['X-PM-Tag'] = 'X-PM-Metadata-tag: gravity-forms';

	return $email;

}

add_filter( 'gform_pre_send_email', 'postmark_headers_for_gravityforms', 10, 4 );

/**
 * Postmark Notification Settings.
 *
 * @param [type] $ui_settings UI Settings.
 * @param [type] $notification Notification.
 * @param [type] $form Form.
 */
function postmark_notification_setting( $ui_settings, $notification, $form ) {

    $ui_settings['postmark_tag'] = '
        <tr>
            <th><label for="postmark-tag">Postmark - Tag</label></th>
            <td><input value="' . rgar( $notification, 'postmark_tag' ) . '" name="postmark_tag"></td>
        </tr>';


    return $ui_settings;
}
add_filter( 'gform_notification_ui_settings', 'postmark_notification_setting', 10, 3 );

/**
 * Postmark Notification Save.
 *
 * @param [type] $notification Notification.
 * @param [type] $form Form.
 */
function postmark_notification_save( $notification, $form ) {
    $notification['postmark_tag'] = rgpost( 'postmark_tag' );
    return $notification;
}
add_filter( 'gform_pre_notification_save', 'postmark_notification_save', 10, 2 );

add_filter( 'gform_pre_send_email', function ( $email, $message_format, $notification ) {
    if( ! empty( $notification['postmark_tag'] ) ) {
    	$email['headers']['X-PM-Tag'] = 'X-PM-Metadata-tag: ' . $notification['postmark_tag'];
	}

    return $email;
}, 10, 3 );

All code snippets are licensed GPLv2 or later unless otherwise stated.