Function: message-send-mail

message-send-mail is a byte-compiled function defined in message.el.gz.

Signature

(message-send-mail &optional _)

Source Code

;; Defined in /usr/src/emacs/lisp/gnus/message.el.gz
(defun message-send-mail (&optional _)
  (require 'mail-utils)
  (let* ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
	 (case-fold-search nil)
	 (news (message-news-p))
	 (mailbuf (current-buffer))
	 (message-this-is-mail t)
	 ;; gnus-setup-posting-charset is autoloaded in mml.el (FIXME
	 ;; maybe it should not be), which this file requires.  Hence
	 ;; the fboundp test is always true.  Loading it from gnus-msg
	 ;; loads many Gnus files (Bug#5642).  If
	 ;; gnus-group-posting-charset-alist hasn't been customized,
	 ;; this is just going to return nil anyway.  FIXME it would
	 ;; be good to improve this further, because even if g-g-p-c-a
	 ;; has been customized, that is likely to just be for news.
	 ;; Eg either move the definition from gnus-msg, or separate out
	 ;; the mail and news parts.
	 (message-posting-charset
	  (if (and (fboundp 'gnus-setup-posting-charset)
		   (boundp 'gnus-group-posting-charset-alist))
	      (gnus-setup-posting-charset nil)
	    message-posting-charset))
	 (headers message-required-mail-headers)
	 options)
    (save-restriction
      (message-narrow-to-headers)
      ;; Generate the Mail-Followup-To header if the header is not there...
      (if (and (message-subscribed-p)
	       (not (mail-fetch-field "mail-followup-to")))
	  (setq headers
		(cons
		 (cons "Mail-Followup-To" (message-make-mail-followup-to))
		 message-required-mail-headers))
	;; otherwise, delete the MFT header if the field is empty
	(when (equal "" (mail-fetch-field "mail-followup-to"))
	  (message-remove-header "Mail-Followup-To")))
      ;; Insert some headers.
      (let ((message-deletable-headers
	     (if news nil message-deletable-headers)))
	(message-generate-headers headers))
      ;; Check continuation headers.
      (message--check-continuation-headers)
      (message--fold-long-headers)
      ;; Let the user do all of the above.
      (run-hooks 'message-header-hook))
    (setq options message-options)
    (unwind-protect
	(with-current-buffer tembuf
	  (erase-buffer)
	  (setq message-options options)
	  ;; Avoid copying text props (except hard newlines).
	  (insert (with-current-buffer mailbuf
		    (mml-buffer-substring-no-properties-except-some
		     (point-min) (point-max))))
	  (message-encode-message-body)
	  (message--cache-encoded mailbuf)
	  (save-restriction
	    (message-narrow-to-headers)
	    ;; We (re)generate the Lines header.
	    (when (memq 'Lines message-required-mail-headers)
	      (message-generate-headers '(Lines)))
	    ;; Remove some headers.
	    (message-remove-header message-ignored-mail-headers t)
            (mail-encode-encoded-word-buffer)
	    ;; Then check for suspicious addresses.
            (dolist (hdr '("To" "Cc" "Bcc"))
              (let ((addr (message-fetch-field hdr)))
                (when (stringp addr)
                  (dolist (address (mail-header-parse-addresses addr t))
                    (when-let* ((warning (textsec-suspicious-p
                                          address 'email-address-header)))
                      (unless (y-or-n-p
                               (format "Suspicious address: %s; send anyway?"
                                       warning))
                        (user-error "Suspicious address %s" address))))))))
	  (goto-char (point-max))
	  ;; require one newline at the end.
	  (or (= (preceding-char) ?\n)
	      (insert ?\n))
	  (message-cleanup-headers)
	  ;; FIXME: we're inserting the courtesy copy after encoding.
	  ;; This is wrong if the courtesy copy string contains
	  ;; non-ASCII characters. -- jh
	  (when
	      (save-restriction
		(message-narrow-to-headers)
		(and news
		     (not (message-fetch-field "List-Post"))
		     (not (message-fetch-field "List-ID"))
		     (or (message-fetch-field "cc")
			 (message-fetch-field "bcc")
			 (message-fetch-field "to"))
		     (let ((content-type (message-fetch-field
					  "content-type")))
		       (and
			(or
			 (not content-type)
			 (string= "text/plain"
				  (car
				   (mail-header-parse-content-type
				    content-type))))
			(not
			 (string= "base64"
				  (message-fetch-field
				   "content-transfer-encoding")))))))
	    (message-insert-courtesy-copy
	     (with-current-buffer mailbuf
	       message-courtesy-message)))
          ;; If this was set, `sendmail-program' takes care of encoding.
          (unless message-inhibit-body-encoding
            ;; Let's make sure we encoded everything in the buffer.
            (cl-assert (save-excursion
                         (goto-char (point-min))
                         (not (re-search-forward "[^\000-\377]" nil t)))))
          (mm-disable-multibyte)
          (message--send-mail-maybe-partially)
	  (setq options message-options))
      (kill-buffer tembuf))
    (set-buffer mailbuf)
    (setq message-options options)
    (push 'mail message-sent-message-via)))