I use Mu4e to do email and my emails are stored locally in a maildir. People often send me emails with large attachments. I'd like to keep the emails but not the attachments because I'm paying for storage and would like to keep cost low. What's the best way to remove attachments from selected emails?

Background: I'm using the development version of Emacs and Ubuntu 16.04.

  • I assume you mean on the server, not locally? It might make a difference.
    – zck
    Jun 8 '16 at 18:10
  • I mean locally in my Maildir. The change will then be propagated to the server using mbsync, but that's irrelevant to the question. I'll update the question. Thank you.
    – tmalsburg
    Jun 8 '16 at 18:12

I found the solution hidden in a closed issue on GitHub (thanks to @mankoff):

(defun my-remove-attachment (msg num) 
  "Remove attachment." 
  (let* ((attach (mu4e~view-get-attach msg num))
         (path (mu4e-msg-field msg :path))
         (filename (and attach (plist-get attach :name)))
         (cmd (format "altermime --input='%s' --remove='%s'"  path filename)))
    (when (and filename
                (format "Are you sure you want to remove '%s'?" filename)))
      (shell-command cmd)
      (message cmd))))

(add-to-list 'mu4e-view-attachment-actions
             '("remove-attachment" . my-remove-attachment))

This adds a new "attachment action" in Mu4e. When viewing an email, press A, then r to select the action defined above, then enter the number of the attachment. The action will ask you for confirmation before deleting the attachment. The header listing the attachments is not updates but when you leave the email and reopen it, the attachment is gone.

The code assumes that the tool altermime is installed. To install it in Ubuntu, do sudo apt-get install altermime.

  • Thank you very much! I had to put single quotes in --input='%s' as well, since some (GMail) folders have spaces. I found that altermime also has the --removeall option, which can be useful as well.
    – AstroFloyd
    Sep 20 '19 at 16:49
  • @AstroFloyd, thank you. I updated the solution.
    – tmalsburg
    Sep 23 '19 at 14:36
  • For macOS see this thread (requires MacPorts and sudo ports install altermime). Although the Emacs fails to complete the shell command, copy-pasting the command in the *Messages* window worked well. This may happen because Emacs does not have the same initialization as the shell: get the full path to altermime from which altermime and replace format "altermime with format "/opt/local/bin/altermime for example. Sep 14 '20 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.