attach from dired

16 views
Skip to first unread message

jouke hijlkema

unread,
Aug 4, 2022, 8:31:38 AM8/4/22
to mu-di...@googlegroups.com
For those interested, this works really nicely for me. 

Jouke

  ;;|--------------------------------------------------------------
  ;;|Description : attach all marked files to a mu4e email
  ;;|NOTE : stolen from mu4e-contrib.el and modified to work with dired
  ;;|-
  ;;|Author : jouke hylkema
  ;;|date   : 04-26-2022 14:26:08
  ;;|--------------------------------------------------------------
  (defun myDiredAttach ()
    "attach all marked files to a mu4e email"
    (interactive)
    (let ((destination nil)
          (files-str nil)
          (bufs nil)
          ;; Remove directories from the list
          (files-to-attach (dired-get-marked-files)))
      (if (null files-to-attach)
          (error "No files to attach")
        (setq files-str
              (mapconcat
               (lambda (f) (file-name-nondirectory f))
               files-to-attach ", "))
        (setq bufs (mu4e--active-composition-buffers))
        ;; set up destination mail composition buffer
        (if (and bufs
                 (y-or-n-p "Attach files to existing mail composition buffer? "))
            (setq destination
                  (if (= (length bufs) 1)
                      (get-buffer (car bufs))
                    (let ((prompt (mu4e-format "%s" "Attach to buffer")))
                      (substring-no-properties
                       (funcall mu4e-completing-read-function prompt
                                bufs)))))
          ;; setup a new mail composition buffer
          (if (y-or-n-p "Compose new mail and attach this file? ")
              (progn (mu4e-compose-new)
                     (setq destination (current-buffer)))))
        ;; if buffer was found, set buffer to destination buffer, and attach files
        (if (not (eq destination 'nil))
            (progn (set-buffer destination)
                   (goto-char (point-max)) ; attach at end of buffer
                   (while files-to-attach
                     (mml-attach-file (car files-to-attach)
                                      (or (mm-default-file-encoding
  (car files-to-attach))
                                          "application/octet-stream") nil)
                     (setq files-to-attach (cdr files-to-attach)))
                   (message "Attached file(s) %s" files-str))
          (message "No buffer to attach file to.")))
      )
    )

Vladimir Sedach

unread,
Aug 4, 2022, 4:41:34 PM8/4/22
to mu-di...@googlegroups.com, jouke hijlkema
That is very useful. I did not know about eshell/mu4e-attach in
mu4e-contrib.el so below is what I wrote for myself a couple of years
ago:

--8<---------------cut here---------------start------------->8---
(define-key dired-mode-map (kbd "C-c C-a")
(lambda ()
(interactive)
(require 'message)
(let* ((mail-buffers (message-buffers))
(mail-point nil)
(mail-buffer
(cond
((null mail-buffers) (save-excursion
(mu4e-compose-new)
(setq mail-point (point))
(current-buffer)))
((= (length mail-buffers) 1) (car mail-buffers))
(t (read-buffer "Choose a mail buffer: "
(car mail-buffers)
t
(lambda (x)
(member (car x) mail-buffers)))))))
(dired-map-over-marks
(let ((file (dired-get-filename)))
(unless (file-directory-p file)
(with-current-buffer mail-buffer
(goto-char (point-max))
(newline)
(mml-attach-file file)
(when mail-point (goto-char mail-point)))))
nil))))
--8<---------------cut here---------------end--------------->8---

I think people would find it useful to have a mu4e configuration
function to add a dired attachment command similar to what jouke
hylkema or I wrote for ourselves.

--
Vladimir Sedach

Stephen Eglen

unread,
Aug 8, 2022, 4:14:25 PM8/8/22
to mu-discuss
In case others have not seen this, this functionality is in the help pages:


Stephen

Reply all
Reply to author
Forward
0 new messages