上次说到可以用markdown配合gnus来发HTML格式的邮件,可是当需要添加附件的时候,上述方法就不起作用了。附件部分的信息被当成邮件的正文转为HTML格式。解决方法是当没有附件时自动将邮件正文转为HTML格式,否则手工转换。实现方式如下:
(setq markdown-command "markdown") (defun mimedown-region (beg end) (interactive "r") (save-excursion (let ((orig-txt (buffer-substring-no-properties beg end))) (shell-command-on-region beg end markdown-command nil t) (insert "<#multipart type=alternative>n") (insert orig-txt) (insert "<#part type=text/html>n<html>n<head>n<title> HTML version of email</title>n</head>n<body>") (exchange-point-and-mark) (insert "n</body>n</html>n<#/multipart>n")))) (defun mimedown-message-body () (interactive) (save-excursion (message-goto-body) (mimedown-region (point) (point-max)))) (add-hook 'message-mode-hook (lambda () (add-hook 'message-send-hook 'mimedown-message-body))) (defun remove-message-send-hook () (interactive) (remove-hook 'message-send-hook 'mimedown-message-body)) (defadvice mml-attach-file (after remove-mimedown-message-body (file &optional type description disposition)) (remove-message-send-hook)) (ad-activate 'mml-attach-file)
手工转换使用命令`markdown-region'
,而message-mode-hook
和mml-attach-file
(添加附件的命令)的after advice分别负责HTML自动转换的打开和关闭。使用advice是因为mml-attach-file
没有hook可以使用。