Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP Mail Error/Exception handling and SMTP settings
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: (WCM) PHPMailer Exceptions & SMTP
* Description: WordPress by default returns <code>FALSE</code> instead of an <code>Exception</code>. This plugin fixes that.
*/
add_action( 'phpmailer_init', 'WCMphpmailerException' );
function WCMphpmailerException( $phpmailer )
{
if ( ! defined( 'WP_DEBUG' ) OR ! WP_DEBUG )
{
$phpmailer->SMTPDebug = 0;
$phpmailer->debug = 0;
return;
}
if ( ! current_user_can( 'manage_options' ) )
return;
// Enable SMTP
# $phpmailer->IsSMTP();
$phpmailer->SMTPDebug = 2;
$phpmailer->debug = 1;
$data = apply_filters(
'wp_mail',
compact( 'to', 'subject', 'message', 'headers', 'attachments' )
);
// Show what we got
current_user_can( 'manage_options' )
AND print htmlspecialchars( var_export( $phpmailer, true ) );
$error = null;
try
{
$sent = $phpmailer->Send();
! $sent AND $error = new WP_Error( 'phpmailer-error', $sent->ErrorInfo );
}
catch ( phpmailerException $e )
{
$error = new WP_Error( 'phpmailer-exception', $e->errorMessage() );
}
catch ( Exception $e )
{
$error = new WP_Error( 'phpmailer-exception-unknown', $e->getMessage() );
}
if ( is_wp_error( $error ) )
return printf(
"%s: %s",
$error->get_error_code(),
$error->get_error_message()
);
}
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: (WCM) PHPMailer SMTP Settings
* Description: Enables SMTP servers, SSL/TSL authentication and SMTP settings.
*/
add_action( 'phpmailer_init', 'phpmailerSMTP' );
function phpmailerSMTP( $phpmailer )
{
# $phpmailer->IsSMTP();
# $phpmailer->SMTPAuth = true; // Authentication
# $phpmailer->Host = '';
# $phpmailer->Username = '';
# $phpmailer->Password = '';
# $phpmailer->SMTPSecure ='ssl'; // enable if required, 'tls' is another possible value
# $phpmailer->Host = ''; // SMTP Host
# $phpmailer->Port = 26; // SMTP Port
}
@userabuser

This comment has been minimized.

Copy link

commented Jun 22, 2013

+1 - this should be in core @WordPress

@renoirb

This comment has been minimized.

Copy link

commented Feb 13, 2014

+1 to be in the core!

@mrizwanghuman

This comment has been minimized.

Copy link

commented Oct 13, 2016

Nice

@jfernandez5

This comment has been minimized.

Copy link

commented Jul 7, 2017

Hello, i'm new in wordpress , can you tell me where i should put this file and how load it please? (wpmail_exceptions.php)

@Inigovd

This comment has been minimized.

Copy link

commented Oct 26, 2017

Hey, the following line is included twice:
$phpmailer->Host = '';

Nice gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.