diff -urN qmail-1.03.orig/qmail-remote.8 qmail-1.03-qmtpc+mxps/qmail-remote.8
--- qmail-1.03.orig/qmail-remote.8	Mon Jun 15 04:53:16 1998
+++ qmail-1.03-qmtpc+mxps/qmail-remote.8	Mon Jul  7 12:54:00 2003
@@ -1,6 +1,6 @@
 .TH qmail-remote 8
 .SH NAME
-qmail-remote \- send mail via SMTP
+qmail-remote \- send mail via SMTP or QMTP
 .SH SYNOPSIS
 .B qmail-remote
 .I host
@@ -26,7 +26,11 @@
 or to a mail exchanger for
 .I host
 listed in the Domain Name System,
-via the Simple Mail Transfer Protocol (SMTP).
+via the Simple Mail Transfer Protocol (SMTP)
+or Quick Mail Transfer Protocol (QMTP).
+Choice of protocol in this version is done only using the MXPS
+(Mail Exchanger Protocol Switch) convention; see http://cr.yp.to/im/mxps.html
+for details.
 .I host
 can be either a fully-qualified domain name:
 
@@ -54,6 +58,7 @@
 .B getopt
 standard.
 .SH TRANSPARENCY
+QMTP is length-prefixed and 8-bit clean, and so is completely transparent.
 End-of-file in SMTP is encoded as dot CR LF.
 A dot at the beginning of a line is encoded as dot dot.
 It is impossible in SMTP to send a message that does not end with a newline.
diff -urN qmail-1.03.orig/qmail-remote.c qmail-1.03-qmtpc+mxps/qmail-remote.c
--- qmail-1.03.orig/qmail-remote.c	Mon Jun 15 04:53:16 1998
+++ qmail-1.03-qmtpc+mxps/qmail-remote.c	Mon Jul  7 14:05:57 2003
@@ -1,4 +1,5 @@
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -14,6 +15,7 @@
 #include "dns.h"
 #include "alloc.h"
 #include "quote.h"
+#include "fmt.h"
 #include "ip.h"
 #include "ipalloc.h"
 #include "ipme.h"
@@ -31,8 +33,11 @@
 
 #define HUGESMTPTEXT 5000
 
-#define PORT_SMTP 25 /* silly rabbit, /etc/services is for users */
-unsigned long port = PORT_SMTP;
+/* silly rabbit, /etc/services is for users */
+#define PORT_SMTP 25
+#define PORT_QMTP 209
+unsigned long smtp_port = PORT_SMTP;
+unsigned long qmtp_port = PORT_QMTP;
 
 GEN_ALLOC_typedef(saa,stralloc,sa,len,a)
 GEN_ALLOC_readyplus(saa,stralloc,sa,len,a,i,n,x,10,saa_readyplus)
