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)))
         (mech (or (plist-get auth-info :smtp-auth) (car mechs)))
         (user (plist-get auth-info :user))
         (password (plist-get auth-info :secret))
	 (save-function (and ask-for-password
			     (plist-get auth-info :save-function))))
    (when (functionp password)
      (setq password (funcall password)))
    (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 (plist-get auth-info :secret)))
    (when (functionp password)
      (setq password (funcall password)))
    (let ((result (catch 'done
                    (if (and mech user password)
                        (smtpmail-try-auth-method process (intern-soft mech) user password)
                      ;; No mechanism, or no credentials.
                      mech))))
      (if (stringp result)
	  (progn
	    (auth-source-forget+ :host host :port port)
	    (throw 'done result))
	(when save-function
	  (funcall save-function))
	result))))