/**
  * (non-PHPdoc)
  *
  * @see PostmanSmtpEngine::send()
  */
 public function send(PostmanMessage $message, $hostname)
 {
     $options = PostmanOptions::getInstance();
     // create the Message
     $charset = $message->getCharset();
     $this->logger->debug('Building Postman_Zend_Mail with charset=' . $charset);
     $mail = new Postman_Zend_Mail($charset);
     // add the Postman signature - append it to whatever the user may have set
     if (!$options->isStealthModeEnabled()) {
         $pluginData = apply_filters('postman_get_plugin_metadata', null);
         $mail->addHeader('X-Mailer', sprintf('Postman SMTP %s for WordPress (%s)', $pluginData['version'], 'https://wordpress.org/plugins/postman-smtp/'));
     }
     // add the headers - see http://framework.zend.com/manual/1.12/en/zend.mail.additional-headers.html
     foreach ((array) $message->getHeaders() as $header) {
         $this->logger->debug(sprintf('Adding user header %s=%s', $header['name'], $header['content']));
         $mail->addHeader($header['name'], $header['content'], true);
     }
     // modify the content-type to include the boundary
     $contentType = $message->getContentType();
     if (false !== stripos($contentType, 'multipart') && !empty($this->boundary)) {
         // Lines in email are terminated by CRLF ("\r\n") according to RFC2821
         $contentType = sprintf("%s;\r\n\t boundary=\"%s\"", $contentType, $message->getBoundary());
     }
     // if the caller set a Content-Type header, push it into the $mail object
     if (!empty($contentType)) {
         $mail->addHeader('Content-Type', $contentType, false);
         $this->logger->debug('Adding content-type ' . $contentType);
     }
     // add the From Header
     $fromHeader = $this->addFrom($message, $mail);
     $fromHeader->log($this->logger, 'From');
     // add the Sender Header, overriding what the user may have set
     $mail->addHeader('Sender', $options->getEnvelopeSender(), false);
     // from RFC 5321: http://tools.ietf.org/html/rfc5321#section-4.4
     // A message-originating SMTP system SHOULD NOT send a message that
     // already contains a Return-path header field.
     // I changed Zend/Mail/Mail.php to fix this
     $mail->setReturnPath($options->getEnvelopeSender());
     // add the to recipients
     foreach ((array) $message->getToRecipients() as $recipient) {
         $recipient->log($this->logger, 'To');
         $mail->addTo($recipient->getEmail(), $recipient->getName());
     }
     // add the cc recipients
     foreach ((array) $message->getCcRecipients() as $recipient) {
         $recipient->log($this->logger, 'Cc');
         $mail->addCc($recipient->getEmail(), $recipient->getName());
     }
     // add the to recipients
     foreach ((array) $message->getBccRecipients() as $recipient) {
         $recipient->log($this->logger, 'Bcc');
         $mail->addBcc($recipient->getEmail(), $recipient->getName());
     }
     // add the reply-to
     $replyTo = $message->getReplyTo();
     // $replyTo is null or a PostmanEmailAddress object
     if (isset($replyTo)) {
         $mail->setReplyTo($replyTo->getEmail(), $replyTo->getName());
     }
     // add the date
     $date = $message->getDate();
     if (!empty($date)) {
         $mail->setDate($date);
     }
     // add the messageId
     $messageId = $message->getMessageId();
     if (!empty($messageId)) {
         $mail->setMessageId($messageId);
     }
     // add the subject
     if (null !== $message->getSubject()) {
         $mail->setSubject($message->getSubject());
     }
     // add the message content as either text or html
     $bodyTextPart = $message->getBodyTextPart();
     $bodyHtmlPart = $message->getBodyHtmlPart();
     if (!empty($bodyTextPart) && empty($bodyHtmlPart)) {
         $this->logger->debug('Adding body as text');
         $mail->setBodyText($message->getBody());
     } else {
         if (empty($bodyTextPart) && !empty($bodyHtmlPart)) {
             $this->logger->debug('Adding body as html');
             $mail->setBodyHtml($message->getBody());
         } else {
             if (!empty($bodyTextPart) && !empty($bodyHtmlPart)) {
                 $this->logger->debug('Adding body as text and html');
                 $mail->setBodyHtml($message->getBodyHtmlPart());
                 $mail->setBodyText($message->getBodyTextPart());
             } else {
                 if (empty($contentType) || substr($contentType, 0, 10) === 'text/plain') {
                     $this->logger->debug('Adding body as text');
                     $mail->setBodyText($message->getBody());
                 } else {
                     if (substr($contentType, 0, 9) === 'text/html') {
                         $this->logger->debug('Adding body as html');
                         $mail->setBodyHtml($message->getBody());
                     } else {
                         if (substr($contentType, 0, 21) === 'multipart/alternative') {
                             $this->logger->debug('Adding body as multipart/alternative');
                             $arr = explode(PHP_EOL, $message->getBody());
                             $textBody = '';
                             $htmlBody = '';
                             $mode = '';
                             foreach ($arr as $s) {
                                 if (substr($s, 0, 25) === "Content-Type: text/plain;") {
                                     $mode = 'foundText';
                                 } else {
                                     if (substr($s, 0, 24) === "Content-Type: text/html;") {
                                         $mode = 'foundHtml';
                                     } else {
                                         if ($mode == 'textReading') {
                                             $textBody .= $s;
                                         } else {
                                             if ($mode == 'htmlReading') {
                                                 $htmlBody .= $s;
                                             } else {
                                                 if ($mode == 'foundText') {
                                                     if ($s == '') {
                                                         $mode = 'textReading';
                                                     }
                                                 } else {
                                                     if ($mode == 'foundHtml') {
                                                         if ($s == '') {
                                                             $mode = 'htmlReading';
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             $mail->setBodyHtml($htmlBody);
                             $mail->setBodyText($textBody);
                         } else {
                             $this->logger->error('Unknown content-type: ' . $contentType);
                             $mail->setBodyText($message->getBody());
                         }
                     }
                 }
             }
         }
     }
     // add attachments
     $this->logger->debug("Adding attachments");
     $message->addAttachmentsToMail($mail);
     // get the transport configuration
     $this->logger->debug("Create the Zend_Mail transport configuration array");
     $config = $this->authenticator->createConfig($this->transport);
     assert(!empty($config));
     // create the SMTP transport
     $this->logger->debug("Create the Zend_Mail transport configuration");
     $zendTransport = $this->transport->createZendMailTransport($hostname, $config);
     assert(!empty($zendTransport));
     try {
         // send the message
         $this->logger->debug("Sending mail");
         $mail->send($zendTransport);
         // finally not supported??
         if ($zendTransport->getConnection() && !PostmanUtils::isEmpty($zendTransport->getConnection()->getLog())) {
             $this->transcript = $zendTransport->getConnection()->getLog();
             $this->logger->trace($this->transcript);
         } else {
             if (method_exists($zendTransport, 'getMessage') && !PostmanUtils::isEmpty($zendTransport->getMessage())) {
                 // then use the Raw Message as the Transcript
                 $this->transcript = $zendTransport->getMessage();
                 $this->logger->trace($this->transcript);
             }
         }
     } catch (Exception $e) {
         // finally not supported??
         if ($zendTransport->getConnection() && !PostmanUtils::isEmpty($zendTransport->getConnection()->getLog())) {
             $this->transcript = $zendTransport->getConnection()->getLog();
             $this->logger->trace($this->transcript);
         } else {
             if (method_exists($zendTransport, 'getMessage') && !PostmanUtils::isEmpty($zendTransport->getMessage())) {
                 // then use the Raw Message as the Transcript
                 $this->transcript = $zendTransport->getMessage();
                 $this->logger->trace($this->transcript);
             }
         }
         // get the current exception message
         $message = $e->getMessage();
         if ($e->getCode() == 334) {
             // replace the unusable Google message with a better one in the case of code 334
             $message = sprintf(__('Communication Error [334] - make sure the Envelope From email is the same account used to create the Client ID.', 'postman-smtp'));
         }
         // create a new exception
         $newException = new Exception($message, $e->getCode());
         // throw the new exception after handling
         throw $newException;
     }
 }