BasicEmailer.jsx
Summary
Shows how to use the Socket object to send emails with attachments from Bridge.
See:
Class Summary
|
BasicEmailer |
Shows how to use the Socket object to send emails with attachments from Bridge. |
function BasicEmailer()
{
this.requiredContext = "\tEnsure that Bridge is running and set as the target and that you have supplied your own mail server details.\n";
this.mailServerName = "";
this.username = "";
this.password = "";
this.sender = "";
this.recipient = "";
this.subject = "Email from Bridge via BasicEmailer.jsx";
this.SMTP = 25;
this.POP = 110;
this.socket = new Socket();
this.message = "Hello World";
this.boundary = "****=_NextPartMyBody_0000 " + new Date().getSeconds();
this.socket.encoding = "binary";
}
BasicEmailer.prototype.buildMessage = function()
{
var sl = app.document.selections;
var message = "BasicEmailer sent the following attachments:\r\n";
for(var i = 0;i < sl.length;i++)
{
if(!sl[i].container)
{
message += "\r\n\tName: " + sl[i].name;
}
}
this.message = message;
}
BasicEmailer.prototype.connect = function(host, port)
{
var result = true;
if(!this.socket.open(host + ":" + port))
{
$.writeln("BasicEmailer: Error - Could not open socket");
result = false;
}
return result;
}
BasicEmailer.prototype.authorise = function()
{
var startDate = new Date();
$.writeln("IN BasicEmailer.authorise() = " + startDate);
try
{
$.writeln("BasicEmailer: Authorizing..." + this.socket.read());
this.doCommand("USER " + this.username, "pop")
this.doCommand("PASS " + this.password, "pop");
this.doCommand ("QUIT", "pop");
}
catch(error)
{
$.writeln("BasicEmailer: Error - " + error);
result = false;
}
var endDate = new Date();
$.writeln("OUT BasicEmailer.authorise() = " + endDate);
}
BasicEmailer.prototype.doCommand = function(cmd, type)
{
var cmndOK = true;
var reply = "";
var replyCode = "";
this.socket.write(cmd + "\r\n");
$.sleep(300);
if(type == "pop")
{
reply = this.socket.read();
replyCode = reply.substring(0, 1);
if(replyCode == "-")
{
cmndOK = false;
$.writeln("BasicEmailer: Error with POP command:");
$.writeln("\t" + reply);
this.close();
}
}
else if(type == "smtp")
{
if(!this.socket.read())
{
cmndOK = false;
$.writeln("BasicEmailer: Error sending mail:");
$.writeln("\t" + reply);
this.close();
}
}
else
{
$.writeln("BasicEmailer: Unknown command type. Closing the socket.");
this.close();
}
if(!cmndOK) throw "Command error for: " + type + " - " + cmd;
return cmndOK;
}
BasicEmailer.prototype.close = function()
{
try
{
return this.socket.close();
}
catch(error){ $.writeln("BasicEmailer: Error closing socket: " + error); }
}
BasicEmailer.prototype.send = function()
{
var startDate = new Date();
$.writeln("IN BasicEmailer.send() = " + startDate);
var retval = false;
$.writeln("BasicEmailer.send(): Connecting ...");
if(!this.connect(this.mailServerName, this.POP))
{
var endDate = new Date();
$.writeln("OUT BasicEmailer.send() (failed to connect) = " + endDate);
return retval;
}
this.authorise();
this.close();
if(this.connect(this.mailServerName, this.SMTP))
{
try
{
var welcome = this.socket.read();
$.writeln("BasicEmailer.send(): Sending message via: " + welcome.substring(4));
this.doCommand ("EHLO " + this.sender, "smtp");
this.doCommand ("MAIL FROM: " + this.sender, "smtp");
this.doCommand ("RCPT TO: " + this.recipient, "smtp");
this.doCommand ("DATA", "smtp");
this.socket.write ('From: "BasicEmailer" <' + this.sender + '>\r\n');
this.socket.write ("To: " + this.recipient + "\r\n");
this.socket.write ("Subject: " + this.subject + "\r\n");
this.socket.write ("Date: " + new Date().toString() + "\r\n");
this.socket.write("MIME-Version: 1.0\r\n");
this.socket.write("Content-Type: multipart/mixed;\r\n");
this.socket.write('\tboundary="' + this.boundary + '"\r\n');
this.socket.write("X-Mailer: Bridge Email Sample\r\n");
this.socket.write("\r\n");
this.socket.write("This is a multi-part mesage in MIME format.\r\n");
this.socket.write("\r\n");
this.socket.write("--" + this.boundary + "\r\n");
this.socket.write("Content-Type: text/plain;\r\n");
this.socket.write("Content-Transfer-Encoding: 7bit\r\n");
this.socket.write("\r\n");
this.socket.write(this.message + "\r\n");
this.socket.write("\r\n");
this.socket.write("--" + this.boundary + "\r\n");
this.addAttachment();
retval = this.doCommand(".", "smtp");
this.doCommand("QUIT", "smtp");
this.close();
var endDate = new Date();
$.writeln("OUT BasicEmailer.send() = " + endDate);
return retval;
}
catch (e)
{
$.writeln("BasicEmailer: ERROR: " + e);
this.close();
}
}
var endDate = new Date();
$.writeln("OUT BasicEmailer.send() = " + endDate);
return retval;
}
BasicEmailer.prototype.addAttachment = function()
{
var startDate = new Date();
$.writeln("IN BasicEmailer.addAttachment() = " + startDate);
var sels = app.document.selections;
if(sels.length != 0)
{
for(var j = 0;j < sels.length;j++)
{
var thumb = sels[j];
if(thumb.container) continue;
var theFile = thumb.spec;
theFile.encoding = "binary";
theFile.open("r");
var contents = theFile.read();
theFile.close();
this.socket.write("Content-type: " + thumb.mimeType + ";\r\n");
this.socket.write('\tname="' + thumb.name + '"\r\n');
this.socket.write("Content-Transfer-Encoding: base64\r\n");
this.socket.write("Content-Disposition: attachment;\r\n");
this.socket.write('\tfilename="' + thumb.name + '"\r\n');
this.socket.write("\r\n");
var encodedData = this.encodeAttachment(contents);
var dataLine = "";
var counter = 0;
for(var i = 0;i < encodedData.length;i++)
{
dataLine += encodedData[i];
counter++;
if(counter > 75)
{
this.socket.write(dataLine + "\r\n");
dataLine = "";
counter = 0;
}
}
this.socket.write(dataLine + "\r\n");
this.socket.write("\r\n");
this.socket.write("--" + this.boundary + "\r\n");
}
this.socket.write("--" + this.boundary + "--\r\n");
}
var endDate = new Date();
$.writeln("OUT BasicEmailer.addAttachment() = " + endDate);
}
BasicEmailer.prototype.encodeAttachment = function(binaryString)
{
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var encoded = "";
var c1, c2, c3;
var e1, e2, e3, e4;
var i = 0;
while(i < binaryString.length)
{
c1 = binaryString.charCodeAt(i++);
c2 = binaryString.charCodeAt(i++);
c3 = binaryString.charCodeAt(i++);
e1 = c1 >> 2;
e2 = ((c1 & 3) << 4) | (c2 >> 4);
e3 = ((c2 & 15) << 2) | (c3 >> 6);
e4 = c3 & 63;
if (isNaN(c2))
{
e3 = e4 = 64;
}
else if (isNaN(c3))
{
e4 = 64;
}
encoded = encoded + keyStr.charAt(e1) + keyStr.charAt(e2) +
keyStr.charAt(e3) + keyStr.charAt(e4);
}
return encoded;
}
BasicEmailer.prototype.run = function() {
if(!this.canRun())
{
return false;
}
try
{
var emailCommand = new MenuElement("command", "SDK BasicEmailer: Send by Email", "at the end of Thumbnail", "BasicEmailMenu");
$.writeln("BasicEmailer.run(): created menu item, select file(s) and see its context-sensitive menu item");
var that = this;
emailCommand.onSelect = function(m)
{
that.buildMessage();
if(that.send())
{
$.writeln("BasicEmailer: Email sent successfully");
}
else
{
$.writeln("BasicEmailer: Email message not sent!");
}
}
emailCommand.onDisplay = function(m)
{
var s = app.document.selections;
if(s.length > 0)
{
this.enabled = true;
}
else
{
this.enabled = false;
}
}
$.writeln("BasicEmailer loaded.");
}
catch(error)
{
$.writeln("BasicEmailer.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;
}
BasicEmailer.prototype.canRun = function()
{
var retval = false;
if(BridgeTalk.appName == "bridge")
{
retval = ! ( (this.username == "") || (this.password == "")
|| (this.sender == "") || (this.recipient == "") || (this.subject == "")
||(this.SMTP == 0) || (this.POP == 0)
|| (this.mailServerName == "") );
if(!retval)
{
$.writeln("BasicEmailer: ERROR - Cannot run BasicEmailer");
$.writeln("BasicEmailer: Check your mail server configuration settings.");
}
return retval;
}
$.writeln("BasicEmailer: ERROR - Cannot run BasicEmailer");
$.writeln("BasicEmailer: " + this.requiredContext);
return retval;
}
if(typeof(BasicEmailer_unitTest) == "undefined") {
new BasicEmailer().run();
}
http://www.adobe.com/devnet/bridge
Documentation generated by
JSDoc on Tue Apr 27 10:21:34 2010