Skip to content

Commit

Permalink
2.0.13
Browse files Browse the repository at this point in the history
* Fixed: WordPress 5.5 compatibility
* Fixed: Email log filtering
* Fixed: Pushover notifications
* New: Suggest solution for email delivery errors
  • Loading branch information
smusman98 committed Jul 18, 2022
1 parent 028fa70 commit 59c5658
Show file tree
Hide file tree
Showing 24 changed files with 481 additions and 269 deletions.
4 changes: 4 additions & 0 deletions Postman/Extensions/Admin/PostmanAdminView.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class="regular-text"
<button type="submit" name="post_smtp_extension[<?php echo $short_name; ?>_activate]" class="button button-primary">Activate</button>
<?php endif; ?>

<?php if ( $license_data->license === 'expired' ) : ?>
<a href="<?php echo $license_data->renew_url; ?>" target="_blank" class="button button-primary">Renew License</a>
<?php endif; ?>

<button type="submit" name="post_smtp_extension[<?php echo $short_name; ?>_deactivate]" class="button button-secondary">Deactivate</button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PostmanPushoverNotify implements Postman_Notify {

public function send_message($message)
{
$options = PostmanOptions::getInstance();
$options = PostmanNotifyOptions::getInstance();

$api_url = "https://api.pushover.net/1/messages.json";
$app_token = $options->getPushoverToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public function send_message($message)
error_log( __CLASS__ . ': ' . $message );
}
}
}
}
56 changes: 56 additions & 0 deletions Postman/Extensions/Core/StatusSolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

class StatusSolution {

private $status;

public function __construct() {
add_filter( 'post_smtp_log_solution', array( $this, 'find_solution' ), 10, 4 );
}

public function find_solution( $solution, $status, $log, $message ) {

if ( empty( $status ) ) {
return 'All good, mail sent.';
}

$this->status = addslashes( $status );
$possible_solution = [];

if ( $this->strExists('timed out') ) {
$possible_solution[] = $this->make_clickable('https://postmansmtp.com/office365-smtp-connection-timed-out/');
} elseif ( $this->strExists('timeout') || $this->strExists('open socket' ) ) {
$possible_solution[] = 'Your hosting is blocking the connection, contact their support';
} elseif ( $this->strExists( 'DATA NOT ACCEPTED' ) || $this->strExists('Exception:SendAsDeniedException' ) ) {
$possible_solution[] = $this->make_clickable('https://postmansmtp.com/storedrv-submission-exceptionsendasdeniedexception-mapiexceptionsendasdenied/');
} elseif ( $this->strExists( 'Incorrect authentication data') ) {
$possible_solution[] = $this->make_clickable( 'https://postmansmtp.com/incorrect-authentication-data/' );
} elseif ( $this->strExists( 'Unrecognized authentication type' ) ) {
$possible_solution[] = 'Change "Authentication" type on plugin settings to "Login"';
} elseif ( $this->strExists( 'Error executing "SendRawEmail"' ) ) {
$possible_solution[] = 'Amazon SES - account permission error (review account configuration)';
} elseif ( $this->strExists( 'Please log in via your web browser and then try again' ) ) {
$possible_solution[] = $this->make_clickable( 'https://postmansmtp.com/gmail-gsuite-please-log-in-via-your-web-browser-and-then-try-again/' );
} elseif ( $this->strExists( 'Application-specific password required' ) ) {
$possible_solution[] = 'Two factor authentication is enabled, replace your password with app password.';
$possible_solution[] = $this->make_clickable( 'https://support.google.com/mail/?p=InvalidSecondFactor' );
} elseif ( $this->strExists( 'Username and Password not accepted' ) || $this->strExists( 'Authentication unsuccessful' ) ) {
$possible_solution[] = 'Check you credentials, wrong email or password.';
} else {
$possible_solution[] = 'Not found, check status column for more info.';
}

return ! empty( $possible_solution ) ? implode( '<br>', $possible_solution ) : '';
}

private function make_clickable($url) {
return '<a target="_blank" href="' . esc_url($url ) . '">' . esc_html( 'Read here' ) . '</a>';
}

private function strExists( $value ) {
return strpos( strtolower( $this->status ), strtolower( addslashes( $value ) ) ) !== false;
}

}

