Guest User

Untitled

a guest
Jul 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. var tcp = require('tcp');
  2. var sys = require('sys');
  3.  
  4. var SMTPClient = function(options){
  5. this.options = options || this.defaultOptions();
  6. };
  7. SMTPClient.prototype = {
  8. defaultOptions: function() {
  9. return {
  10. from:"",
  11. subject:""
  12. }
  13. },
  14. sendmail:function (to, body){
  15. var promise = new process.Promise();
  16. var self = this;
  17.  
  18. this.connection = tcp.createConnection(25);
  19. this.connection.addListener("connect", function (socket) {
  20. self.connection.send("helo localhost\r\n");
  21. self.connection.send("mail from: " + self.options.from + "\r\n");
  22. self.connection.send("rcpt to: " + to + "\r\n");
  23. self.connection.send("data\r\n");
  24. self.connection.send("From: " + self.options.from + "\r\n");
  25. self.connection.send("To: " + to + "\r\n");
  26. self.connection.send("Subject: " + self.options.subject + "\r\n");
  27. self.connection.send("Content-Type: text/html\r\n");
  28. self.connection.send(self.wordwrap(body) + "\r\n");
  29. self.connection.send(".\r\n");
  30. self.connection.send("quit\r\n");
  31. self.connection.close();
  32. });
  33.  
  34. this.connection.addListener("receive", function (data) {
  35. if(self.parseResponse(data)){
  36. promise.emitSuccess("Email sent!");
  37. sys.puts("SUCC");
  38. }else{
  39. promise.emitError("EMAIL ERROR");
  40. sys.puts("ERR");
  41. }
  42. sys.puts(data);
  43. });
  44. },
  45. parseResponse:function(data){
  46. var d = data.split("\r\n");
  47. d.forEach(function(itm){
  48. if(itm.indexOf("250 OK id=") != -1){
  49. return true;
  50. }
  51. });
  52. return false;
  53. },
  54. wordwrap:function(str){
  55. var m = 80;
  56. var b = "\r\n";
  57. var c = false;
  58. var i, j, l, s, r;
  59. str += '';
  60. if (m < 1) {
  61. return str;
  62. }
  63. for (i = -1, l = (r = str.split(/\r\n|\n|\r/)).length; ++i < l; r[i] += s) {
  64. for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")){
  65. 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;
  66. }
  67. }
  68. return r.join("\n");
  69. }
  70. }
  71.  
  72. exports.SMTPClient = SMTPClient;
Add Comment
Please, Sign In to add comment