Function: smtpmail-try-auth-methods

smtpmail-try-auth-methods is a byte-compiled function defined in smtpmail.el.gz.

Signature

(smtpmail-try-auth-methods PROCESS SUPPORTED-EXTENSIONS HOST PORT &optional ASK-FOR-PASSWORD)

Source Code

;; Defined in /usr/src/emacs/lisp/mail/smtpmail.el.gz
(defun smtpmail-try-auth-methods (process supported-extensions host port
					  &optional ask-for-password)
  (setq port
	(if port
	    (format "%s" port)
	  "smtp"))
  (let* ((mechs (seq-intersection
                 smtpmail-auth-supported
                 (cdr-safe (assoc 'auth supported-extensions))
                 #'eq))
	 (auth-source-creation-prompts
          '((user  . "SMTP user name for %h: ")
            (secret . "SMTP password for %u@%h: ")))
         (auth-info (car
		     (auth-source-search
		      :host host
		      :port port
		      :user smtpmail-smtp-user
		      :max 1
		      :require (and ask-for-password
				    '(:user :secret))
		      :create ask-for-password)))
         (user (plist-get auth-info :user))
         (password (auth-info-password auth-info))
	 (save-function (and ask-for-password
			     (plist-get auth-info :save-function))))
    (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
	      :user smtpmail-smtp-user
	      :require '(:user :secret)
	      :create t))
	    password (auth-info-password auth-info)))
    (let ((mechs (or (ensure-list (plist-get auth-info :smtp-auth))
                     mechs))
          (result ""))
      (when (and mechs user password)
        (while (and mechs
                    (stringp result))
          (setq result (catch 'done
                         (smtpmail-try-auth-method
                          process (intern-soft (pop mechs)) user password))))
        ;; A string result is an error.
        (if (stringp result)
            (progn
              ;; All methods failed.
              ;; Forget the credentials.
	      (auth-source-forget+ :host host :port port)
              (throw 'done result))
          ;; Success.
	  (when save-function
	    (funcall save-function))
          result)))))