ActiveCampaign / postmark-wordpress

The Official Postmark Wordpress Plugin
GNU General Public License v2.0
17 stars 16 forks source link

Tag Default WordPress and WooCommerce Emails #49

Open bhubbard opened 5 years ago

bhubbard commented 5 years ago

I would love it if we could have all the default emails get Postmark Tag Support. So for example the New User Email could get a tag like 'wp-new-user' or something similar to that. Here is a good list I found of all the default WordPress emails.

https://github.com/johnbillion/wp_mail

Being able to filter these emails by tags in Postmark would be extremely useful.

bhubbard commented 4 years ago

@pgraham3 I have a snippet that will tag to WooCommerce Emails. Not sure where best to place the code in the plugin.

/**
 * Postmark Headers for WooCommerce.
 *
 * @param  [type] $headers  Headers.
 * @param  [type] $email_id Email ID.
 * @param  [type] $order    Order.
 */
function postmark_headers_for_woocommerce( $headers, $email_id, $order ) {

    $order_id = $order->get_order_number() ?? '';

    if ( ! empty( $order_id ) ) {
        $headers .= 'X-PM-Metadata-order-id: ' . $order_id . "\r\n";
    }
        $headers .= 'X-PM-Tag: woocommerce_' . $email_id . "\r\n";

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'postmark_headers_for_woocommerce', 10, 3 );
bhubbard commented 4 years ago

Here is one for Gravity Forms:

/**
 * [postmark_headers_for_gravityforms description]
 * @param  [type] $email          [description]
 * @param  [type] $message_format [description]
 * @param  [type] $notification   [description]
 * @return [type]                 [description]
 */
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-Tag: gravity-forms';

     return $email;

}

add_filter( 'gform_pre_send_email', 'postmark_headers_for_gravityforms', 10, 4 );
pgraham3 commented 4 years ago

@bhubbard Love the idea.

We don't want to be forcing default tags for anyone but I do think the plugin should support adding a Tag in general. Then you could use your filters above to set the X-PM-Tag header you wish to use and it will be present on the sent email.

Can you give this PR a try and let me know if it meets your needs for setting a Tag for your messages?

https://github.com/wildbit/postmark-wordpress/pull/67

After this looks good we can look into adding metadata support as well, which will be a bit more effort since the key for the metadata headers is variable (though it always starts with X-PM-Metadata-).