Browse Source

Add helo and login

Pascal Peridont 19 years ago
parent
commit
ffc62ee2de
1 changed files with 51 additions and 3 deletions
  1. 51 3
      std/mtwin/mail/Smtp.hx

+ 51 - 3
std/mtwin/mail/Smtp.hx

@@ -29,7 +29,7 @@ import mtwin.mail.Exception;
 
 
 class Smtp {
 class Smtp {
 
 
-	public static function send( host : String, from : String, to : String, data : String, ?port: Int ){
+	public static function send( host : String, from : String, to : String, data : String, ?port: Int, ?user: String, ?password: String ){
 		if( port == null ) port = 25;
 		if( port == null ) port = 25;
 
 
 		var cnx = new Socket();
 		var cnx = new Socket();
@@ -41,10 +41,58 @@ class Smtp {
 		}
 		}
 		
 		
 		// get server init line
 		// get server init line
-		cnx.input.readLine();
+		var ret = StringTools.trim(cnx.input.readLine());
+		if ( StringTools.endsWith(ret, "ESMTP" ) ) { //if server support extensions
+			//EHLO
+			cnx.write( "EHLO " + Socket.localhost() + "\r\n");
+			ret = "";
+			var supportLoginAuth = false;
+			
+			do {
+				ret = StringTools.trim(cnx.input.readLine());
+				if( ret.substr(0,3) != "250" ){
+					cnx.close();
+					throw SmtpMailFromError(ret);
+				} else if ( ret.substr(4, 4) == "AUTH" && ret.indexOf("LOGIN") != -1) {
+					supportLoginAuth = true;
+				}
+			} while(ret.substr(0,4) != "250 ");
+
+			if ( supportLoginAuth && user != null ) { //if server support AUTH LOGIN and we have user to login with
+				var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
+				cnx.write( "AUTH LOGIN\r\n" );
+				ret = StringTools.trim(cnx.input.readLine());
+				if( ret.substr(0,3) != "334" ){
+					cnx.close();
+					throw SmtpMailFromError(ret);
+				}
+				
+				cnx.write( StringTools.baseEncode(user, base64Alphabet) + "\r\n" );
+				ret = StringTools.trim(cnx.input.readLine());
+				if( ret.substr(0,3) != "334" ){
+					cnx.close();
+					throw SmtpMailFromError(ret);
+				}
+				cnx.write( StringTools.baseEncode(password, base64Alphabet) + "\r\n" );
+				ret = StringTools.trim(cnx.input.readLine());
+				if( ret.substr(0,3) != "235" ){
+					cnx.close();
+					throw SmtpMailFromError(ret);
+				}
+			}
+		} else {
+			//HELO
+			cnx.write( "HELO " + Socket.localhost() + "\r\n");
+			ret = StringTools.trim(cnx.input.readLine());
+			if( ret.substr(0,3) != "250" ){
+				cnx.close();
+				throw SmtpMailFromError(ret);
+			}
+		}
+
 
 
 		cnx.write( "MAIL FROM:<" + from + ">\r\n" );
 		cnx.write( "MAIL FROM:<" + from + ">\r\n" );
-		var ret = StringTools.trim(cnx.input.readLine());
+		ret = StringTools.trim(cnx.input.readLine());
 		if( ret.substr(0,3) != "250" ){
 		if( ret.substr(0,3) != "250" ){
 			cnx.close();
 			cnx.close();
 			throw SmtpMailFromError(ret);
 			throw SmtpMailFromError(ret);