MailSuite Pro 7 documentation

Uploading files

When sending mails out, you can supply one or multiple attachments there. And prior to MessageSend method call, you'll need to have those files actually uploaded on the server. There are several ways to achieve that, and they're used for uploading to Files storage, too.

Upload via form

»»»  Useful when you can submit a form in a web browser.

Sample form:

<form action="index.php?/Upload/Attachment/" method="POST" enctype="multipart/form-data">
  <input type="text" name="Token" value="169874786e1d429e20fd993800a8c08c" />
  <input type="text" name="AuthToken" value="89e307c653d701c17d61f2f116dba618" />
  <input type="text" name="AccountID" value="1" />
  <input type="text" name="jua-post-type" value="iframe" />
  <input type="file" name="jua-uploader" />
  <input type="submit" value="submit" />
</form>

Be sure to supply specific Token and AccountID values there.

Sample response:

{
  "Action":"UploadAttachment",
  "AccountID":1,
  "Result":
  {
    "Attachment":
    {
      "Name":"apple-icon.png",
      "TempName":"upload-post-dcbcbd6b29caf4c0dbbc464a1b86e94a", // temporary name used when saving and sending message
      "MimeType":"image/png",
      "Size":576,
      "Iframed":false,
      "Hash":"Dmpd0arW8Qbc1KKintwtn50z8aPhYhl1Y0ZL6kOSgQV4f_rN9pDIj9XhkDwzfHi75X4i5Crzpoxc-9N93FDwG7wOHAcokoU7RdG3c66yIqkjlN5TQIEKuKCoGPVvH32LobMav8I6mD8FiqEguDZiq9A9XjN_e9jqaoeKalaXNxWj952gBs_Q7_hcYulPCZWndSojv7849RWQ6PzPwDr-GvEKJOeGMJqR" // needed to view or download file
    }
  },
  "@Time":0.14194798469543
}

In case of uploading into Files storage, both the request and the response look slightly different:

<form action="index.php?/Upload/File/" method="POST" enctype="multipart/form-data">
  <input type="text" name="Token" value="169874786e1d429e20fd993800a8c08c" />
  <input type="text" name="AuthToken" value="89e307c653d701c17d61f2f116dba618" />
  <input type="text" name="AccountID" value="1" />
  <input type="text" name="jua-post-type" value="iframe" />
  <input type="file" name="jua-uploader" />
  <input type="text" name="AdditionalData" value="{&quot;Type&quot;:&quot;personal&quot;,&quot;Path&quot;:&quot;my folder&quot;}" />
  <input type="submit" value="submit" />
</form>

AdditionalData is a serialized object which holds information on file storage type ("personal"|"corporate") and path to the folder the file is uploaded to.

Sample response:

{
  "Action":"UploadFile",
  "AccountID":98,
  "Result":
  {
    "File":
    {
      "Name":"file1.png",
      "TempName":"upload-post-ba41a90deb4aeff6a754d853367ddcee",
      "MimeType":"image\/png",
      "Size":29390,
      "Hash":"p6JdADEcdmHB98iWmR87MfXRyVRpp8PMxTeaMgS9s8V2HR3sSs0lIiH9e01WJtgN5n-WYWU8W5UUcCT4TD-bc376J6Kyje7c06DTyr_q047sO_HTpjNgzXhUgqESKJgye92qaDDD66BOcBdAAiLtg73N-CubV8iWGoM6aD_QG6xvzo10-Asbz39MEqngDSdfsvKwFR5LDip6x1cIWuXZ41Ji5KQ." // needed to view or download file
    }
  },
  "@Time":0.2218611240387
}

Upload via AJAX

»»»  Useful when your application can send AJAX requests.

Sample request:

<script type="text/javascript">
  function UploadFile()
  {
    var
      oInput = document.getElementById('file-upload-input'),
      aFiles = (oInput && oInput.files && 0 < oInput.files.length) ? oInput.files : [],
      oXhr = new XMLHttpRequest(),
      oFormData = new FormData()
    ;
    if (aFiles.length > 0)
    {
      oXhr.open('POST', 'index.php?/Upload/Attachment/', true);
      oFormData.append('Token', '169874786e1d429e20fd993800a8c08c');
      oFormData.append('AccountID', '1');
      oFormData.append('jua-post-type', 'ajax');
      oFormData.append('jua-uploader', aFiles[0]);
      oXhr.send(oFormData);
    }
  }
</script>
<input type="file" id="file-upload-input" />
<input type="button" onclick="UploadFile()" value="click" />

In case of uploading into Files storage, both the request will look slightly different:

<script type="text/javascript">
  function UploadFile()
  {
    var
      oInput = document.getElementById('file-upload-input'),
      aFiles = (oInput && oInput.files && 0 < oInput.files.length) ? oInput.files : [],
      oXhr = new XMLHttpRequest(),
      oFormData = new FormData()
    ;
    if (aFiles.length > 0)
    {
      oXhr.open('POST', 'index.php?/Upload/File/', true);
      oFormData.append('Token', '284d4f04a9baf3406657b13e48d64442');
      oFormData.append('AccountID', '98');
      oFormData.append('AdditionalData', '{"Type":"personal","Path":"my folder","Overwrite":1}');
      oFormData.append('jua-post-type', 'ajax');
      oFormData.append('jua-uploader', aFiles[0]);
      oXhr.send(oFormData);
    }
  }
</script>
<input type="file" id="file-upload-input" />
<input type="button" onclick="UploadFile()" value="click" />

Note that Overwrite parameter of AdditionalData should be set to 1 if you wish to overwrite existing file, and to 0 otherwise.

The response to the AJAX upload request is identical to the web form's upload.

Uploading via PUT

»»»  Useful for desktop, mobile and server applications.

Starting from version 7.6.3 of MailSuite Pro, files can be uploaded using PUT method.

The response to PUT upload request is identical to the web form's upload and AJAX upload.

Storing files on server directly

»»»  Useful when you have direct access to server filesystem.

$sName = "orig-filename.txt"; // original filename
$sTempName = "temp-filename.txt"; // temporary filename used for storing on filesystem
$sKey = 'upload-post-'.md5($sName.$sTempName); // temporary filename sent by client for attaching the file
$sEmail = "user@domain.com"; // email address of the account
$sEmailMd5 = md5(strtolower($sEmail));
$sKeyPath = md5($sKey);
$sKeyPath = substr($sKeyPath, 0, 2).'/'.$sKeyPath;
$sFilePath ='data/temp/.cache/'.substr($sEmailMd5, 0, 2).'/'.$sEmailMd5.'/'.$sKeyPath;

In the last line of the above code, full path to file stored on server is obtained, and $sTempName contains the filename.

NB: this approach only works for adding attachments, not for uploading to Files storage - you can use PUT method for that.