new StatusSolution();
55 changes: 49 additions & 6 deletions Postman/Extensions/License/PostmanLicenseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@


class PostmanLicenseHandler {

const DAYS_TO_ALERT = array( 30, 14, 7, 1);

private $file;
private $license;
private $license_data;
Expand Down Expand Up @@ -81,7 +84,6 @@ public function hooks() {

add_action( 'init', array( $this, 'cron' ), 20 );

// Check that license is valid once per week
add_action( 'admin_init', array( $this, 'validate_license' ) );

// Updater
Expand Down Expand Up @@ -241,9 +243,9 @@ public function activate_license() {
set_site_transient( 'update_plugins', null );

// Decode license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
$this->license_data = json_decode( wp_remote_retrieve_body( $response ) );

update_option( $this->item_shortname . '_license_active', $license_data );
update_option( $this->item_shortname . '_license_active', $this->license_data );
update_option( $this->item_shortname . '_license_key', $license );

$slug = plugin_basename($this->file);
Expand Down Expand Up @@ -307,7 +309,7 @@ public function deactivate_license() {
}

// Decode the license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
$this->license_data = json_decode( wp_remote_retrieve_body( $response ) );

delete_option( $this->item_shortname . '_license_active' );
delete_option( $this->item_shortname . '_license_key' );
Expand All @@ -323,6 +325,39 @@ public function validate_license() {

set_transient( $this->item_shortname . '_cron', true, rand( 12, 48 ) * HOUR_IN_SECONDS );
}

$license_data = $this->license_data;

$datetime1 = new DateTime();
$datetime2 = new DateTime( $license_data->expires );

foreach ( self::DAYS_TO_ALERT as $day_to_alert ) {

$interval = $datetime1->diff($datetime2);
if( $interval->days == $day_to_alert ){
add_action( 'admin_notices', function () use ( $day_to_alert, $license_data ) {
echo $this->item_name . ' is about to expire in ' . $day_to_alert . ' days: ' . $license_data->expires;
});

return;
}

if ( $interval->days == 0 ) {
add_action( 'admin_notices', function () use ( $license_data ) {
echo $this->item_name . ' license expire today at: ' . $license_data->expires;
});

return;
}
}

if ( $license_data->activations_left == 0 ) {
add_action( 'admin_notices', function () use ( $license_data ) {
echo $this->item_name . ' has no activations';
});

return;
}
}


Expand Down Expand Up @@ -357,10 +392,18 @@ public function license_check() {
return false;
}

$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
return false;
}

$this->license_data = json_decode( wp_remote_retrieve_body( $response ) );

update_option( $this->item_shortname . '_license_active', $license_data );
update_option( $this->item_shortname . '_license_active', $this->license_data );

}

private function get_license_data() {
return get_option( $this->item_shortname . '_license_active' );
}


