Created
October 22, 2022 09:49
-
-
Save danieliser/c60aba2609599598089da133e812ad8a to your computer and use it in GitHub Desktop.
Allow overloading WP email templates with custom wrappers and dynamic data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_custom_email_template() { | |
ob_start(); | |
include __DIR__ . '/index.php'; | |
$content = ob_get_clean(); | |
ob_end_clean(); | |
return $content; | |
} | |
function custom_wp_mail_atts( $atts ) { | |
$headers = [ | |
'Content-type: text/html; charset=UTF-8', | |
]; | |
$template_tags = [ | |
'[title]' => $atts['subject'], | |
'[subject]' => $atts['subject'], | |
'[content]' => $atts['message'], | |
'[copyright]' => sprintf( "© %d %s, All rights reserved", date( 'Y' ), get_bloginfo( 'name' ) ), | |
]; | |
$content = get_custom_email_template(); | |
foreach( $template_tags as $tag => $value ) { | |
$content = str_replace( array_keys( $template_tags ), array_values( $template_tags ), $content ); | |
} | |
$atts["message"] = $content; | |
$atts['headers'] = is_array( $atts['headers'] ) ? | |
array_merge( $atts['headers'], $headers ) : | |
$headers; | |
return $atts; | |
} | |
add_filter( 'wp_mail', 'custom_wp_mail_atts', 10 ); | |
function custom_email_template_attachments( &$phpmailer) { | |
$template_images = [ | |
'logo' => 'logo.png', | |
'shadow' => 'shadow.png', | |
]; | |
$used_tags = | |
$phpmailer->SMTPKeepAlive = true; | |
foreach( $template_images as $uid => $file ) { | |
if ( is_file( __DIR__ . '/img/' . $file ) ) { | |
$phpmailer->AddEmbeddedImage( | |
__DIR__ . '/img/' . $file, | |
$uid, | |
$file | |
); | |
} | |
} | |
} | |
add_action( 'phpmailer_init', 'custom_email_template_attachments' ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width,initial-scale=1"> | |
<meta name="x-apple-disable-message-reformatting"> | |
<title>[title]</title> | |
<!--[if mso]> | |
<noscript> | |
<xml> | |
<o:OfficeDocumentSettings> | |
<o:PixelsPerInch>96</o:PixelsPerInch> | |
</o:OfficeDocumentSettings> | |
</xml> | |
</noscript> | |
<![endif]--> | |
<style> | |
table, td, div, h1, p {font-family: Arial, sans-serif;} | |
</style> | |
</head> | |
<body style="margin:0;padding:0;"> | |
<table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0;background:#ffffff;"> | |
<tr> | |
<td align="center" style="padding:0;"> | |
<img src="cid:logo" width="300" /> | |
[content] | |
<img src="cid:shadow" width="300" /> | |
</td> | |
</tr> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment