Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active September 5, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/bb78aeccc1650ee4a1043478d53100db to your computer and use it in GitHub Desktop.
Save xnau/bb78aeccc1650ee4a1043478d53100db to your computer and use it in GitHub Desktop.
Shows how to wrap an outgoing Participants Database email with an HTML framework
<?php
/*
* Plugin Name: PDb HTML Email Wrapper
* Description: Adds a structural HTML wrapper to outgoing Participants Database emails.
*/
class pdb_email_html_wrapper {
/**
*
*/
public function __construct()
{
// this filter is called in PDb_Template_Email::send_email()
add_filter( 'pdb-template_email_html_body', array($this, 'wrap_email_body'), 20, 2 );
}
/**
* wraps the email body of all Participants Database outgoing emails
*
* @param string $body the email body
* @param \PDb_Template_Email $template instance
* @return string the email body
*/
public function wrap_email_body( $body, $template )
{
return str_replace( '{body}', $body, $this->wrapper() );
}
/**
* provides the html wrapper
*
* this wrapper must contain the {body} placeholder where the unwrapped email body goes
*
* this wrapper is based on an article in emailonacid.com
*
* @see https://www.emailonacid.com/blog/article/email-development/which-code-should-i-include-in-every-email/
* @return string
*/
private function wrapper()
{
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="margin:0;padding:0;min-width:100%;background-color:#ffffff;">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="min-width: 100%;" role="presentation">
<tr>
<td>
{body}
</td>
</tr>
</table>
</body>
</html>
<?php
return ob_get_clean();
}
}
// invoke the class
new pdb_email_html_wrapper;
@xnau
Copy link
Author

xnau commented Sep 5, 2020

This will wrap the body of all Participants Database emails in a recommended HTML framework.

This is really only necessary to address layout issues with inconsistent CSS implementation across email clients. It has no effect on deliverability.

If you want to be selective about which emails get this treatment, inspect the $template object in the filter handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment