Advertisement
daymobrew

WordPress: Log Emails for Debugging

Jun 23rd, 2016
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Log Emails for Debugging
  4. Plugin URI: http://www.damiencarbery.com
  5. Description: Log emails sent.
  6. Author: Damien Carbery
  7. Version: 0.4
  8.  
  9. $Id: wp-mail-logging.php 3641 2016-04-15 17:48:53Z damien $
  10. */
  11.  
  12. add_filter( 'wp_mail', 'dbg_log_email_info' );
  13. function dbg_log_email_info( $params )
  14. {
  15.     // If $params['to'] email is an array then implode it into a string.
  16.     if (is_array($params['to'])) {
  17.         $to = implode(', ', $params['to']);
  18.     }
  19.     else {
  20.         $to = $params['to'];
  21.     }
  22.     $log_info = sprintf('To: %s | Subject: %s | Message: %s | Headers: %s', $to, $params['subject'], var_export($params['message'], true), var_export($params['headers'], true));
  23.    
  24.     // Write to/from/subject of email to a log.
  25.     if (function_exists('debug2log')) {
  26.         debug2log($log_info);
  27.     }
  28.     else {
  29.         error_log($log_info);
  30.     }
  31.     return $params;
  32. }
  33.  
  34.  
  35. // From email address is not sent in 'wp_mail' filter so log it via another filter.
  36. add_filter( 'wp_mail_from', 'dbg_log_email_from' );
  37. function dbg_log_email_from( $from ) {
  38.   if (function_exists('debug2log')) {
  39.     debug2log(sprintf('Email from:%s', $from));
  40.   }
  41.   else {
  42.     error_log(sprintf('Email from:%s', $from));
  43.   }
  44.     return $from;
  45. }
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement