Changes in SendMail

From Zarafa wiki

Jump to: navigation, search

We are planning some changes in HandleSendMail and backend's SendMail. The main reason is that in ActiveSync protocol version 14 there are more properties we have to handle. Adding them to the SendMail's signature seems to not be a good idea because there's no guarantee there won't be any other new in the future. Instead of the current parameters there will be only one SyncSendMail object which will contain all necessary parameters.

lib/requestprocessor.php

   static private function HandleSendMail($forward = false, $reply = false, $parent = false) {
       $status = SYNC_COMMONSTATUS_SUCCESS;
       $sm = new SyncSendMail();
       if (self::$decoder->IsWBXML()) {
           $el = self::$decoder->getElement();
           if($el[EN_TYPE] != EN_TYPE_STARTTAG)
               return false;
           $sendmail = $smartreply = $smartforward = false;
           if($el[EN_TAG] == SYNC_COMPOSEMAIL_SENDMAIL)
               $sendmail = true;
           else if($el[EN_TAG] == SYNC_COMPOSEMAIL_SMARTREPLY)
               $smartreply = true;
           else if($el[EN_TAG] == SYNC_COMPOSEMAIL_SMARTFORWARD)
               $smartforward = true;
           if(!$sendmail && !$smartreply && !$smartforward)
               return false;
           $sm->Decode(self::$decoder);
       }
       else {
           $sm->mime = self::$decoder->GetPlainInputStream();
           // no wbxml output is provided, only a http OK
           $sm->saveinsent = Request::GetGETSaveInSent();
       }
       if ($reply || $forward) {
           if (!isset($sm->source)) $sm->source = new SyncSendMailSource();
           if ($reply) $sm->source->itemid = $reply;
           else $sm->source->itemid = $forward;
           if ($parent) $sm->source->folderid = $parent;
           else {
               ZLog::Write(LOGLEVEL_ERROR, sprintf("No parent folder id while replying or forwarding message:'%s'", (($reply) ? $reply : $forward)));
           }
       }
       self::$topCollector->AnnounceInformation(sprintf("Sending email with %d bytes", strlen($sm->mime)), true);
       try {
           $status = self::$backend->SendMail1($sm);
       }
       catch (StatusException $se) {
           $status = $se->getCode();
           $statusMessage = $se->getMessage();
       }
       if (self::$decoder->IsWBXML()) {
            self::$encoder->StartWBXML();
            self::$encoder->startTag(SYNC_COMPOSEMAIL_SENDMAIL);
            self::$encoder->startTag(SYNC_COMPOSEMAIL_STATUS);
            self::$encoder->content($status); //TODO return the correct status
            self::$encoder->endTag();
            self::$encoder->endTag();
       }
       elseif ($status != SYNC_COMMONSTATUS_SUCCESS) {
           throw new HTTPReturnCodeException($statusMessage, HTTP_CODE_500, null, LOGLEVEL_WARN);
       }
       return $status;
   }

lib/syncobjects/syncsendmail.php

class SyncSendMail extends SyncObject {
   public $clientid;
   public $saveinsent;
   public $replacemime;
   public $accountid;
   public $source;
   public $mime;
   function SyncSendMail() {
       $mapping = array (
                   SYNC_COMPOSEMAIL_CLIENTID                             => array (  self::STREAMER_VAR      => "clientid"),
                   SYNC_COMPOSEMAIL_SAVEINSENTITEMS                      => array (  self::STREAMER_VAR      => "saveinsent",
                                                                                     self::STREAMER_TYPE     => self::STREAMER_TYPE_SEND_EMPTY),
                   SYNC_COMPOSEMAIL_REPLACEMIME                          => array (  self::STREAMER_VAR      => "replacemime",
                                                                                     self::STREAMER_TYPE     => self::STREAMER_TYPE_SEND_EMPTY),
                   SYNC_COMPOSEMAIL_ACCOUNTID                            => array (  self::STREAMER_VAR      => "accountid"),
                   SYNC_COMPOSEMAIL_SOURCE                               => array (  self::STREAMER_VAR      => "source",
                                                                                     self::STREAMER_TYPE     => "SyncSendMailSource"),
                   SYNC_COMPOSEMAIL_MIME                                 => array (  self::STREAMER_VAR      => "mime"),
       );
       parent::SyncObject($mapping);
   }
}

lib/syncobjects/syncsendmailsource.php

class SyncSendMailSource extends SyncObject {
   public $folderid;
   public $itemid;
   public $longid;
   public $instanceid;
   function SyncSendMailSource() {
       $mapping = array (
                   SYNC_COMPOSEMAIL_FOLDERID                             => array (  self::STREAMER_VAR      => "folderid"),
                   SYNC_COMPOSEMAIL_ITEMID                               => array (  self::STREAMER_VAR      => "itemid"),
                   SYNC_COMPOSEMAIL_LONGID                               => array (  self::STREAMER_VAR      => "longid"),
                   SYNC_COMPOSEMAIL_INSTANCEID                           => array (  self::STREAMER_VAR      => "instanceid"),
       );
       parent::SyncObject($mapping);
   }
}

Include lib/syncobjects/syncsendmail.php and lib/syncobjects/syncsendmailsource.php in index.php

include_once('lib/syncobjects/syncsendmail.php');
include_once('lib/syncobjects/syncsendmailsource.php');

And here's a part of Zarafa backend's SendMail

    public function SendMail($sm) {
       $reply = $forward = $parent = false;
       if (Request::GetCommandCode() == ZPush::COMMAND_SMARTREPLY) {
           $reply = $sm->source->itemid;
           $parent = $sm->source->folderid;
       }
       if (Request::GetCommandCode() == ZPush::COMMAND_SMARTFORWARD) {
           $forward = $sm->source->itemid;
           $parent = $sm->source->folderid;
       }
       // do whatever SendMail in backend has to do
   }
Personal tools