By request, SASL PLAIN for IMAP and SMTP.
[squirrelmail.git] / class / deliver / Deliver_SMTP.class.php
index 546d612c4a6832bf672819874a1caa7b45d5de00..a70be716622dd46b056997907398d91e2f6faf22 100644 (file)
@@ -50,27 +50,35 @@ class Deliver_SMTP extends Deliver {
            return(0);
        }
 
-       /* If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP
-          server.  This should fix the DNS issues some people have had */
+       /* 
+     * If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP
+        * server.  This should fix the DNS issues some people have had 
+     */
        if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER)) { // HTTP_HOST is set
+        // optionally trim off port number
+        if($p = strrpos($HTTP_HOST, ':')) {
+            $HTTP_HOST = substr($HTTP_HOST, 0, $p);
+        }
                $helohost = $HTTP_HOST;
        } else { // For some reason, HTTP_HOST is not set - revert to old behavior
                $helohost = $domain;
        }
        
        /* Lets introduce ourselves */
+    fputs($stream, "EHLO $helohost\r\n");
+    $tmp = fgets($stream,1024);
+    if ($this->errorCheck($tmp,$stream)) {
+        return(0);
+    }
+
        if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
          // Doing some form of non-plain auth
-         fputs($stream, "EHLO $helohost\r\n");
-         $tmp = fgets($stream,1024);
-         if ($this->errorCheck($tmp,$stream)) {
-           return(0);
-         }
          if ($smtp_auth_mech == 'cram-md5') {
            fputs($stream, "AUTH CRAM-MD5\r\n");
          } elseif ($smtp_auth_mech == 'digest-md5') {
            fputs($stream, "AUTH DIGEST-MD5\r\n");
          }
+
          $tmp = fgets($stream,1024);
           
          if ($this->errorCheck($tmp,$stream)) {
@@ -114,11 +122,6 @@ class Deliver_SMTP extends Deliver {
          }
        } elseif ($smtp_auth_mech == 'login') {
          // The LOGIN method
-      fputs($stream, "EHLO $helohost\r\n");
-      $tmp = fgets($stream, 1024);
-         if ($this->errorCheck($tmp, $stream)) {
-       return(0);
-         }
       fputs($stream, "AUTH LOGIN\r\n");
       $tmp = fgets($stream, 1024);
 
@@ -136,7 +139,25 @@ class Deliver_SMTP extends Deliver {
          if ($this->errorCheck($tmp, $stream)) {
        return(0);
          }
-       } else {
+       } elseif ($smtp_auth_mech == "plain") {
+      /* SASL Plain */
+      $auth = base64_encode("$username\0$username\0$pass");
+                  
+      $query = "AUTH PLAIN\r\n";
+      fputs($stream, $query);
+      $read=fgets($stream, 1024);
+
+      if (substr($read,0,3) == '334') { // OK so far..
+         fputs($stream, "$auth\r\n");
+         $read = fgets($stream, 1024);
+      }
+                
+      $results=explode(" ",$read,3);
+      $response=$results[1];
+      $message=$results[2];
+
+      
+    } else {
                /* Right here, they've reached an unsupported auth mechanism.
                   This is the ugliest hack I've ever done, but it'll do till I can fix
                   things up better tomorrow.  So tired... */
@@ -146,7 +167,7 @@ class Deliver_SMTP extends Deliver {
     }
     
        /* Ok, who is sending the message? */
-        fputs($stream, 'MAIL FROM: <'.$from->mailbox.'@'.$from->host.">\r\n");
+        fputs($stream, 'MAIL FROM:<'.$from->mailbox.'@'.$from->host.">\r\n");
         $tmp = fgets($stream, 1024);
        if ($this->errorCheck($tmp, $stream)) {
            return(0);
@@ -156,7 +177,7 @@ class Deliver_SMTP extends Deliver {
        for ($i = 0, $cnt = count($to); $i < $cnt; $i++) {
            if (!$to[$i]->host) $to[$i]->host = $domain;
            if ($to[$i]->mailbox) {
-               fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
+               fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
                $tmp = fgets($stream, 1024);
                if ($this->errorCheck($tmp, $stream)) {
                    return(0);
@@ -167,17 +188,18 @@ class Deliver_SMTP extends Deliver {
        for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) {      
            if (!$cc[$i]->host) $cc[$i]->host = $domain;
            if ($cc[$i]->mailbox) {
-               fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
+               fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
                $tmp = fgets($stream, 1024);
                if ($this->errorCheck($tmp, $stream)) {
                    return(0);
                }
            }
        }
+
        for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) {
            if (!$bcc[$i]->host) $bcc[$i]->host = $domain;
            if ($bcc[$i]->mailbox) {
-               fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
+               fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
                $tmp = fgets($stream, 1024);
                if ($this->errorCheck($tmp, $stream)) {
                    return(0);
@@ -286,17 +308,17 @@ class Deliver_SMTP extends Deliver {
                   . " $err_no : $err_str");
         } else {
             $tmp = fgets($popConnection, 1024); /* banner */
-            if (!eregi("^\+OK", $tmp, $regs)) {
+            if (substr($tmp, 0, 3) != '+OK') {
                 return(0);
             }
             fputs($popConnection, "USER $user\r\n");
             $tmp = fgets($popConnection, 1024);
-            if (!eregi("^\+OK", $tmp, $regs)) {
+            if (substr($tmp, 0, 3) != '+OK') {
                 return(0);
             }
             fputs($popConnection, 'PASS ' . $pass . "\r\n");
             $tmp = fgets($popConnection, 1024);
-            if (!eregi("^\+OK", $tmp, $regs)) {
+            if (substr($tmp, 0, 3) != '+OK') {
                 return(0);
             }
             fputs($popConnection, "QUIT\r\n"); /* log off */