From 18c7ae6216c4705a2942b07081469c4c0f1059fd Mon Sep 17 00:00:00 2001 From: yehuda Date: Mon, 4 Mar 2019 16:36:02 +0200 Subject: * Added support for Mailgun Europe region. * Replace "buggey" mime_content_type php function --- Postman/Postman-Mail/PostmanMailgunMailEngine.php | 9 +++++++- Postman/Postman-Mail/PostmanMailgunTransport.php | 21 ++++++++++++++--- Postman/Postman-Mail/PostmanSendGridMailEngine.php | 4 ++-- Postman/PostmanConfigTextHelper.php | 9 +++----- Postman/PostmanInputSanitizer.php | 1 + Postman/PostmanOptions.php | 7 ++++++ Postman/PostmanViewController.php | 2 +- postman-smtp.php | 5 ++-- readme.txt | 27 ++++++++-------------- 9 files changed, 52 insertions(+), 33 deletions(-) diff --git a/Postman/Postman-Mail/PostmanMailgunMailEngine.php b/Postman/Postman-Mail/PostmanMailgunMailEngine.php index 8258022..e874704 100644 --- a/Postman/Postman-Mail/PostmanMailgunMailEngine.php +++ b/Postman/Postman-Mail/PostmanMailgunMailEngine.php @@ -1,6 +1,8 @@ api_endpoint = ! is_null( $options->getMailgunRegion() ) ? 'https://api.eu.mailgun.net' : 'https://api.mailgun.net'; // add the Postman signature - append it to whatever the user may have set if ( ! $options->isStealthModeEnabled() ) { @@ -161,7 +165,10 @@ if ( ! class_exists( 'PostmanMailgunMailEngine' ) ) { $this->logger->debug( 'Sending mail' ); } - $mg = Mailgun::create( $this->apiKey ); + $configurator = new HttpClientConfigurator(); + $configurator->setEndpoint( $this->api_endpoint . '/v3/'. $this->domainName .'/messages'); + $configurator->setApiKey($this->apiKey); + $mg = Mailgun::configure($configurator); // Make the call to the client. $result = $this->processSend( $mg ); diff --git a/Postman/Postman-Mail/PostmanMailgunTransport.php b/Postman/Postman-Mail/PostmanMailgunTransport.php index 9a5e9d2..dcbc1ae 100644 --- a/Postman/Postman-Mail/PostmanMailgunTransport.php +++ b/Postman/Postman-Mail/PostmanMailgunTransport.php @@ -9,6 +9,7 @@ class PostmanMailgunTransport extends PostmanAbstractModuleTransport implements const SLUG = 'mailgun_api'; const PORT = 443; const HOST = 'api.mailgun.net'; + const EU_REGION = 'api.eu.mailgun.net'; const PRIORITY = 8000; const MAILGUN_AUTH_OPTIONS = 'postman_mailgun_auth_options'; const MAILGUN_AUTH_SECTION = 'postman_mailgun_auth_section'; @@ -43,7 +44,7 @@ class PostmanMailgunTransport extends PostmanAbstractModuleTransport implements * @return string */ public function getHostname() { - return self::HOST; + return ! is_null( $this->options->getMailgunRegion() ) ? self::EU_REGION : self::HOST; } /** * v0.2.1 @@ -128,10 +129,10 @@ class PostmanMailgunTransport extends PostmanAbstractModuleTransport implements $recommendation ['transport'] = self::SLUG; $recommendation ['hostname'] = null; // scribe looks this $recommendation ['label'] = $this->getName(); - if ( $hostData->hostname == self::HOST && $hostData->port == self::PORT ) { + if ( $hostData->hostname == $this->getHostname() && $hostData->port == self::PORT ) { $recommendation ['priority'] = self::PRIORITY; /* translators: where variables are (1) transport name (2) host and (3) port */ - $recommendation ['message'] = sprintf( __( ('Postman recommends the %1$s to host %2$s on port %3$d.') ), $this->getName(), self::HOST, self::PORT ); + $recommendation ['message'] = sprintf( __( ('Postman recommends the %1$s to host %2$s on port %3$d.') ), $this->getName(), $this->getHostname(), self::PORT ); } return $recommendation; } @@ -197,6 +198,11 @@ class PostmanMailgunTransport extends PostmanAbstractModuleTransport implements $this, 'mailgun_domain_name_callback', ), PostmanMailgunTransport::MAILGUN_AUTH_OPTIONS, PostmanMailgunTransport::MAILGUN_AUTH_SECTION ); + + add_settings_field( PostmanOptions::MAILGUN_REGION, __( 'Mailgun Europe Region?', Postman::TEXT_DOMAIN ), array( + $this, + 'mailgun_region_callback', + ), PostmanMailgunTransport::MAILGUN_AUTH_OPTIONS, PostmanMailgunTransport::MAILGUN_AUTH_SECTION ); } public function printMailgunAuthSectionInfo() { /* Translators: Where (1) is the service URL and (2) is the service name and (3) is a api key URL */ @@ -214,6 +220,11 @@ class PostmanMailgunTransport extends PostmanAbstractModuleTransport implements printf( '', null !== $this->options->getMailgunDomainName() ? esc_attr( $this->options->getMailgunDomainName() ) : '', __( 'Required', Postman::TEXT_DOMAIN ) ); } + function mailgun_region_callback() { + $value = $this->options->getMailgunRegion(); + printf( '', null !== $value ? ' checked' : '' ); + } + /** */ public function registerStylesAndScripts() { @@ -243,6 +254,10 @@ class PostmanMailgunTransport extends PostmanAbstractModuleTransport implements printf( '', __( 'Domain Name', Postman::TEXT_DOMAIN ) ); print '
'; print $this->mailgun_domain_name_callback(); + print '
'; + printf( '', __( 'Mailgun Europe Region?', Postman::TEXT_DOMAIN ) ); + print '
'; + print $this->mailgun_region_callback(); print ''; } } diff --git a/Postman/Postman-Mail/PostmanSendGridMailEngine.php b/Postman/Postman-Mail/PostmanSendGridMailEngine.php index b6082f9..06302b0 100644 --- a/Postman/Postman-Mail/PostmanSendGridMailEngine.php +++ b/Postman/Postman-Mail/PostmanSendGridMailEngine.php @@ -260,9 +260,10 @@ if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) { $file_name = basename( $file ); $file_parts = explode( '.', $file_name ); + $file_type = wp_check_filetype( $file ); $attachments[] = array( 'content' => base64_encode( file_get_contents( $file ) ), - 'type' => mime_content_type( $file ), + 'type' => $file_type['type'], 'file_name' => $file_name, 'disposition' => 'attachment', 'id' => $file_parts[0], @@ -279,4 +280,3 @@ if ( ! class_exists( 'PostmanSendGridMailEngine' ) ) { } } } - diff --git a/Postman/PostmanConfigTextHelper.php b/Postman/PostmanConfigTextHelper.php index 66a21c8..bcae7f5 100644 --- a/Postman/PostmanConfigTextHelper.php +++ b/Postman/PostmanConfigTextHelper.php @@ -28,13 +28,10 @@ if ( ! class_exists( 'PostmanAbstractConfigTextHelper' ) ) { */ abstract class PostmanAbstractConfigTextHelper implements PostmanConfigTextHelper { public function getOAuthHelp() { - $attention = __( 'Attention' ); - /* translators: parameters available are 1=portal-url, 2=portal-name, 3=clientId-name, 4=clientSecret-name, 5=callbackUrl, 6=service-name, 7=portal-application (e.g. Open the Google Developer Console, create a Client ID for web application using the URL's displayed below, and copy the Client ID and Client Secret here.) */ - $errorMessage = sprintf( __( 'Open the %2$s, create %7$s with the values displayed below, and copy the generated %3$s and %4$s here.', Postman::TEXT_DOMAIN ), $this->getApplicationPortalUrl(), $this->getApplicationPortalName(), $this->getClientIdLabel(), $this->getClientSecretLabel(), $this->getCallbackUrlLabel(), $this->getOwnerName(), $this->getApplicationDescription() ); + $attention = __( 'Attention', Postman::TEXT_DOMAIN ); + $errorMessage = sprintf( __('Check this article how to configure Gmail/Gsuite OAuth:Read Here', Postman::TEXT_DOMAIN ), 'https://postmansmtp.com/how-to-configure-post-smtp-with-gmailgsuite-using-oauth/' ); $text = sprintf( '%s! %s', $attention, $errorMessage ); - /* translators: parameters available are 1=clientId-name, 2=service-name, 3=FAQ-URL, 4=Video-URL (e.g. See How do I get a Google Client ID? in the F.A.Q.) */ - $howToTemplate = __( 'See How do I get a %1$s %2$s? in the F.A.Q. or watch our How-To video 📺.', Postman::TEXT_DOMAIN ); - $text .= sprintf( ' %s', sprintf( $howToTemplate, $this->getOwnerName(), $this->getClientIdLabel(), 'https://wordpress.org/plugins/post-smtp/faq/', 'https://vimeo.com/128589255' ) ); + return $text; } function isOauthHost() { diff --git a/Postman/PostmanInputSanitizer.php b/Postman/PostmanInputSanitizer.php index 859e3cb..b17df30 100644 --- a/Postman/PostmanInputSanitizer.php +++ b/Postman/PostmanInputSanitizer.php @@ -54,6 +54,7 @@ if ( ! class_exists( 'PostmanInputSanitizer' ) ) { $this->sanitizeString( 'From Name Override', PostmanOptions::PREVENT_MESSAGE_SENDER_NAME_OVERRIDE, $input, $new_input ); $this->sanitizeString( 'From Email Override', PostmanOptions::PREVENT_MESSAGE_SENDER_EMAIL_OVERRIDE, $input, $new_input ); $this->sanitizeString( 'Disable Email Validation', PostmanOptions::DISABLE_EMAIL_VALIDAITON, $input, $new_input ); + $this->sanitizeString( 'Mailgun Europe Region', PostmanOptions::MAILGUN_REGION, $input, $new_input ); $this->sanitizeString( 'Forced To Recipients', PostmanOptions::FORCED_TO_RECIPIENTS, $input, $new_input ); $this->sanitizeString( 'Forced CC Recipients', PostmanOptions::FORCED_CC_RECIPIENTS, $input, $new_input ); $this->sanitizeString( 'Forced BCC Recipients', PostmanOptions::FORCED_BCC_RECIPIENTS, $input, $new_input ); diff --git a/Postman/PostmanOptions.php b/Postman/PostmanOptions.php index 240b1aa..9c244bd 100644 --- a/Postman/PostmanOptions.php +++ b/Postman/PostmanOptions.php @@ -87,6 +87,7 @@ if ( ! class_exists( 'PostmanOptions' ) ) { const SENDGRID_API_KEY = 'sendgrid_api_key'; const MAILGUN_API_KEY = 'mailgun_api_key'; const MAILGUN_DOMAIN_NAME = 'mailgun_domain_name'; + const MAILGUN_REGION = 'mailgun_region'; const PREVENT_MESSAGE_SENDER_NAME_OVERRIDE = 'prevent_sender_name_override'; const PREVENT_MESSAGE_SENDER_EMAIL_OVERRIDE = 'prevent_sender_email_override'; const CONNECTION_TIMEOUT = 'connection_timeout'; @@ -317,6 +318,12 @@ if ( ! class_exists( 'PostmanOptions' ) ) { } } + public function getMailgunRegion() { + if ( isset( $this->options [ PostmanOptions::MAILGUN_REGION ] ) ) { + return $this->options [ PostmanOptions::MAILGUN_REGION ]; + } + } + public function getPushoverUser() { if ( isset( $this->options [ PostmanOptions::PUSHOVER_USER ] ) ) { return base64_decode( $this->options [ PostmanOptions::PUSHOVER_USER ] ); diff --git a/Postman/PostmanViewController.php b/Postman/PostmanViewController.php index d2e6f45..46aa39f 100644 --- a/Postman/PostmanViewController.php +++ b/Postman/PostmanViewController.php @@ -307,7 +307,7 @@ if ( ! class_exists( 'PostmanViewController' ) ) { echo '

- Version ' . $version . ' I do mistakes too - A must read: Read Here + Version ' . $version . ' Mailgun Europe region support: Read Here