@@ -70,6 +75,8 @@
 Unable to switch to home directory. (#4.3.0)\n"); zerodie(); }
 void temp_control() { out("Z\
 Unable to read control files. (#4.3.0)\n"); zerodie(); }
+void temp_proto() { out("Z\
+recipient did not talk proper QMTP (#4.3.0)\n"); zerodie(); }
 void perm_partialline() { out("D\
 SMTP cannot transfer messages with partial final lines. (#5.6.2)\n"); zerodie(); }
 void perm_usage() { out("D\
@@ -273,6 +280,115 @@
   quit("K"," accepted message");
 }
 
+int qmtp_priority(int pref)
+{
+  if (pref < 12800) return 0;
+  if (pref > 13055) return 0;
+  if (pref % 16 == 1) return 1;
+  return 0;
+}
+
+void qmtp() /* thanks to russell nelson */
+{
+  struct stat st;
+  unsigned long len;
+  int len2;
+  char *x;
+  int i;
+  int n;
+  unsigned char ch;
+  char num[FMT_ULONG];
+  int flagallok;
+
+  if (fstat(0,&st) == -1) quit("Z", " unable to fstat stdin");
+  len = st.st_size;
+
+  /* the following code was substantially taken from serialmail's serialqmtp.c */
+  substdio_put(&smtpto,num,fmt_ulong(num,len+1));
+  substdio_put(&smtpto,":\n",2);
+  while (len > 0) {
+    n = substdio_feed(&ssin);
+    if (n <= 0) _exit(32); /* wise guy again */
+    x = substdio_PEEK(&ssin);
+    substdio_put(&smtpto,x,n);
+    substdio_SEEK(&ssin,n);
+    len -= n;
+  }
+  substdio_put(&smtpto,",",1);
+
+  len = sender.len;
+  substdio_put(&smtpto,num,fmt_ulong(num,len));
+  substdio_put(&smtpto,":",1);
+  substdio_put(&smtpto,sender.s,sender.len);
+  substdio_put(&smtpto,",",1);
+
+  len = 0;
+  for (i = 0;i < reciplist.len;++i)
+    len += fmt_ulong(num,reciplist.sa[i].len) + 1 + reciplist.sa[i].len + 1;
+  substdio_put(&smtpto,num,fmt_ulong(num,len));
+  substdio_put(&smtpto,":",1);
+  for (i = 0;i < reciplist.len;++i) {
+    substdio_put(&smtpto,num,fmt_ulong(num,reciplist.sa[i].len));
+    substdio_put(&smtpto,":",1);
+    substdio_put(&smtpto,reciplist.sa[i].s,reciplist.sa[i].len);
+    substdio_put(&smtpto,",",1);
+  }
+  substdio_put(&smtpto,",",1);
+  substdio_flush(&smtpto);
+
+  flagallok = 1;
+
+  for (i = 0;i < reciplist.len;++i) {
+    len = 0;
+    for (;;) {
+      get(&ch);
+      if (ch == ':') break;
+      if (len > 200000000) temp_proto();
+      if (ch - '0' > 9) temp_proto();
+      len = 10 * len + (ch - '0');
+    }
+    if (!len) temp_proto();
+    get(&ch); --len;
+    if ((ch != 'Z') && (ch != 'D') && (ch != 'K')) temp_proto();
+
+    if (!stralloc_copyb(&smtptext,&ch,1)) temp_proto();
+    if (!stralloc_cats(&smtptext,"Remote host said: ")) temp_nomem();
+
+    while (len > 0) {
+      get(&ch);
+      --len;
+    }
+
+    for (len = 0;len < smtptext.len;++len) {
+      ch = smtptext.s[len];
+      if ((ch < 32) || (ch > 126)) smtptext.s[len] = '?';
+    }
+    get(&ch);
+    if (ch != ',') temp_proto();
+    smtptext.s[smtptext.len-1] = '\n';
+
+    if (smtptext.s[0] == 'K') out("r");
+    else if (smtptext.s[0] == 'D') {
+      out("h");
+      flagallok = 0;
+    }
+    else { /* if (smtptext.s[0] == 'Z') */
+      out("s");
+      flagallok = 0;
+    }
+    /* if (substdio_put(subfdoutsmall,smtptext.s+1,smtptext.len-1) == -1) temp_noconn(); */
+    zero();
+  }
+  if (!flagallok) {
+    out("DGiving up on ");outhost();out("\nProtocol: qmtp\n");
+  } else {
+    out("K");outhost();out(" accepted message.\n");
+  }
+  if (substdio_put(subfdoutsmall,smtptext.s+1,smtptext.len-1) == -1) _exit(0);
+  out("Protocol: qmtp\n");
+  zerodie();
+}
+
 stralloc canonhost = {0};
 stralloc canonbox = {0};
 
@@ -357,7 +473,7 @@
   if (relayhost) {
     i = str_chr(relayhost,':');
     if (relayhost[i]) {
-      scan_ulong(relayhost + i + 1,&port);
+      scan_ulong(relayhost + i + 1,&smtp_port);
       relayhost[i] = 0;
     }
     if (!stralloc_copys(&host,relayhost)) temp_nomem();
@@ -414,7 +530,18 @@
     smtpfd = socket(AF_INET,SOCK_STREAM,0);
     if (smtpfd == -1) temp_oserr();
  
-    if (timeoutconn(smtpfd,&ip.ix[i].ip,(unsigned int) port,timeoutconnect) == 0) {
+    if (qmtp_priority(ip.ix[i].pref)) {
+      if (timeoutconn(smtpfd,&ip.ix[i].ip,(unsigned int) qmtp_port,timeoutconnect) == 0) {
+        tcpto_err(&ip.ix[i].ip,0);
+        partner = ip.ix[i].ip;
+        qmtp(); /* does not return */
+      }
+      /* logging un-final things is too hard */
+    }
+    close(smtpfd);
+    smtpfd = socket(AF_INET,SOCK_STREAM,0);
+    if (smtpfd == -1) temp_oserr(); /* thanks to ian lance taylor */
+    if (timeoutconn(smtpfd,&ip.ix[i].ip,(unsigned int) smtp_port,timeoutconnect) == 0) {
       tcpto_err(&ip.ix[i].ip,0);
       partner = ip.ix[i].ip;
       smtp(); /* does not return */

