Skip to content

Instantly share code, notes, and snippets.

@tareq1988
Created September 12, 2017 14:37
Show Gist options
  • Save tareq1988/c8c009b2cdb5acd3857d821c3c5051d5 to your computer and use it in GitHub Desktop.
Save tareq1988/c8c009b2cdb5acd3857d821c3c5051d5 to your computer and use it in GitHub Desktop.
WordPress Email Log to File
<?php
/**
* Plugin Name: Email Log
* Plugin URI: https://tareq.co
* Description: Log all outgoing emails to <code>wp-content/uploads/emails/DATE.log</code> file
* Author: Tareq Hasan
* Author URI: https://tareq.co
* Version: 1.0
*/
add_filter( 'wp_mail', function( $args ) {
$current_time = current_time( 'timestamp' );
$upload_dir = wp_upload_dir();
$log_folder = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'emails';
$log_file = $log_folder . DIRECTORY_SEPARATOR . date( 'd_m_Y', $current_time ) . '.log';
// create the emails folder if not exists
if ( ! is_dir( $log_folder ) ) {
mkdir( $log_folder );
}
$subject = $args['subject'];
$mail_to = is_array( $args['to'] ) ? implode( ',', $args['to'] ) : $args['to'];
$message = sprintf( "[%s][%s] %s\n", date( 'h:i:s', $current_time ), $mail_to, $subject );
error_log( $message, 3, $log_file );
return $args;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment