Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var tcp = require('tcp');
- var sys = require('sys');
- var SMTPClient = function(options){
- this.options = options || this.defaultOptions();
- };
- SMTPClient.prototype = {
- defaultOptions: function() {
- return {
- from:"",
- subject:""
- }
- },
- sendmail:function (to, body){
- var promise = new process.Promise();
- var self = this;
- this.connection = tcp.createConnection(25);
- this.connection.addListener("connect", function (socket) {
- self.connection.send("helo localhost\r\n");
- self.connection.send("mail from: " + self.options.from + "\r\n");
- self.connection.send("rcpt to: " + to + "\r\n");
- self.connection.send("data\r\n");
- self.connection.send("From: " + self.options.from + "\r\n");
- self.connection.send("To: " + to + "\r\n");
- self.connection.send("Subject: " + self.options.subject + "\r\n");
- self.connection.send("Content-Type: text/html\r\n");
- self.connection.send(self.wordwrap(body) + "\r\n");
- self.connection.send(".\r\n");
- self.connection.send("quit\r\n");
- self.connection.close();
- });
- this.connection.addListener("receive", function (data) {
- if(self.parseResponse(data)){
- promise.emitSuccess("Email sent!");
- sys.puts("SUCC");
- }else{
- promise.emitError("EMAIL ERROR");
- sys.puts("ERR");
- }
- sys.puts(data);
- });
- },
- parseResponse:function(data){
- var d = data.split("\r\n");
- d.forEach(function(itm){
- if(itm.indexOf("250 OK id=") != -1){
- return true;
- }
- });
- return false;
- },
- wordwrap:function(str){
- var m = 80;
- var b = "\r\n";
- var c = false;
- var i, j, l, s, r;
- str += '';
- if (m < 1) {
- return str;
- }
- for (i = -1, l = (r = str.split(/\r\n|\n|\r/)).length; ++i < l; r[i] += s) {
- for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")){
- j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
- }
- }
- return r.join("\n");
- }
- }
- exports.SMTPClient = SMTPClient;
Add Comment
Please, Sign In to add comment