Skip to content

Instantly share code, notes, and snippets.

@valerio-bozzolan
Last active October 20, 2016 10:19
Show Gist options
  • Save valerio-bozzolan/0a6486057e1c863e4baa07192f67740c to your computer and use it in GitHub Desktop.
Save valerio-bozzolan/0a6486057e1c863e4baa07192f67740c to your computer and use it in GitHub Desktop.
Why incorporate other-people SMTP code when you have PEAR installed via package manager? Add this as a plugin.
<?php
/*
Plugin Name: Debianatore Mailoso di Valerio
Plugin URI: https://wordpress.org/support/topic/the-e-mail-could-not-be-sent/#post-179327
Description: Send SMTP mails with an include "Net/SMTP.php";... simple! #nocrap
Author: Valerio Bozzolan
Author URI: https://boz.reyboz.it
Version: 0000000.0.0.-1.0
License: GPLv3 or later
*/
if( ! function_exists('wp_mail') ):
function error_debianatore_mailoso($title, $asd) {
if( ! DEBUG ) {
$asd = "Enable DEBUG to show";
}
printf(
"<p>Debianatore Mailoso. %s: %s.</p>\n",
$title,
esc_html( $asd )
);
}
function wp_mail($to, $subject, $message, $additional_headers = "", $more = "") {
require_once "Net/SMTP.php";
// Force array
if( ! is_array( $to ) ) {
$to = [ $to ];
}
$socket_options = [
'ssl' => [
'verify_peer_name' => false,
'verify_peer' => false
]
];
if( ! ($smtp = new Net_SMTP(DEBIANATORE_MAILOSO_HOST, DEBIANATORE_MAILOSO_PORT, null, false, 0, $socket_options)) ) {
error_debianatore_mailoso(
"Unable to instantiate Net_SMTP object",
$smtp->getUserInfo()
);
return false;
}
WP_DEBUG && $smtp->setDebug(true);
if( PEAR::isError($e = $smtp->connect()) ) {
error_debianatore_mailoso(
"Error connect",
$e->getMessage()
);
return false;
}
if( PEAR::isError($e = $smtp->auth(DEBIANATORE_MAILOSO_FROM, DEBIANATORE_MAILOSO_PASS, DEBIANATORE_MAILOSO_AUTH, true, '', true) ) ) {
error_debianatore_mailoso(
"Error auth",
$e->getMessage()
);
return false;
}
if( PEAR::isError( $smtp->mailFrom(DEBIANATORE_MAILOSO_FROM) ) ) {
error_debianatore_mailoso(
"Error set from",
$res->getMessage()
);
return false;
}
foreach($to as $i => $single_to) {
if( filter_var($single_to, FILTER_VALIDATE_EMAIL) === false ) {
unset( $to[$i] );
error_debianatore_mailoso(
"Wrong e-mail address stripped out",
$single_to
);
continue;
}
if( PEAR::isError($res = $smtp->rcptTo($single_to)) ) {
error_debianatore_mailoso(
"Error set To",
$res->getMessage()
);
return false;
}
}
if( count($to) === 0 ) {
error_debianatore_mailoso("No email sent", "no addresses");
return false;
}
$headers = [
"MIME-Version" => "1.0",
"Subject" => "$subject",
"To" => implode(',', $to),
"From" => sprintf(
"%s <%s>",
get_bloginfo('name'),
DEBIANATORE_MAILOSO_FROM
),
"Content-Type" => sprintf(
"text/plain;charset=%s",
get_settings('blog_charset')
),
"X-Mailer" => "Net/SMTP.php tramite plugin WP debianatore mailoso"
];
$merge = [];
foreach($headers as $header=>$value) {
$value = trim($value);
$merge[] = sprintf("%s: %s", $header, $value);
}
$headers = $additional_headers . implode("\r\n", $merge);
$error = PEAR::isError($smtp->data("$headers\r\n$message"));
$smtp->disconnect();
return ! $error;
}
endif;
add_filter( 'https_local_ssl_verify', '__return_false' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment