Email attachment reminders in mu4e

Continuing my series of posts on using mu4e for emails in emacs, I wanted to duplicate the functionality of other email clients that warn you if you appear to have forgotten an attachment when you send an email.

My solution is decidedly lo-fi, but it does the trick. It is based on this post (coincidentally by the author of mu4e), which shows how to create highlighting for arbitrary keywords in emacs. This is a versatile trick and with the simple code below, any strings in a mu4e composition buffer that match “attach”, “pdf” or “file” get a nice red on yellow highlight.

;; attachment reminder based on
;; http://emacs-fu.blogspot.co.uk/2008/12/highlighting-todo-fixme-and-friends.html
(set-face-attribute 'font-lock-warning-face nil :foreground "red" :weight 'bold :background "yellow")
(add-hook 'mu4e-compose-mode-hook
          (defun bjm/mu4e-highlight-attachment ()
            "Flag attachment keywords"
            (font-lock-add-keywords nil
                                    '(("\\(attach\\|pdf\\|file\\)" 1 font-lock-warning-face t)))))

It’s not pretty but it works for me! I’d prefer something that checked my email to see if it actually had an attachment and then warned me when I tried to send if there was no attachement there. I’ve not had time to work on that though, but if you know of anything similar, let me know!

Update

There were some helpful suggestions in the comments for improved methods that prompt the user if they try to send a mail without an attachment. mbork has a nice clear example on their website.

  • egh

    I don’t use mu4e, but I have something similar for wanderlust that you might be able to adapt:

    (defun egh:wl-draft-check-attachment ()
    (save-excursion
    (mail-text)
    (if (re-search-forward "attach" nil t)
    (progn
    (mail-text)
    (unless (re-search-forward mime-edit-beginning-tag-regexp nil t)
    (unless (yes-or-no-p "You used the word `attach' in this mail, but did not include an attachment. Are you sure you want to send? ")
    (error "No attachment!")))))))

    (add-hook 'wl-draft-send-hook 'egh:wl-draft-check-attachment)

  • JohnKitchin

    Here is my similar code for mu4e (sorry for the ugly formatting):

    (defun email-says-attach-p ()
    “Return t if email suggests there could be an attachment.”
    (save-excursion
    (goto-char (point-min))
    (re-search-forward “attach” nil t)))

    (defun email-has-attachment-p ()
    “Return t if the currently open email has an attachment”
    (save-excursion
    (goto-char (point-min))
    (re-search-forward “<#part" nil t)))

    (defun email-pre-send-check-attachment ()

    (when (and (email-says-attach-p)

    (not (email-has-attachment-p)))

    (unless

    (y-or-n-p "Your email suggests you need an attachment, but no attachment was found. Send anyway?")

    (error "It seems an attachment is needed, but none was found. Aborting send."))))

    (add-hook 'message-send-hook 'email-pre-send-check-attachment)