Subject: bug#38066: 26.1; X-Message-SMTP-Method header does not affect smtpmail-stream-type

From: Robert Pluim <[email-masked]>
Date: Tue, 05 Nov 2019 11:18:21 +0100

>>>>> On Mon, 04 Nov 2019 23:45:52 -0500, Ryan Tate said:

    Ryan> I believe the ideal solution here is for emacs to either auto-associate
    Ryan> ports with their usual stream types (465->'ssl, 587->'starttls, 25->nil)
    Ryan> or provide a mechanism for stream type to be specified in the header as
    Ryan> the fifth value (after method, machine, port, alt username). Or
    Ryan> auto-associate by default and provide a config variable through which to
    Ryan> override stream type by machine and port.

Iʼm not so sure about the auto association. How about we allow people
to ask for auto-detection? Something like the following (and then we
can discuss whether to turn it on by default, and I can argue that
STARTTLS is evil and we should force everyone to use 465 :-) )

Alternatively we could put this kind of code in open-network-stream,
to allow other protocols to benefit from the same kind of detection.

diff --git a/lisp/mail/smtpmail.el b/lisp/mail/smtpmail.el
index 9cf28fbe8a..75b15a99aa 100644
--- a/lisp/mail/smtpmail.el
+++ b/lisp/mail/smtpmail.el
@@ -98,11 +98,13 @@ smtpmail-stream-type
   "Type of SMTP connections to use.
 This may be either nil (upgrade with STARTTLS if possible),
 `starttls' (refuse to send if STARTTLS isn't available),
-`plain' (never use STARTTLS), or `ssl' (to use TLS/SSL)."
-  :version "24.1"
+`plain' (never use STARTTLS), or `ssl' (to use TLS/SSL), or
+`auto' to derive the stream-type from the port number."
+  :version "27.1"
   :type '(choice (const :tag "Possibly upgrade to STARTTLS" nil)
 		 (const :tag "Always use STARTTLS" starttls)
 		 (const :tag "Never use STARTTLS" plain)
+                 (const :tag "Derive from port number" auto)
 		 (const :tag "Use TLS/SSL" ssl)))
 
 (defcustom smtpmail-sendto-domain nil
@@ -666,6 +668,16 @@ smtpmail-user-mail-address
 	      (string-match "\\." (cadr parts))
 	      user-mail-address))))
 
+(defun smtpmail-derive-stream-type (stream-type port)
+  (if (equal stream-type 'auto)
+      (cdr (assoc port '((465 . ssl)
+                         ("465" . ssl)
+                         (587 . starttls)
+                         ("587" . starttls)
+                         (25 . nil)
+                         ("25" . nil))))
+    stream-type))
+
 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
 				    &optional ask-for-password
                                     send-attempts)
@@ -712,7 +724,9 @@ smtpmail-via-smtp
           ;; FIXME: Should we use raw-text-dos coding system to handle the r\n
           ;; for us?
 	  (let ((coding-system-for-read 'binary)
-		(coding-system-for-write 'binary))
+                (coding-system-for-write 'binary)
+                (smtpmail-stream-type
+                 (smtpmail-derive-stream-type smtpmail-stream-type port)))
 	    (setq result
 		  (open-network-stream
 		   "smtpmail" process-buffer host port