Expand Down
6 changes: 2 additions & 4 deletions Postman/Extensions/License/PostmanLicenseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class PostmanLicenseManager {

const ENDPOINT = 'https://postmansmtp.com';
const ENDPOINT = 'https://postmansmtpcom-staging.dxpsites.net';

const CORE_EXTENSIONS = [ 'gmail_api', 'sendgrid_api', 'mandrill_api', 'mailgun_api' ];

Expand Down Expand Up @@ -68,11 +68,9 @@ public function init() {
$this->extensions[$slug]['plugin_dir_and_filename'] = $plugin_dir_and_filename;
$this->extensions[$slug]['license_manager'] = new PostmanLicenseHandler(
$plugin_path, $plugin_data['Name'],
$plugin_data['Version'], $plugin_data['Author']
$plugin_data['Version'], $plugin_data['Author'], null, self::ENDPOINT
);
if ( $this->extensions[$slug]['license_manager']->is_licensed() ) {
include_once $plugin_path;

$this->extensions[$slug]['instance'] = new $class;
}
}
Expand Down
159 changes: 159 additions & 0 deletions Postman/Phpmailer/PostsmtpMailer-old.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

if ( ! class_exists( 'PHPMailer', false ) ) {
require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

add_action('plugins_loaded', function() {
global $phpmailer;

$phpmailer = new PostsmtpMailer(true);
});

class PostsmtpMailer extends PHPMailer {

private $mail_args = array();

private $options;

private $error;

private $transcript = '';

public function __construct($exceptions = null)
{
parent::__construct($exceptions);

$this->set_vars();
$this->hooks();

}

public function set_vars() {
$this->options = PostmanOptions::getInstance();
$this->Debugoutput = function($str, $level) {
$this->transcript .= $str;
};
}

public function hooks() {
add_filter( 'wp_mail', array( $this, 'get_mail_args' ) );
if ( $this->options->getTransportType() == 'smtp' ) {
add_action( 'phpmailer_init', array( $this, 'phpmailer_smtp_init' ), 999 );
}
}

public function get_mail_args( $atts ) {
$this->mail_args = array();
$this->mail_args[] = $atts['to'];
$this->mail_args[] = $atts['subject'];
$this->mail_args[] = $atts['message'];
$this->mail_args[] = $atts['headers'];
$this->mail_args[] = $atts['attachments'];

return $atts;
}

/**
* @param PHPMailer $mail
*/
public function phpmailer_smtp_init($mail) {
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = $this->options->getHostname();

if ( $this->options->getAuthenticationType() !== 'none' ) {
$mail->SMTPAuth = true;
$mail->Username = $this->options->getUsername();
$mail->Password = $this->options->getPassword();
}

if ( $this->options->getEncryptionType() !== 'none' ) {
$mail->SMTPSecure = $this->options->getEncryptionType();
}

$mail->Port = $this->options->getPort();

if ( $this->options->isPluginSenderEmailEnforced() ) {
$mail->setFrom( $this->options->getMessageSenderEmail() , $this->options->getMessageSenderName () );
}
}

public function send()
{
require_once dirname(__DIR__) . '/PostmanWpMail.php';

// create a PostmanWpMail instance
$postmanWpMail = new PostmanWpMail();
$postmanWpMail->init();

list($to, $subject, $body, $headers, $attachments) = array_pad( $this->mail_args, 5, null );

// build the message
$postmanMessage = $postmanWpMail->processWpMailCall( $to, $subject, $body, $headers, $attachments );

// build the email log entry
$log = new PostmanEmailLog();
$log->originalTo = $to;
$log->originalSubject = $subject;
$log->originalMessage = $body;
$log->originalHeaders = $headers;

// get the transport and create the transportConfig and engine
$transport = PostmanTransportRegistry::getInstance()->getActiveTransport();

add_filter( 'postman_wp_mail_result', [ $this, 'postman_wp_mail_result' ] );

try {

if ( $send_email = apply_filters( 'post_smtp_do_send_email', true ) ) {
$result = $this->options->getTransportType() !== 'smtp' ?
$postmanWpMail->send( $to, $subject, $body, $headers, $attachments ) :
$this->sendSmtp();
}


do_action( 'post_smtp_on_success', $log, $postmanMessage, $this->transcript, $transport );

return $result;

} catch (Exception $exc) {

$this->error = $exc;

$this->mailHeader = '';

$this->setError($exc->getMessage());
if ($this->exceptions) {
throw $exc;
}
return false;
}

}

public function sendSmtp() {
if (!$this->preSend()) {
return false;
}
return $this->postSend();
}


public function postman_wp_mail_result() {
$result = [
'time' => '',
'exception' => $this->error,
'transcript' => $this->transcript,
];
return $result;
}
}
Loading

0 comments on commit 59c5658

Please sign in to comment.