Permalink
Browse files

Allow SIZE with MAIL

  • Loading branch information...
andris9 committed Jan 16, 2014
1 parent 951850f commit 3b4040282a41c90278cddc8e626d578996ca62a3
Showing with 54 additions and 3 deletions.
  1. +43 −0 examples/size.js
  2. +11 −3 lib/server.js
View
@@ -0,0 +1,43 @@
+var simplesmtp = require("../index"),
+ fs = require("fs");
+
+// Example for http://tools.ietf.org/search/rfc1870
+
+var allowedRecipientDomains = ["node.ee", "neti.ee"];
+
+var smtp = simplesmtp.createServer({
+ maxSize: 5000, // maxSize must be set in order to support SIZE
+ disableDNSValidation: true
+});
+smtp.listen(25);
+
+// Set up recipient validation function
+smtp.on("validateRecipient", function(connection, email, done){
+
+ // SIZE value can be found from connection.messageSize
+ if(typeof connection.messageSize == "number" && connection.messageSize > 100){
+ var err = new Error("Max space reached");
+ // anything in the SMTPResponse will be reported back to user. If not set, default error message wil, be used
+ err.SMTPResponse = "452 Insufficient channel storage: " + email;
+ done(err);
+ }else{
+ done();
+ }
+});
+
+smtp.on("startData", function(connection){
+ connection.saveStream = fs.createWriteStream("/tmp/message.txt");
+});
+
+smtp.on("data", function(connection, chunk){
+ connection.saveStream.write(chunk);
+});
+
+smtp.on("dataReady", function(connection, done){
+ connection.saveStream.end();
+ done();
+
+ console.log("Delivered message by " + connection.from +
+ " to " + connection.to.join(", ") + ", sent from " + connection.host +
+ " (" + connection.remoteAddress + ")");
+});
View
@@ -323,7 +323,8 @@ SMTPServerConnection.prototype._onCommandMAIL = function(mail){
var self = this,
match,
email,
- domain;
+ domain,
+ messageSize;
if(!this.hostNameAppearsAs){
return this.client.send("503 5.5.1 Error: send HELO/EHLO first");
@@ -341,6 +342,12 @@ SMTPServerConnection.prototype._onCommandMAIL = function(mail){
return this.client.send("501 5.1.7 Bad sender address syntax");
}
+ if(this.server.options.maxSize){
+ mail.replace(/> size=(\d+)\b\s*/i, function(o, size){
+ messageSize = Number(size) || 0;
+ });
+ }
+
email = match[1] || "";
domain = (match[2] || "").toLowerCase();
@@ -350,6 +357,7 @@ SMTPServerConnection.prototype._onCommandMAIL = function(mail){
}
email = email.substr(0, email.length - domain.length) + domain;
self.envelope.from = email;
+ self.envelope.messageSize = messageSize;
self.client.send("250 2.1.0 Ok");
});
};
@@ -438,7 +446,7 @@ SMTPServerConnection.prototype._validateAddress = function (addressType, email,
if(this.server.listeners(validateEvent).length){
this.server.emit(validateEvent, this.envelope, email, (function(err){
if(err){
- return callback(new Error(localErrorMessage));
+ return callback(new Error(err.SMTPResponse || localErrorMessage));
}
return callback();
}).bind(this));
@@ -451,7 +459,7 @@ SMTPServerConnection.prototype._validateAddress = function (addressType, email,
dnslib.resolveMx(domain, (function(err, addresses){
if(err || !addresses || !addresses.length){
this.server.emit(validationFailedEvent, email);
- return callback(new Error(dnsErrorMessage));
+ return callback(new Error(err && err.SMTPResponse || dnsErrorMessage));
}
validateViaLocal.call(this);
}).bind(this));

0 comments on commit 3b40402

Please sign in to comment.