Skip to content

Instantly share code, notes, and snippets.

@idokd
Created September 19, 2021 15:57
Show Gist options
  • Select an option

  • Save idokd/d49d6637ab03451d23e15c21c99f7f5d to your computer and use it in GitHub Desktop.

Select an option

Save idokd/d49d6637ab03451d23e15c21c99f7f5d to your computer and use it in GitHub Desktop.
WordPress + Mailgun plugin - Apply Template to all WordPress mail
<?php
/*
This code with conjunction of Wordpress Mailgun plugin will force all wordpress mails to apply mailgun wordpress to all mail,
This could be easy way to beautify emails.
use {{subject}} {{text}} or {{{html}}} - see that html has triple {{{ in order to use raw content and not escape special chars
*/
add_filter( 'mg_mutate_message_body', function( $body ) {
$body[ 'template' ] = 'wordpress';
$vars = [];
$vars[ 'subject' ] = $body[ 'subject' ];
$vars[ 'text' ] = $body[ 'text' ];
$vars[ 'html' ] = !isset( $body[ 'html' ] ) || !$body[ 'html' ] ? nl2br( $body[ 'text' ] ) : null;
$vars[ 'body' ] = isset( $vars[ 'html' ] ) && $vars[ 'html' ] ? $vars[ 'html' ] : $vars[ 'text' ];
$body[ 'h:X-Mailgun-Variables' ] = json_encode( $vars );
return( $body );
}, 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment