]> code.delx.au - gnu-emacs/blobdiff - lisp/mail/smtpmail.el
Use `customize-save-variable' unconditionally, now that it works under emacs -Q
[gnu-emacs] / lisp / mail / smtpmail.el
index 58a517ba83651dfcc56100c405f3f3e24ec9e424..073e2fa4a3cdae21e69ac3415f4e2f12577707c1 100644 (file)
   :group 'mail)
 
 
-(defvar smtpmail-default-smtp-server nil
+(defcustom smtpmail-default-smtp-server nil
   "Specify default SMTP server.
-This only has effect if you specify it before loading the smtpmail library.")
+This only has effect if you specify it before loading the smtpmail library."
+  :type '(choice (const nil) string)
+  :group 'smtpmail)
 
 (defcustom smtpmail-smtp-server
   (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
@@ -96,13 +98,14 @@ don't define this value."
 
 (defcustom smtpmail-stream-type nil
   "Connection type SMTP connections.
-This may be either nil (plain connection) or `starttls' (use the
-starttls mechanism to turn on TLS security after opening the
-stream)."
+This may be either nil (possibly upgraded to STARTTLS if
+possible), or `starttls' (refuse to send if STARTTLS isn't
+available), or `plain' (never use STARTTLS).."
   :version "24.1"
   :group 'smtpmail
-  :type '(choice (const :tag "Plain" nil)
-                (const starttls)))
+  :type '(choice (const :tag "Possibly upgrade to STARTTLS" nil)
+                (const :tag "Always use STARTTLS" starttls)
+                (const :tag "Never use STARTTLS" plain)))
 
 (defcustom smtpmail-sendto-domain nil
   "Local domain name without a host name.
@@ -477,23 +480,42 @@ The list is in preference order.")
 
 (defun smtpmail-try-auth-methods (process supported-extensions host port
                                          &optional ask-for-password)
+  (setq port
+       (if port
+           (format "%s" port)
+         "smtp"))
   (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
         (mech (car (smtpmail-intersection mechs smtpmail-auth-supported)))
         (auth-source-creation-prompts
           '((user  . "SMTP user at %h: ")
             (secret . "SMTP password for %u@%h: ")))
          (auth-info (car
-                    (auth-source-search :max 1
-                                        :host host
-                                        :port (if port
-                                                  (format "%s" port)
-                                                "smtp")
-                                        :create ask-for-password)))
+                    (auth-source-search
+                     :host host
+                     :port port
+                     :max 1
+                     :require (and ask-for-password
+                                   '(:user :secret))
+                     :create ask-for-password)))
          (user (plist-get auth-info :user))
          (password (plist-get auth-info :secret))
         (save-function (and ask-for-password
                             (plist-get auth-info :save-function)))
         ret)
+    (when (and user
+              (not password))
+      ;; The user has stored the user name, but not the password, so
+      ;; ask for the password, even if we're not forcing that through
+      ;; `ask-for-password'.
+      (setq auth-info
+           (car
+            (auth-source-search
+             :max 1
+             :host host
+             :port port
+             :require '(:user :secret)
+             :create t))
+           password (plist-get auth-info :secret)))
     (when (functionp password)
       (setq password (funcall password)))
     (cond
@@ -596,6 +618,8 @@ The list is in preference order.")
                            (and mail-specify-envelope-from
                                 (mail-envelope-from))
                            user-mail-address))
+       (coding-system-for-read 'binary)
+       (coding-system-for-write 'binary)
        response-code
        process-buffer
        result
@@ -632,7 +656,9 @@ The list is in preference order.")
 
          ;; If we couldn't access the server at all, we give up.
          (unless (setq process (car result))
-           (throw 'done "Unable to contact server"))
+           (throw 'done (if (plist-get (cdr result) :error)
+                            (plist-get (cdr result) :error)
+                          "Unable to contact server")))
 
          ;; set the send-filter
          (set-process-filter process 'smtpmail-process-filter)