Click here to Skip to main content
15,895,793 members
Articles / Programming Languages / Javascript

MySendToMail: A Replacement for Windows SendTo/Email

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
31 Jan 2007CPOL2 min read 40.9K   1.6K   23  
This article presents a tool to send embedded/attached images via email by simple right-click => Send To
// 
// $Id: my_send_to_mail.js 531 2007-02-01 09:52:23Z ns21834\Emmanuel $
//

//=============================================================================
// Check we called the wsf program (includes appropriate files)
if (WScript.ScriptFullName.match(/\.wsf$/i) == null)
{
  throw new Error("ERROR: invalid execution context: please call *.wsf program");
}
// Check that we are running CSCRIPT.EXE and not WSCRIPT.EXE
if (WScript.FullName.match(/cscript\.exe$/i) == null)
{
  throw new Error("ERROR: invalid execution context: please call this program from the command line with CSCRIPT.EXE and not WSCRIPT.EXE!");
}

function my_send_to_mail_main()
{
  WScript.Echo("***************************************************");
  WScript.Echo("");
  WScript.Echo("              My Send To Mail v1.0");
  WScript.Echo("");
  WScript.Echo("***************************************************");
  WScript.Echo("(c) 2007 Emmanuel KARTMANN (emmanuel@kartmann.org)");

  var blnFirst = true;
  var arrAttachments = null;
  for (var intAtt=0; intAtt< WScript.Arguments.Unnamed.Count; intAtt++)
  {
    var strFilePath = WScript.Arguments(intAtt);

    // Embedded files
    var strEmbeddedFileOutputPath = strFilePath;
    if (intImgEmbeddedResize != 0)
    {
      strEmbeddedFileOutputPath = strTemporaryDir + "\\" + FilePathGetName(strFilePath) + ".emb" + FilePathGetExtension(strFilePath);
      SystemAddTemporaryFile(strEmbeddedFileOutputPath);
      SystemExecute(strImgResizeExe, "\"" + strFilePath + "\" " + "\"" + strEmbeddedFileOutputPath + "\" " + intImgEmbeddedResize);
    }
    if (blnFirst)
    {
      strHTMLBody += strTablePrefix + strLinePrefix;
      blnFirst = false;
    }
    else if (intAtt % intImgEmbeddedColumns == 0)
    {
      strHTMLBody += strLineSuffix + strLinePrefix;
    }
    strHTMLBody += strColumnPrefix 
                + "<img src=\"" + URLBuildImgSrc(strEmbeddedFileOutputPath) + "\" title=\"" + HTMLEncode(FilePathGetName(strFilePath)) +  "\" alt=\"" + HTMLEncode(FilePathGetName(strFilePath)) +  "\">"
                + strColumnSuffix;

    // Attached files
    var strAttachedFileOutputPath = strFilePath;
    if (intImageAttachedResize != 0)
    {
      if (intImageAttachedResize == intImgEmbeddedResize)
      {
        // Same resize ratio: reuse the same file for both embedding and attachments
        strAttachedFileOutputPath = strEmbeddedFileOutputPath;
      }
      else
      {
        // Different ratio: call resize program again with new ratio and new output file name
        strAttachedFileOutputPath = strTemporaryDir + "\\" + FilePathGetName(strFilePath) + ".att" + FilePathGetExtension(strFilePath);
        SystemAddTemporaryFile(strAttachedFileOutputPath);
        SystemExecute(strImgResizeExe, "\"" + strFilePath + "\" " + "\"" + strAttachedFileOutputPath + "\" " + intImageAttachedResize);
      }

    } // if (intImageAttachedResize != 0)
    
    if (arrAttachments == null)
    {
      arrAttachments = new Array();
    }
    arrAttachments.push(strAttachedFileOutputPath);
    
  } // for (var intAtt=0; intAtt< WScript.Arguments.Unnamed.Count; intAtt++)
  
  if (intAtt % intImgEmbeddedColumns == 0)
  {
    strHTMLBody += strLineSuffix;
  }
  else
  {
    strHTMLBody += strColumnPrefix + "&#32;" + strColumnSuffix + strLineSuffix;
  }

  // Zip all attached file in ONE single zip file
  if (arrAttachments != null)
  {
    var strAttachments = "";
    var strAttachedFileSeparator = "";
    for (var intAtt = 0; intAtt < arrAttachments.length; intAtt++)
    {
      strAttachments += strAttachedFileSeparator + "\"" + arrAttachments[intAtt] +"\"";
      strAttachedFileSeparator = " ";
    }
    strZipParams = strZipParams.replace(/%ATTACHMENTS%/, strAttachments);
    SystemAddTemporaryFile(strZipAttachment);
    SystemExecute(strZipExe, strZipParams);
  }

  if (!blnFirst)
  {
    strHTMLBody += strTableSuffix + "</body></html>";
  }
  
  // DEBUG WScript.Echo(strHTMLBody);
  strEmailComposerParams = strEmailComposerParams.replace(/%BODY%/, strHTMLBody);
  strEmailComposerParams = strEmailComposerParams.replace(/%ATTACHMENTS%/, URLBuildImgSrc(strZipAttachment));
  SystemExecute(strEmailComposerExe, strEmailComposerParams);

  WScript.StdOut.WriteLine("Please press any key once email message has been SENT...");
  WScript.StdIn.ReadLine();
  // Cleanup after confirmation
  SystemCleanupTemporaryFiles();

} // function my_send_to_mail_main()

//=============================================================================
function my_send_to_mail_install()
{
  WScript.Echo("Create shortcut at SentTo...");
  var objShell = SystemCreateShell();
  var strSendToPath = objShell.SpecialFolders("SendTo") + "\\My Send To Mail.lnk";
  var strTarget = FilePathGetFolder(WScript.ScriptFullName) + "\\my_send_to_mail.bat";
  var strIconLocation = FilePathGetFolder(WScript.ScriptFullName) + "\\MySendToMail.ico";
  SystemCreateShortcut(strSendToPath, strTarget, null, strIconLocation, "Send pictures by email - resize, embed and zip them first", FilePathGetFolder(WScript.ScriptFullName));

  WScript.Echo("Done.");
} // function my_send_to_mail_install()

function my_send_to_mail_uninstall()
{
  WScript.Echo("Delete shortcut at SentTo...");
  var objShell = SystemCreateShell();
  var strSendToPath = objShell.SpecialFolders("SendTo") + "\\My Send To Mail.lnk";
  FileDelete(strSendToPath);

  
  WScript.Echo("Done.");
  WScript.Echo("");
  WScript.Echo("Uninstallation complete. You can delete the directory now.");
  WScript.StdOut.WriteLine("Please press any key.");
  WScript.StdIn.ReadLine();
} // function my_send_to_mail_install()

//=============================================================================

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
France France
Fell into computer software at the age of 11, founder of 3 startups, and now manager of an independent software vendor (ISV) labelled proSDK (www.prosdk.com)... And still a freeware writer and technical article author!

Comments and Discussions