Using Gravity Forms ‘gform_html_message_template_pre_send_email’ PHP action

The gform_html_message_template_pre_send_email Gravity Forms PHP filter allows you to override the template for the HTML-formatted message.

Usage

add_filter( 'gform_html_message_template_pre_send_email', 'custom_html_message_template', 10, 1 );
function custom_html_message_template( $template ) {
    // Your custom code here

    return $template;
}

Parameters

  • $template (string): The template for the HTML-formatted message. Use {message} and {subject} as placeholders.

More information

See Gravity Forms Docs: gform_html_message_template_pre_send_email

Examples

Customize the email template by adding a custom header and footer.

add_filter( 'gform_html_message_template_pre_send_email', 'add_custom_header_footer' );
function add_custom_header_footer( $template ) {
    $header = '<div style="background-color: #f5f5f5; padding: 20px;">Your Custom Header</div>';
    $footer = '<div style="background-color: #f5f5f5; padding: 20px;">Your Custom Footer</div>';

    $template = str_replace( '{message}', $header . '{message}' . $footer, $template );

    return $template;
}

Change the email background color

Update the background color of the email template.

add_filter( 'gform_html_message_template_pre_send_email', 'change_email_background_color' );
function change_email_background_color( $template ) {
    $template = str_replace( 'background-color:#FFFFFF;', 'background-color:#F0E68C;', $template );

    return $template;
}

Apply custom CSS to the email template

Add custom CSS styles to the email template.

add_filter( 'gform_html_message_template_pre_send_email', 'apply_custom_css' );
function apply_custom_css( $template ) {
    $custom_css = '<style>
        body {
            font-family: Arial, sans-serif;
            font-size: 14px;
            line-height: 1.5;
        }
        .custom_class {
            color: #333;
        }
    </style>';

    $template = $custom_css . $template;

    return $template;
}

Add an unsubscribe link to the email footer.

add_filter( 'gform_html_message_template_pre_send_email', 'add_unsubscribe_link' );
function add_unsubscribe_link( $template ) {
    $unsubscribe_link = '<p style="text-align:center;"><a href="https://your-domain.com/unsubscribe">Unsubscribe</a></p>';

    $template = str_replace( '</body>', $unsubscribe_link . '</body>', $template );

    return $template;
}

Wrap the email content with a custom container

Add a custom container around the email content.

add_filter( 'gform_html_message_template_pre_send_email', 'wrap_email_content_with_container' );
function wrap_email_content_with_container( $template ) {
    $start_container = '<div style="max-width: 600px; margin: 0 auto;">';
    $end_container = '</div>';

    $template = str_replace( '{message}', $start_container . '{message}' . $end_container, $template );

    return $template;
}