Skip to content

Instantly share code, notes, and snippets.

@mthchz
Last active July 21, 2017 09:48
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 mthchz/ae8b1285b22fd285a855dd33fdfd60cf to your computer and use it in GitHub Desktop.
Save mthchz/ae8b1285b22fd285a855dd33fdfd60cf to your computer and use it in GitHub Desktop.
WP Mail force SMTP on send error
<?php
/**
* Force WP_Mail with SMTP
* Dependency : https://github.com/rohmann/global-smtp
*/
function WPMailOnSendFail($wp_error){
$GlobalSMTP = false;
if( file_exists( dirname(__FILE__).'/global-smtp.php' ) ){
include_once( dirname(__FILE__).'/global-smtp.php' );
Global_SMTP_Mailer::launch();
// If Global SMTP is activated and configurated
if((defined('GLOBAL_SMTP_HOST') || defined('GLOBAL_SMTP_USER') || defined('GLOBAL_SMTP_PASSWORD')) && GLOBAL_SMTP_DISABLE !== true){
$GlobalSMTP = true;
}
}
$mail_meta = $wp_error->get_error_data();
$user_email = $mail_meta['to'];
$subject = $mail_meta['subject'];
$message = $mail_meta['message'];
$message_headers = $mail_meta['headers'];
$attachments = $mail_meta['attachments'];
// Try a new send with wp_mail and Global SMTP
if( $GlobalSMTP ){
$wp_mail = wp_mail($user_email, $subject, $message, $message_headers);
}
// If wp_mail fail again or no Global SMTP installed, try to send with php mail()
// Not the best way and can be SPAM
if(!$GlobalSMTP || !$wp_mail) {
$user_email_string = '';
for ($i=0; $i <= count($user_email) ; $i++) {
$user_email_string .= $user_email[i].',';
}
mail($user_email_string, $subject, $message, $message_headers);
}
}
add_action( 'wp_mail_failed', 'WPMailOnSendFail', 1, 10 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment