Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active April 15, 2017 21:03
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 damiencarbery/f2e0dd1c8259ba587dfde8554ec58524 to your computer and use it in GitHub Desktop.
Save damiencarbery/f2e0dd1c8259ba587dfde8554ec58524 to your computer and use it in GitHub Desktop.
Log WordPress emails to aid debugging.
<?php
/*
Plugin Name: Log Emails for Debugging
Plugin URI: http://www.damiencarbery.com
Description: Log emails sent.
Author: Damien Carbery
Version: $Revision: 4213 $
$Id: log-emails-for-debugging.php 4213 2017-04-11 20:50:44Z damien $
*/
add_filter( 'wp_mail', 'dbg_log_email_info' );
function dbg_log_email_info( $params )
{
// If $params['to'] email is an array then implode it into a string.
if (is_array($params['to'])) {
$to = implode(', ', $params['to']);
}
else {
$to = $params['to'];
}
$log_info = sprintf('To: %s | Subject: %s | Headers: %s | Message: %s', $to, $params['subject'], var_export($params['headers'], true), var_export($params['message'], true));
// Write to/from/subject of email to a log.
if (function_exists('debug2log')) {
debug2log($log_info);
}
else {
error_log($log_info);
}
// Return $params unchanged.
return $params;
}
// From email address is not sent in 'wp_mail' filter so log it via another filter.
add_filter( 'wp_mail_from', 'dbg_log_email_from' );
function dbg_log_email_from( $from ) {
if (function_exists('debug2log')) {
debug2log(sprintf('Email from:%s', $from));
}
else {
error_log(sprintf('Email from:%s', $from));
}
// Return $from unchanged.
return $from;
}
?>
<?php
/*
Plugin Name: Send Test Email
Plugin URI: http://www.damiencarbery.com
Description: Send a test email.
Author: Damien Carbery
Version: 0.1
*/
add_action( 'wp_footer', 'ste_send_test_email' );
function ste_send_test_email() {
wp_mail( 'email@yahoo.com', 'Test from the client site', 'This is a simple test email.' );
}
<?php
/*
Plugin Name: Send Test HTML Email
Plugin URI: http://www.damiencarbery.com
Description: Send a test html email.
Author: Damien Carbery
Version: 0.1
*/
add_action( 'wp_footer', 'sthe_send_test_html_email' );
function sthe_send_test_html_email() {
$message = '<!DOCTYPE html>
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test from the client site</title>
</head>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
<p>This is <strong>HTML</strong></p>
</body>
</html>
';
add_filter( 'wp_mail_content_type', 'sthe_set_html_content_type' );
$headers[] = 'From: Client Website <wordpress@client_site_domain.ie>';
$to = 'email@yahoo.com';
wp_mail( $to, 'Test from client site', $message );
// Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'sthe_set_html_content_type' );
}
function sthe_set_html_content_type() {
return 'text/html';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment