BackgroundEmailer.jsx
Summary
Sample using the Socket object to send email with attachments as a background (scheduled) task.
See:
Class Summary
|
BackgroundEmailer |
Sample using the Socket object to send email with attachments as a background (scheduled) task. |
function BackgroundEmailer()
{
$.level = 1;
this.requiredContext = "\tEnsure Bridge is running and that it is your target and that you have supplied your own mail server details\n";
BackgroundEmailer.mailServerName = "";
BackgroundEmailer.username = "";
BackgroundEmailer.password = "";
BackgroundEmailer.subject = "SDK BackgroundEmailer Subject";
BackgroundEmailer.sender = "";
BackgroundEmailer.recipient = "";
BackgroundEmailer.POP = 110;
BackgroundEmailer.SMTP = 25;
BackgroundEmailer.encodeTaskID = 0;
BackgroundEmailer.contents = "";
BackgroundEmailer.finishedAttachment = false;
BackgroundEmailer.startAttach = true;
BackgroundEmailer.b64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
BackgroundEmailer.Thumbnails = new Array();
BackgroundEmailer.socket = new Socket();
BackgroundEmailer.socket.encoding = "binary";
BackgroundEmailer.isRunning = false;
BackgroundEmailer.boundary = "****=_NextPartMyBody_0000 " + new Date().getSeconds();
}
BackgroundEmailer.buildMessageBody = function()
{
var localMessage = "BackgroundEmailer sent the following attachments:\r\n";
for(var i = 0; i < BackgroundEmailer.Thumbnails.length; i++)
{
localMessage += "\r\n\tName: " + BackgroundEmailer.Thumbnails[i].name;
}
BackgroundEmailer.message = localMessage;
}
BackgroundEmailer.connect = function(host, port)
{
var result = true;
if(!BackgroundEmailer.socket.open(host + ":" + port))
{
$.writeln("BackgroundEmailer.connect(): Error: Could not open socket");
result = false;
}
return result;
}
BackgroundEmailer.authorise = function()
{
var startDate = new Date();
$.writeln("IN BackgroundEmailer.authorise() = " + startDate);
try
{
$.writeln("BackgroundEmailer.authorise(): Authorizing..." + BackgroundEmailer.socket.read());
BackgroundEmailer.doCommand("USER " + BackgroundEmailer.username, "pop")
BackgroundEmailer.doCommand("PASS " + BackgroundEmailer.password, "pop");
BackgroundEmailer.doCommand ("QUIT", "pop");
}
catch(error)
{
$.writeln("BackgroundEmailer.authorise() Error:" + error);
result = false;
}
var endDate = new Date();
$.writeln("OUT BackgroundEmailer.authorise() = " + endDate);
}
BackgroundEmailer.doCommand = function(cmd, type)
{
var retval = true;
var reply = "";
var replyCode = "";
BackgroundEmailer.socket.write(cmd + "\r\n");
$.sleep(300);
if(type == "pop")
{
reply = BackgroundEmailer.socket.read();
replyCode = reply.substring(0, 1);
if(replyCode == "-")
{
retval = false;
$.writeln("BackgroundEmailer.doCommand(): Error with POP command:");
$.writeln("\t" + reply);
BackgroundEmailer.close();
}
}
else if(type == "smtp")
{
if(!BackgroundEmailer.socket.read())
{
retval = false;
$.writeln("BackgroundEmailer.doCommand(): Error sending mail:");
$.writeln("\t" + reply);
BackgroundEmailer.close();
}
}
else
{
$.writeln("BackgroundEmailer.doCommand(): Unknown command type. Closing the socket.");
BackgroundEmailer.close();
}
if(!retval)
{
app.cancelTask(BackgroundEmailer.encodeTaskID);
BackgroundEmailer.isRunning = false;
throw "Command error for: " + type + " - " + cmd;
}
return retval;
}
BackgroundEmailer.close = function()
{
try
{
BackgroundEmailer.socket.close();
}
catch(error)
{
$.writeln("BackgroundEmailer.close(): Error closing socket: " + error);
}
}
BackgroundEmailer.send = function()
{
var startDate = new Date();
$.writeln("IN BackgroundEmailer.send() = " + startDate);
if(BackgroundEmailer.Thumbnails.length != 0)
{
BackgroundEmailer.isRunning = true;
if(!BackgroundEmailer.connect(BackgroundEmailer.mailServerName , BackgroundEmailer.POP))
{
BackgroundEmailer.isRunning = false;
var endPremDate = new Date();
$.writeln("OUT (Premature exit: failed to connect) BackgroundEmailer.send() = " + endPremDate);
return;
}
BackgroundEmailer.authorise();
BackgroundEmailer.close();
if(BackgroundEmailer.connect(BackgroundEmailer.mailServerName , BackgroundEmailer.SMTP))
{
var welcome = BackgroundEmailer.socket.read();
$.writeln("BackgroundEmailer.send(): Sending message via: " + welcome.substring(4));
BackgroundEmailer.doCommand ("EHLO " + BackgroundEmailer.sender, "smtp");
BackgroundEmailer.doCommand ("MAIL FROM: " + BackgroundEmailer.sender, "smtp");
BackgroundEmailer.doCommand ("RCPT TO: " + BackgroundEmailer.recipient, "smtp");
BackgroundEmailer.doCommand ("DATA", "smtp");
BackgroundEmailer.socket.write ('From: "BackgroundEmailer" <' + BackgroundEmailer.sender + '>\r\n');
BackgroundEmailer.socket.write ("To: " + BackgroundEmailer.recipient + "\r\n");
BackgroundEmailer.socket.write ("Subject: " + BackgroundEmailer.subject + "\r\n");
BackgroundEmailer.socket.write ("Date: " + new Date().toString() + "\r\n");
BackgroundEmailer.socket.write("MIME-Version: 1.0\r\n");
BackgroundEmailer.socket.write("Content-Type: multipart/mixed;\r\n");
BackgroundEmailer.socket.write('\tboundary="' + BackgroundEmailer.boundary + '"\r\n');
BackgroundEmailer.socket.write("X-Mailer: Bridge Email Sample\r\n");
BackgroundEmailer.socket.write("\r\n");
BackgroundEmailer.socket.write("This is a multi-part mesage in MIME format.\r\n");
BackgroundEmailer.socket.write("\r\n");
BackgroundEmailer.socket.write("--" + BackgroundEmailer.boundary + "\r\n");
BackgroundEmailer.socket.write("Content-Type: text/plain;\r\n");
BackgroundEmailer.socket.write("Content-Transfer-Encoding: 7bit\r\n");
BackgroundEmailer.socket.write("\r\n");
BackgroundEmailer.socket.write(BackgroundEmailer.message + "\r\n");
BackgroundEmailer.socket.write("\r\n");
BackgroundEmailer.socket.write("--" + BackgroundEmailer.boundary + "\r\n");
BackgroundEmailer.startAttach = true;
BackgroundEmailer.encodeTaskID = app.scheduleTask('BackgroundEmailer.sendData()', 10, true);
}
}
var endDate = new Date();
$.writeln("OUT BackgroundEmailer.send() = " + endDate);
}
BackgroundEmailer.sendData = function()
{
app.document.status = "BackgroundEmailer.sendData is sending files...";
if(BackgroundEmailer.startAttach)
{
BackgroundEmailer.thumb = BackgroundEmailer.Thumbnails.pop();
var theFile = BackgroundEmailer.thumb.spec;
theFile.encoding = "binary";
theFile.open('r');
BackgroundEmailer.contents = theFile.read();
theFile.close();
BackgroundEmailer.socket.write("Content-type: " + BackgroundEmailer.thumb.mimeType + ";\r\n");
BackgroundEmailer.socket.write('\tname="' + BackgroundEmailer.thumb.name + '"\r\n');
BackgroundEmailer.socket.write("Content-Transfer-Encoding: base64\r\n");
BackgroundEmailer.socket.write("Content-Disposition: attachment;\r\n");
BackgroundEmailer.socket.write('\tfilename="' + BackgroundEmailer.thumb.name + '"\r\n');
BackgroundEmailer.socket.write("\r\n");
BackgroundEmailer.startAttach = false;
}
BackgroundEmailer.encodeString(BackgroundEmailer.nextChunk());
if(BackgroundEmailer.finishedAttachment)
{
if(BackgroundEmailer.Thumbnails.length != 0)
{
BackgroundEmailer.startAttach = true;
BackgroundEmailer.finishedAttachment = false
BackgroundEmailer.socket.write("--" + BackgroundEmailer.boundary + "\r\n");
return true;
}
else
{
app.scheduleTask('BackgroundEmailer.finishEmail()', 10, false);
app.cancelTask(BackgroundEmailer.encodeTaskID);
$.writeln("BackgroundEmailer.sendData(): just scheduled finishEmail");
return;
}
}
return true;
}
BackgroundEmailer.nextChunk = function()
{
var retval = "";
var dataLength = BackgroundEmailer.contents.length;
retval = BackgroundEmailer.contents.substring(0, 5760);
if(dataLength < 5760)
{
BackgroundEmailer.finishedAttachment = true;
}
else
{
BackgroundEmailer.contents = BackgroundEmailer.contents.substring(5760);
}
return retval;
}
BackgroundEmailer.encodeString = function(binaryString)
{
var encoded = "";
var c1, c2, c3;
var b1, b2, b3, b4;
var i = 0;
while(i < binaryString.length)
{
c1 = binaryString.charCodeAt(i++);
c2 = binaryString.charCodeAt(i++);
c3 = binaryString.charCodeAt(i++);
b1 = c1 >> 2;
b2 = ((c1 & 3) << 4) | (c2 >> 4);
b3 = ((c2 & 15) << 2) | (c3 >> 6);
b4 = c3 & 63;
if (isNaN(c2))
{
b3 = b4 = 64;
}
else if (isNaN(c3))
{
b4 = 64;
}
encoded = encoded + BackgroundEmailer.b64Chars.charAt(b1) + BackgroundEmailer.b64Chars.charAt(b2) +
BackgroundEmailer.b64Chars.charAt(b3) + BackgroundEmailer.b64Chars.charAt(b4);
}
while(encoded.length > 0)
{
BackgroundEmailer.socket.write(encoded.substring(0,72) + "\r\n");
encoded = encoded.substring(72);
}
}
BackgroundEmailer.finishEmail = function()
{
var startDate = new Date();
$.writeln("IN BackgroundEmailer.finishEmail() = " + startDate);
BackgroundEmailer.socket.write("\r\n");
BackgroundEmailer.socket.write("--" + BackgroundEmailer.boundary + "--\r\n");
retval = BackgroundEmailer.doCommand(".", "smtp");
BackgroundEmailer.doCommand("QUIT", "smtp");
BackgroundEmailer.close();
BackgroundEmailer.isRunning = false;
app.document.status = "BackgroundEmail has finished emailing files.";
var endDate = new Date();
$.writeln("OUT BackgroundEmailer.finishEmail() = " + endDate);
}
BackgroundEmailer.prototype.run = function()
{
if(!this.canRun())
{
return false;
}
try
{
var emailMenuItem = new MenuElement("command", "SDK BackgroundEmailer: Send by Email",
"at the end of Thumbnail", "BackgroundEmailMenu");
$.writeln("BackgroundEmailer.run(): created menu item, select file(s) and see its context-sensitive menu item");
emailMenuItem.onSelect = function(m)
{
if(BackgroundEmailer.isRunning)
{
$.writeln("BackgroundEmailer.run() : onSelect(): Error! Email already in progress");
}
else
{
var cachedSelections = app.document.selections;
BackgroundEmailer.Thumbnails = new Array();
for(var i = 0; i < cachedSelections.length; i++)
{
var nextThumbnail = cachedSelections[i];
if(!nextThumbnail.container)
{
$.writeln("BackgroundEmailer.run() : onSelect(): Adding " + nextThumbnail.uri);
BackgroundEmailer.Thumbnails.push(nextThumbnail);
}
}
if(BackgroundEmailer.Thumbnails.length > 0)
{
BackgroundEmailer.buildMessageBody();
BackgroundEmailer.send();
}
else
{
$.writeln("BackgroundEmailer.run() : onSelect(): Error! Need some selected file(s) to send");
}
}
}
emailMenuItem.onDisplay = function(m)
{
var selLength = app.document.selectionLength;
if(selLength > 0)
{
this.enabled = true;
}
else
{
this.enabled = false;
}
}
}
catch(error)
{
$.writeln("BackgroundEmailer.run() Error:" + error);
$.writeln("\tNote that you only need run this snippet once, then use its menu item");
$.writeln("\tto send files");
return false;
}
return true;
}
BackgroundEmailer.prototype.canRun = function()
{
var retval = false;
if(BridgeTalk.appName == "bridge")
{
retval = ! ( (BackgroundEmailer.username == "") || (BackgroundEmailer.password == "")
|| (BackgroundEmailer.sender == "") || (BackgroundEmailer.recipient == "") || (BackgroundEmailer.subject == "")
||(BackgroundEmailer.SMTP == 0) || (BackgroundEmailer.POP == 0)
|| (BackgroundEmailer.mailServerName == "") );
if(!retval)
{
$.writeln("ERROR:: - Cannot run BackgroundEmailer");
$.writeln("\tCheck mail server configuration settings.");
$.writeln("\tYou must customize these with your own mail server settings.");
}
return true;
}
$.writeln("ERROR:: Cannot run BackgroundEmailer");
$.writeln(this.requiredContext);
return false;
}
if(typeof(BackgroundEmailer_unitTest) == "undefined") {
new BackgroundEmailer().run();
}
http://www.adobe.com/devnet/bridge
Documentation generated by
JSDoc on Tue Apr 27 10:21:34 2010