/** * A convenient place for any code to inject a constructed PostmanMessage * (for example, from MyMail) * * The body parts may be set already at this time. * * @param PostmanMessage $message * @return boolean */ public function sendMessage(PostmanMessage $message, PostmanEmailLog $log) { // get the Options and AuthToken $options = PostmanOptions::getInstance(); $authorizationToken = PostmanOAuthToken::getInstance(); // get the transport and create the transportConfig and engine $transport = PostmanTransportRegistry::getInstance()->getActiveTransport(); // create the Mail Engine $engine = $transport->createMailEngine(); // add plugin-specific attributes to PostmanMessage $message->addHeaders($options->getAdditionalHeaders()); $message->addTo($options->getForcedToRecipients()); $message->addCc($options->getForcedCcRecipients()); $message->addBcc($options->getForcedBccRecipients()); // apply the WordPress filters // may impact the from address, from email, charset and content-type $message->applyFilters(); // create the body parts (if they are both missing) if ($message->isBodyPartsEmpty()) { $message->createBodyParts(); } // is this a test run? $testMode = apply_filters('postman_test_email', false); if ($this->logger->isDebug()) { $this->logger->debug('testMode=' . $testMode); } // start the clock $startTime = microtime(true) * 1000; try { // prepare the message $message->validate($transport); // send the message if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION) { if ($transport->isLockingRequired()) { PostmanUtils::lock(); // may throw an exception attempting to contact the OAuth2 provider $this->ensureAuthtokenIsUpdated($transport, $options, $authorizationToken); } $this->logger->debug('Sending mail'); // may throw an exception attempting to contact the SMTP server $engine->send($message); // increment the success counter, unless we are just tesitng if (!$testMode) { PostmanState::getInstance()->incrementSuccessfulDelivery(); } } // clean up $this->postSend($engine, $startTime, $options, $transport); if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY) { // log the successful delivery PostmanEmailLogService::getInstance()->writeSuccessLog($log, $message, $engine->getTranscript(), $transport); } // return successful return true; } catch (Exception $e) { // save the error for later $this->exception = $e; // write the error to the PHP log $this->logger->error(get_class($e) . ' code=' . $e->getCode() . ' message=' . trim($e->getMessage())); // increment the failure counter, unless we are just tesitng if (!$testMode && $options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION) { PostmanState::getInstance()->incrementFailedDelivery(); } // clean up $this->postSend($engine, $startTime, $options, $transport); if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY) { // log the failed delivery PostmanEmailLogService::getInstance()->writeFailureLog($log, $message, $engine->getTranscript(), $transport, $e->getMessage()); } // return failure return false; } }
/** * Handles the authorization grant */ function handleAuthorizationGrant() { $logger = $this->logger; $options = $this->options; $authorizationToken = $this->authorizationToken; $logger->debug('Authorization in progress'); $transactionId = PostmanSession::getInstance()->getOauthInProgress(); // begin transaction PostmanUtils::lock(); $authenticationManager = PostmanAuthenticationManagerFactory::getInstance()->createAuthenticationManager(PostmanTransportRegistry::getInstance()->getCurrentTransport(), $options, $authorizationToken); try { if ($authenticationManager->processAuthorizationGrantCode($transactionId)) { $logger->debug('Authorization successful'); // save to database $authorizationToken->save(); $this->messageHandler->addMessage(__('The OAuth 2.0 authorization was successful. Ready to send e-mail.', 'postman-smtp')); } else { $this->messageHandler->addError(__('Your email provider did not grant Postman permission. Try again.', 'postman-smtp')); } } catch (PostmanStateIdMissingException $e) { $this->messageHandler->addError(__('The grant code from Google had no accompanying state and may be a forgery', 'postman-smtp')); } catch (Exception $e) { $logger->error('Error: ' . get_class($e) . ' code=' . $e->getCode() . ' message=' . $e->getMessage()); /* translators: %s is the error message */ $this->messageHandler->addError(sprintf(__('Error authenticating with this Client ID. [%s]', 'postman-smtp'), '<em>' . $e->getMessage() . '</em>')); } // clean-up PostmanUtils::unlock(); PostmanSession::getInstance()->unsetOauthInProgress(); // redirect home PostmanUtils::redirect(PostmanUtils::POSTMAN_HOME_PAGE_RELATIVE_URL); }
/** * A convenient place for other code to send a PostmanMessage * * @param PostmanMessage $message * @return boolean */ public function sendMessage(PostmanMessage $message, PostmanEmailLog $log) { // start the clock $startTime = microtime(true) * 1000; // get the Options and AuthToken $options = PostmanOptions::getInstance(); $authorizationToken = PostmanOAuthToken::getInstance(); // add plugin-specific attributes to PostmanMessage $message->addHeaders($options->getAdditionalHeaders()); $message->addTo($options->getForcedToRecipients()); $message->addCc($options->getForcedCcRecipients()); $message->addBcc($options->getForcedBccRecipients()); // get the transport and create the transportConfig and engine $transport = PostmanTransportRegistry::getInstance()->getCurrentTransport(); // create the Zend Mail Transport Configuration Factory if (PostmanOptions::AUTHENTICATION_TYPE_OAUTH2 == $transport->getAuthenticationType()) { $transportConfiguration = new PostmanOAuth2ConfigurationFactory(); } else { $transportConfiguration = new PostmanBasicAuthConfigurationFactory(); } // create the Mail Engine $engine = new PostmanMailEngine($transport, $transportConfiguration); // is this a test run? $testMode = apply_filters('postman_test_email', false); $this->logger->debug('testMode=' . $testMode); try { // validate the message $message->applyFilters(); $message->validate(); // send the message if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION) { if ($options->isAuthTypeOAuth2()) { PostmanUtils::lock(); // may throw an exception attempting to contact the OAuth2 provider $this->ensureAuthtokenIsUpdated($transport, $options, $authorizationToken); } $this->logger->debug('Sending mail'); // may throw an exception attempting to contact the SMTP server $engine->send($message, $options->getHostname()); // increment the success counter, unless we are just tesitng if (!$testMode) { PostmanStats::getInstance()->incrementSuccessfulDelivery(); } } if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY) { // log the successful delivery PostmanEmailLogService::getInstance()->writeSuccessLog($log, $message, $engine->getTranscript(), $transport); } // clean up $this->postSend($engine, $startTime, $options); // return successful return true; } catch (Exception $e) { // save the error for later $this->exception = $e; // write the error to the PHP log $this->logger->error(get_class($e) . ' code=' . $e->getCode() . ' message=' . trim($e->getMessage())); // increment the failure counter, unless we are just tesitng if (!$testMode && $options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION) { PostmanStats::getInstance()->incrementFailedDelivery(); } if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY) { // log the failed delivery PostmanEmailLogService::getInstance()->writeFailureLog($log, $message, $engine->getTranscript(), $transport, $e->getMessage()); } // clean up $this->postSend($engine, $startTime, $options); // return failure return false; } }