Exemple #1
0
 /**
  * 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;
     }
 }