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

From: Ryan Tate <[email-masked]>
Date: Mon, 04 Nov 2019 23:45:52 -0500

The internal emacs mail header X-Message-SMTP-Method is described in
section 3.3 of the manual as a way to get "some messages to go via one
mail server, and other messages to go through another." Both examples
given show that this header can be used to communicate a method, server
name, and port.
https://www.gnu.org/software/emacs/manual/html_node/message/Mail-Variables.html

However, emacs, in particular gnus/message.el when sending through
smtpmail.el, fails to adjust the variable smtpmail-smtp-stream type to
match the port. So, for example, if a user selects port 465, the
connection will fail as this is the port for SMTP connections with
TLS.

Port 587 will happen to work as this is the port for SMTP with STARTTLS,
and STARTTLS upgrades can happen in a plain stream type (the smtpmail
default). But emacs will not (and does not provide an option to) force
STARTTLS when port 587 is specified as would occur if 'ssl were supplied
as the value for smtpmail-stream-type. This provides an avenue under
which encryption may be silently dropped. 

There is no warning in the manual that port 465 will not function as
expected using the X-Message-SMTP-Method header.

There is also no mechanism through which a stream may be user specified
via the X-Message-SMTP-Method header.

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

Personally I am working around this with the following hook. (Due to
changes to emacs in subsequent versions, it needs to work with both
strings and numbers as the port):

(defun set-smtpmail-stream-type-from-port ()
  (when (or (equal smtpmail-smtp-service "465")
	    (equal smtpmail-smtp-service 465))
    (setq smtpmail-stream-type 'ssl))
  (when (or (equal smtpmail-smtp-service "587")
	    (equal smtpmail-smtp-service 587))
    (setq smtpmail-stream-type 'starttls))
  (when (or (equal smtpmail-smtp-service "25")
	    (equal smtpmail-smtp-service 25))
    (setq smtpmail-stream-type nil))
  )

(add-hook 'message-send-mail-hook #'set-smtpmail-stream-type-from-port)

In GNU Emacs 26.1 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.4)
 of 2019-02-03, modified by Debian built on zam904
Windowing system distributor 'The X.Org Foundation', version 11.0.12004000
System Description:	Debian GNU/Linux 10 (buster)

Recent messages:
Loading /etc/emacs/site-start.d/50autoconf.el (source)...done
Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...
Loading debian-ispell...
Loading /var/cache/dictionaries-common/emacsen-ispell-default.el (source)...done
Loading debian-ispell...done
Loading /var/cache/dictionaries-common/emacsen-ispell-dicts.el (source)...done
Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...done
For information about GNU Emacs and the GNU system, type C-h C-a.
Mark set [4 times]
Auto-saving...done

Configured using:
 'configure --build x86_64-linux-gnu --prefix=/usr
 --sharedstatedir=/var/lib --libexecdir=/usr/lib
 --localstatedir=/var/lib --infodir=/usr/share/info
 --mandir=/usr/share/man --enable-libsystemd --with-pop=yes
 --enable-locallisppath=/etc/emacs:/usr/local/share/emacs/26.1/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/26.1/site-lisp:/usr/share/emacs/site-lisp
 --with-sound=alsa --without-gconf --with-mailutils --build
 x86_64-linux-gnu --prefix=/usr --sharedstatedir=/var/lib
 --libexecdir=/usr/lib --localstatedir=/var/lib
 --infodir=/usr/share/info --mandir=/usr/share/man --enable-libsystemd
 --with-pop=yes
 --enable-locallisppath=/etc/emacs:/usr/local/share/emacs/26.1/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/26.1/site-lisp:/usr/share/emacs/site-lisp
 --with-sound=alsa --without-gconf --with-mailutils --with-x=yes
 --with-x-toolkit=gtk3 --with-toolkit-scroll-bars 'CFLAGS=-g -O2
 -fdebug-prefix-map=/build/emacs-26.1+1=. -fstack-protector-strong
 -Wformat -Werror=format-security -Wall' 'CPPFLAGS=-Wdate-time
 -D_FORTIFY_SOURCE=2' LDFLAGS=-Wl,-z,relro'

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND GPM DBUS GSETTINGS NOTIFY
ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE M17N_FLT LIBOTF XFT ZLIB
TOOLKIT_SCROLL_BARS GTK3 X11 THREADS LIBSYSTEMD LCMS2

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Message[Notmuch]

Minor modes in effect:
  diff-auto-refine-mode: t
  mml-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  auto-fill-function: message-do-auto-fill
  transient-mark-mode: t
  abbrev-mode: t

Load-path shadows:
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-show hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-show
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-jump hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-jump
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-crypto hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-crypto
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/coolj hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/coolj
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-draft hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-draft
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-compat hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-compat
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-autoloads hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-autoloads
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-company hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-company
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-query hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-query
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-hello hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-hello
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-message hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-message
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/make-deps hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/make-deps
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-address hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-address
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-parser hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-parser
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-print hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-print
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-tag hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-tag
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-lib hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-lib
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-wash hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-wash
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-maildir-fcc hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-maildir-fcc
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-pkg hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-pkg
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-tree hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-tree
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-version hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-version
/usr/share/emacs/site-lisp/elpa/notmuch-0.28.4/notmuch-mua hides /usr/share/emacs/site-lisp/elpa-src/notmuch-0.28.4/notmuch-mua

Features:
(shadow emacsbug sendmail sort gnus-alias mail-extr notmuch hl-line
notmuch-message notmuch-hello wid-edit notmuch-tree notmuch-show
notmuch-print notmuch-crypto notmuch-mua notmuch-draft
notmuch-maildir-fcc notmuch-address notmuch-company notmuch-parser
notmuch-wash diff-mode easy-mmode coolj notmuch-query goto-addr
thingatpt icalendar diary-lib diary-loaddefs cal-menu calendar
cal-loaddefs notmuch-tag edmacro kmacro crm notmuch-lib advice
notmuch-version notmuch-compat cl message rmc puny dired dired-loaddefs
format-spec rfc822 mml mailabbrev gmm-utils mailheader mm-view mml-smime
mml-sec epa derived epg gnus-util rmail rmail-loaddefs mail-utils smime
dig mm-decode mm-bodies mm-encode mailcap mail-parse rfc2231 rfc2047
rfc2045 mm-util ietf-drums mail-prsvr elec-pair finder-inf info package
easymenu epg-config url-handlers url-parse auth-source cl-seq eieio
eieio-core cl-macs eieio-loaddefs password-cache url-vars seq byte-opt
gv bytecomp byte-compile cconv cl-loaddefs cl-lib time-date mule-util
tooltip eldoc electric uniquify ediff-hook vc-hooks lisp-float-type
mwheel term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode elisp-mode
lisp-mode prog-mode register page menu-bar rfn-eshadow isearch timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame cl-generic cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms cp51932
hebrew greek romanian slovak czech european ethiopic indian cyrillic
chinese composite charscript charprop case-table epa-hook jka-cmpr-hook
help simple abbrev obarray minibuffer cl-preloaded nadvice loaddefs
button faces cus-face macroexp files text-properties overlay sha1 md5
base64 format env code-pages mule custom widget hashtable-print-readable
backquote dbusbind inotify lcms2 dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty
make-network-process emacs)

Memory information:
((conses 16 260049 19820)
 (symbols 48 29943 6)
 (miscs 40 184 580)
 (strings 32 83885 3752)
 (string-bytes 1 2093686)
 (vectors 16 25869)
 (vector-slots 8 656309 21192)
 (floats 8 58 202)
 (intervals 56 493 19)
 (buffers 992 14))