getAuthenticationType() ) {
$config = PostmanOAuth2ConfigurationFactory::createConfig( $this );
} else {
$config = PostmanBasicAuthConfigurationFactory::createConfig( $this );
}
return new Postman_Zend_Mail_Transport_Smtp( $this->getHostname(), $config );
}
/**
* Determines whether Mail Engine locking is needed
*
* @see PostmanModuleTransport::requiresLocking()
*/
public function isLockingRequired() {
return PostmanOptions::AUTHENTICATION_TYPE_OAUTH2 == $this->getAuthenticationType();
}
public function getSlug() {
return self::SLUG;
}
public function getName() {
return 'SMTP';
}
public function getHostname() {
$this->options = $this->options;
return $this->options->getHostname();
}
public function getPort() {
$this->options = $this->options;
return $this->options->getPort();
}
public function getAuthenticationType() {
return $this->options->getAuthenticationType();
}
public function getCredentialsId() {
$this->options = $this->options;
if ( $this->options->isAuthTypeOAuth2() ) {
return $this->options->getClientId();
} else {
return $this->options->getUsername();
}
}
public function getCredentialsSecret() {
$this->options = $this->options;
if ( $this->options->isAuthTypeOAuth2() ) {
return $this->options->getClientSecret();
} else {
return $this->options->getPassword();
}
}
/**
* (non-PHPdoc)
*
* @see PostmanTransport::getMisconfigurationMessage()
*/
protected function validateTransportConfiguration() {
$messages = parent::validateTransportConfiguration();
if ( ! $this->isHostConfigured( $this->options ) ) {
array_push( $messages, __( 'Outgoing Mail Server Hostname and Port can not be empty.', 'post-smtp' ) );
$this->setNotConfiguredAndReady();
}
if ( ! $this->isEnvelopeFromConfigured() ) {
array_push( $messages, __( 'Envelope-From Email Address can not be empty', 'post-smtp' ) . '.' );
$this->setNotConfiguredAndReady();
}
if ( $this->options->isAuthTypePassword() && ! $this->isPasswordAuthenticationConfigured( $this->options ) ) {
array_push( $messages, __( 'Username and password can not be empty.', 'post-smtp' ) );
$this->setNotConfiguredAndReady();
}
if ( $this->getAuthenticationType() == PostmanOptions::AUTHENTICATION_TYPE_OAUTH2 ) {
if ( ! $this->isOAuth2SupportedHostConfigured() ) {
/* translators: %1$s is the Client ID label, and %2$s is the Client Secret label (e.g. Warning: OAuth 2.0 authentication requires an OAuth 2.0-capable Outgoing Mail Server, Sender Email Address, Client ID, and Client Secret.) */
array_push( $messages, sprintf( __( 'OAuth 2.0 authentication requires a supported OAuth 2.0-capable Outgoing Mail Server.', 'post-smtp' ) ) );
$this->setNotConfiguredAndReady();
}
}
if ( empty( $messages ) ) {
$this->setReadyForOAuthGrant();
if ( $this->isPermissionNeeded( $this->options, $this->getOAuthToken() ) ) {
/* translators: %1$s is the Client ID label, and %2$s is the Client Secret label */
$message = sprintf( __( 'You have configured OAuth 2.0 authentication, but have not received permission to use it.', 'post-smtp' ), $this->getScribe()->getClientIdLabel(), $this->getScribe()->getClientSecretLabel() );
$message .= sprintf( ' %s.', PostmanUtils::getGrantOAuthPermissionUrl(), $this->getScribe()->getRequestPermissionLinkText() );
array_push( $messages, $message );
$this->setNotConfiguredAndReady();
}
}
return $messages;
}
/**
*
* @return boolean
*/
private function isOAuth2SupportedHostConfigured() {
$options = PostmanOptions::getInstance();
$hostname = $options->getHostname();
$supportedOAuthProvider = $this->isServiceProviderGoogle( $hostname ) || $this->isServiceProviderMicrosoft( $hostname ) || $this->isServiceProviderYahoo( $hostname );
return $supportedOAuthProvider;
}
/**
* Given a hostname, what ports should we test?
*
* May return an array of several combinations.
*/
public function getSocketsForSetupWizardToProbe( $hostname, $smtpServerGuess ) {
$hosts = array(
$this->createSocketDefinition( $hostname, 25 ),
$this->createSocketDefinition( $hostname, 465 ),
$this->createSocketDefinition( $hostname, 587 ),
);
return $hosts;
}
/**
* Creates a single socket for the Wizard to test
*/
protected function createSocketDefinition( $hostname, $port ) {
$socket = parent::createSocketDefinition( $hostname, $port );
$socket ['smtp'] = true;
return $socket;
}
/**
* SendGrid will never recommend it's configuration
*
* @param mixed $hostData
*/
public function getConfigurationBid( PostmanWizardSocket $hostData, $userAuthOverride, $originalSmtpServer ) {
$port = $hostData->port;
$hostname = $hostData->hostname;
// because some servers, like smtp.broadband.rogers.com, report XOAUTH2 but have no OAuth2 front-end
$supportedOAuth2Provider = $this->isServiceProviderGoogle( $hostname ) || $this->isServiceProviderMicrosoft( $hostname ) || $this->isServiceProviderYahoo( $hostname );
$score = 1;
$recommendation = array();
// increment score for auth type
if ( $hostData->mitm ) {
$this->logger->debug( 'Losing points for MITM' );
$score -= 10000;
$recommendation ['mitm'] = true;
}
if ( ! empty( $originalSmtpServer ) && $hostname != $originalSmtpServer ) {
$this->logger->debug( 'Losing points for Not The Original SMTP server' );
$score -= 10000;
}
$secure = true;
if ( $hostData->startTls ) {
// STARTTLS was formalized in 2002
// http://www.rfc-editor.org/rfc/rfc3207.txt
$recommendation ['enc'] = PostmanOptions::SECURITY_TYPE_STARTTLS;
$score += 30000;
} elseif ( $hostData->protocol == 'SMTPS' ) {
// "The hopelessly confusing and imprecise term, SSL,
// has often been used to indicate the SMTPS wrapper and
// TLS to indicate the STARTTLS protocol extension."
// http://stackoverflow.com/a/19942206/4368109
$recommendation ['enc'] = PostmanOptions::SECURITY_TYPE_SMTPS;
$score += 28000;
} elseif ( $hostData->protocol == 'SMTP' ) {
$recommendation ['enc'] = PostmanOptions::SECURITY_TYPE_NONE;
$score += 26000;
$secure = false;
}
// if there is a way to send mail....
if ( $score > 10 ) {
// determine the authentication type
if ( $hostData->auth_xoauth && $supportedOAuth2Provider && (empty( $userAuthOverride ) || $userAuthOverride == 'oauth2') ) {
$recommendation ['auth'] = PostmanOptions::AUTHENTICATION_TYPE_OAUTH2;
$recommendation ['display_auth'] = 'oauth2';
$score += 500;
if ( ! $secure ) {
$this->logger->debug( 'Losing points for sending credentials in the clear' );
$score -= 10000;
}
} elseif ( $hostData->auth_crammd5 && (empty( $userAuthOverride ) || $userAuthOverride == 'password') ) {
$recommendation ['auth'] = PostmanOptions::AUTHENTICATION_TYPE_CRAMMD5;
$recommendation ['display_auth'] = 'password';
$score += 400;
if ( ! $secure ) {
$this->logger->debug( 'Losing points for sending credentials in the clear' );
$score -= 10000;
}
} elseif ( $hostData->authPlain && (empty( $userAuthOverride ) || $userAuthOverride == 'password') ) {
$recommendation ['auth'] = PostmanOptions::AUTHENTICATION_TYPE_PLAIN;
$recommendation ['display_auth'] = 'password';
$score += 300;
if ( ! $secure ) {
$this->logger->debug( 'Losing points for sending credentials in the clear' );
$score -= 10000;
}
} elseif ( $hostData->auth_login && (empty( $userAuthOverride ) || $userAuthOverride == 'password') ) {
$recommendation ['auth'] = PostmanOptions::AUTHENTICATION_TYPE_LOGIN;
$recommendation ['display_auth'] = 'password';
$score += 200;
if ( ! $secure ) {
$this->logger->debug( 'Losing points for sending credentials in the clear' );
$score -= 10000;
}
} else if ( empty( $userAuthOverride ) || $userAuthOverride == 'none' ) {
$recommendation ['auth'] = PostmanOptions::AUTHENTICATION_TYPE_NONE;
$recommendation ['display_auth'] = 'none';
$score += 100;
}
// tiny weighting to prejudice the port selection, all things being equal
if ( $port == 587 ) {
$score += 4;
} elseif ( $port == 25 ) {
// "due to the prevalence of machines that have worms,
// viruses, or other malicious software that generate large amounts of
// spam, many sites now prohibit outbound traffic on the standard SMTP
// port (port 25), funneling all mail submissions through submission
// servers."
// http://www.rfc-editor.org/rfc/rfc6409.txt
$score += 3;
} elseif ( $port == 465 ) {
// use of port 465 for SMTP was deprecated in 1998
// http://www.imc.org/ietf-apps-tls/mail-archive/msg00204.html
$score += 2;
} else {
$score += 1;
}
// create the recommendation message for the user
// this can only be set if there is a valid ['auth'] and ['enc']
$transportDescription = $this->getTransportDescription( $recommendation ['enc'] );
$authDesc = $this->getAuthenticationDescription( $recommendation ['auth'] );
$recommendation ['label'] = sprintf( 'SMTP - %2$s:%3$d', $transportDescription, $hostData->hostnameDomainOnly, $port );
/* translators: where %1$s is a description of the transport (eg. SMTPS-SSL), %2$s is a description of the authentication (eg. Password-CRAMMD5), %3$d is the TCP port (eg. 465), %4$d is the hostname */
$recommendation ['message'] = sprintf( __( 'Postman recommends %1$s with %2$s authentication to host %4$s on port %3$d.', 'post-smtp' ), $transportDescription, $authDesc, $port, $hostname );
}
// fill-in the rest of the recommendation
$recommendation ['transport'] = PostmanSmtpModuleTransport::SLUG;
$recommendation ['priority'] = $score;
$recommendation ['port'] = $port;
$recommendation ['hostname'] = $hostname;
$recommendation ['transport'] = self::SLUG;
return $recommendation;
}
/**
* Functions to execute on the admin_init event
*
* "Runs at the beginning of every admin page before the page is rendered."
* ref: http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_an_Admin_Page_Request
*/
public function on_admin_init() {
// only administrators should be able to trigger this
if ( PostmanUtils::isAdmin() ) {
$this->addSettings();
$this->registerStylesAndScripts();
}
}
/**
*/
public function registerStylesAndScripts() {
// register the stylesheet and javascript external resources
$pluginData = apply_filters( 'postman_get_plugin_metadata', null );
wp_register_script( 'postman_smtp_script', plugins_url( 'Postman/Postman-Mail/postman_smtp.js', $this->rootPluginFilenameAndPath ), array(
PostmanViewController::JQUERY_SCRIPT,
'jquery_validation',
PostmanViewController::POSTMAN_SCRIPT,
), $pluginData ['version'] );
}
/*
* What follows in the code responsible for creating the Admin Settings page
*/
/**
*/
public function enqueueScript() {
wp_enqueue_script( 'postman_smtp_script' );
}
/**
*/
public function addSettings() {
$transport = $this;
$this->options = $this->options;
$oauthScribe = $transport->getScribe();
// Sanitize
add_settings_section( PostmanAdminController::SMTP_SECTION, __( 'Transport Settings', 'post-smtp' ), array(
$this,
'printSmtpSectionInfo',
), PostmanAdminController::SMTP_OPTIONS );
add_settings_field( PostmanOptions::HOSTNAME, __( 'Outgoing Mail Server Hostname', 'post-smtp' ), array(
$this,
'hostname_callback',
), PostmanAdminController::SMTP_OPTIONS, PostmanAdminController::SMTP_SECTION );
add_settings_field( PostmanOptions::PORT, __( 'Outgoing Mail Server Port', 'post-smtp' ), array(
$this,
'port_callback',
), PostmanAdminController::SMTP_OPTIONS, PostmanAdminController::SMTP_SECTION );
add_settings_field( PostmanOptions::ENVELOPE_SENDER, __( 'Envelope-From Email Address', 'post-smtp' ), array(
$this,
'sender_email_callback',
), PostmanAdminController::SMTP_OPTIONS, PostmanAdminController::SMTP_SECTION );
add_settings_field( PostmanOptions::SECURITY_TYPE, _x( 'Security', 'Configuration Input Field', 'post-smtp' ), array(
$this,
'encryption_type_callback',
), PostmanAdminController::SMTP_OPTIONS, PostmanAdminController::SMTP_SECTION );
add_settings_field( PostmanOptions::AUTHENTICATION_TYPE, __( 'Authentication', 'post-smtp' ), array(
$this,
'authentication_type_callback',
), PostmanAdminController::SMTP_OPTIONS, PostmanAdminController::SMTP_SECTION );
add_settings_section( PostmanAdminController::BASIC_AUTH_SECTION, __( 'Authentication', 'post-smtp' ), array(
$this,
'printBasicAuthSectionInfo',
), PostmanAdminController::BASIC_AUTH_OPTIONS );
add_settings_field( PostmanOptions::BASIC_AUTH_USERNAME, __( 'Username', 'post-smtp' ), array(
$this,
'basic_auth_username_callback',
), PostmanAdminController::BASIC_AUTH_OPTIONS, PostmanAdminController::BASIC_AUTH_SECTION );
add_settings_field( PostmanOptions::BASIC_AUTH_PASSWORD, __( 'Password', 'post-smtp' ), array(
$this,
'basic_auth_password_callback',
), PostmanAdminController::BASIC_AUTH_OPTIONS, PostmanAdminController::BASIC_AUTH_SECTION );
// the OAuth section
add_settings_section( PostmanAdminController::OAUTH_SECTION, __( 'Authentication', 'post-smtp' ), array(
$this,
'printOAuthSectionInfo',
), PostmanAdminController::OAUTH_AUTH_OPTIONS );
add_settings_field( 'callback_domain', sprintf( '%s', $oauthScribe->getCallbackDomainLabel() ), array(
$this,
'callback_domain_callback',
), PostmanAdminController::OAUTH_AUTH_OPTIONS, PostmanAdminController::OAUTH_SECTION );
add_settings_field( 'redirect_url', sprintf( '%s', $oauthScribe->getCallbackUrlLabel() ), array(
$this,
'redirect_url_callback',
), PostmanAdminController::OAUTH_AUTH_OPTIONS, PostmanAdminController::OAUTH_SECTION );
add_settings_field( PostmanOptions::CLIENT_ID, $oauthScribe->getClientIdLabel(), array(
$this,
'oauth_client_id_callback',
), PostmanAdminController::OAUTH_AUTH_OPTIONS, PostmanAdminController::OAUTH_SECTION );
add_settings_field( PostmanOptions::CLIENT_SECRET, $oauthScribe->getClientSecretLabel(), array(
$this,
'oauth_client_secret_callback',
), PostmanAdminController::OAUTH_AUTH_OPTIONS, PostmanAdminController::OAUTH_SECTION );
}
/**
* Print the Section text
*/
public function printSmtpSectionInfo() {
print __( 'Configure the communication with the mail server.', 'post-smtp' );
}
/**
* Get the settings option array and print one of its values
*/
public function hostname_callback() {
printf( '', null !== $this->options->getHostname() ? esc_attr( $this->options->getHostname() ) : '', __( 'Required', 'post-smtp' ) );
}
/**
* Get the settings option array and print one of its values
*/
public function port_callback( $args ) {
printf( '', null !== $this->options->getPort() ? esc_attr( $this->options->getPort() ) : '', isset( $args ['style'] ) ? $args ['style'] : '', __( 'Required', 'post-smtp' ) );
}
/**
* Get the settings option array and print one of its values
*/
public function encryption_type_callback() {
$encType = $this->options->getEncryptionType();
print '';
}
/**
* Get the settings option array and print one of its values
*/
public function authentication_type_callback() {
$authType = $this->options->getAuthenticationType();
printf( '';
}
/**
* Print the Section text
*/
public function printBasicAuthSectionInfo() {
print __( 'Enter the account credentials.', 'post-smtp' );
}
/**
* Get the settings option array and print one of its values
*/
public function basic_auth_username_callback() {
$inputValue = (null !== $this->options->getUsername() ? esc_attr( $this->options->getUsername() ) : '');
$inputDescription = __( 'The Username is usually the same as the Envelope-From Email Address.', 'post-smtp' );
print ('') ;
printf( '
%s', $inputValue, __( 'Required', 'post-smtp' ), $inputDescription );
}
/**
* Get the settings option array and print one of its values
*/
public function basic_auth_password_callback() {
print ('') ;
printf( '', null !== $this->options->getPassword() ? esc_attr( PostmanUtils::obfuscatePassword( $this->options->getPassword() ) ) : '', __( 'Required', 'post-smtp' ) );
print ' ';
}
/**
* Get the settings option array and print one of its values
*/
public function oauth_client_id_callback() {
printf( '', null !== $this->options->getClientId() ? esc_attr( $this->options->getClientId() ) : '', __( 'Required', 'post-smtp' ) );
}
/**
* Get the settings option array and print one of its values
*/
public function oauth_client_secret_callback() {
printf( '', null !== $this->options->getClientSecret() ? esc_attr( $this->options->getClientSecret() ) : '', __( 'Required', 'post-smtp' ) );
}
/**
* Print the Section text
*/
public function printOAuthSectionInfo() {
$this->options = $this->options;
$transport = $this;
$oauthScribe = $transport->getScribe();
printf( '
%s
', $oauthScribe->getOAuthHelp() ); } /** * Get the settings option array and print one of its values */ public function callback_domain_callback() { printf( '', $this->getCallbackDomain() ); } /** */ private function getCallbackDomain() { try { $this->options = $this->options; $transport = $this; $oauthScribe = $transport->getScribe(); return $oauthScribe->getCallbackDomain(); } catch ( Exception $e ) { return __( 'Error computing your domain root - please enter it manually', 'post-smtp' ); } } /** * Get the settings option array and print one of its values */ public function redirect_url_callback() { $this->options = $this->options; $transport = $this; $oauthScribe = $transport->getScribe(); printf( '', $oauthScribe->getCallbackUrl() ); } /** * Get the settings option array and print one of its values */ public function sender_email_callback() { $inputValue = (null !== $this->options->getEnvelopeSender() ? esc_attr( $this->options->getEnvelopeSender() ) : ''); $requiredLabel = __( 'Required', 'post-smtp' ); $envelopeFromMessage = __( 'This address, like the return address printed on an envelope, identifies the account owner to the SMTP server.', 'post-smtp' ); $spfMessage = sprintf( __( 'For reliable delivery, this domain must specify an SPF record permitting the use of the SMTP server named above.', 'post-smtp' ), 'https://www.mail-tester.com/spf/' ); printf( '%s
', __( 'This is the Outgoing (SMTP) Mail Server, or Mail Submission Agent (MSA), which Postman delegates mail delivery to. This server is specific to your email account, and if you don\'t know what to use, ask your email service provider.', 'post-smtp' ) ); printf( '%s
', __( 'Note that many WordPress hosts, such as GoDaddy, Bluehost and Dreamhost, require that you use their mail accounts with their mail servers, and prevent you from using others.', 'post-smtp' ) ); printf( '%s: %s
', $warning, $nonGodaddyDomainMessage ); /* Translators: Where (%1$s) is the SPF-info URL and (%2$s) is the name of the web host */ $godaddyCustomDomainMessage = sprintf( __( 'If you own this domain, make sure it has an SPF record authorizing %2$s as a relay, or you will have delivery problems.', 'post-smtp' ), 'http://www.mail-tester.com/spf/godaddy', 'GoDaddy' ); printf( '%s: %s
', $warning, $godaddyCustomDomainMessage ); } /** */ public function printWizardAuthenticationStep() { print '%s
', __( 'Enter the account credentials.', 'post-smtp' ) ); printf( '', __( 'Username', 'post-smtp' ) ); print '