djcb / mu

maildir indexer/searcher + emacs mail client + guile bindings
http://www.djcbsoftware.nl/code/mu
GNU General Public License v3.0
1.61k stars 388 forks source link

Saving Message to PDF with Attachment Names #2117

Open Richard-CAID opened 3 years ago

Richard-CAID commented 3 years ago

I do not program, but use emacs extensively and find mu4e to be a great asset in my charity-based work with Indigenous People ( a lot of correspondence is involved). I need to get messages to "save to pdf" with attachment names to trim my work flow down (I currently print-to-pdf from Thunderbird). I have message "save to pdf" working after installing wkhtmltopdf with the following in my dotspacemacs user init:

(defun mu4e-action-save-to-pdf (msg) (let* ((date (mu4e-message-field msg :date)) (infile (mu4e~write-body-to-html msg)) (outfile (format-time-string "%Y-%m-%d%H%M%S.pdf" date))) (with-temp-buffer (shell-command (format "wkhtmltopdf %s ~/Desktop/Email/%s" infile outfile) t)))) (add-to-list 'mu4e-view-actions '("Save to PDF" . mu4e-action-save-to-pdf) t)

I was able to add Cc to the "save to pdf" by adding the following in my mu4e-actions.el under mu4e~write-body-to-html:

  (insert (concat "<strong>Cc</strong>: " (mu4e~action-header-to-html msg :cc) "</br>"))

I attempted to have attachment names saved with the message "save to pdf" by adding the following under mu4e~write-body-to-html:

  (insert (concat "<strong>Attachments</strong>: " (mu4e-message-field msg :attachment) "</p>"))

The heading "Attachments: "is within the message pdf, but not the name of the attachment(s). I have attached examples of what I have so far with saving to pdf as apposed to printing to pdf from Thunderbird. My system is:

OS Opensuse Tumbleweed Emacs (Spacemacs) is v. 27.2 Mu4e is mu for emacs version 1.4.15

How do I set for attachments names to be included in "save to pdf"?

2021_08_20_TBird_Redacted.pdf 2021_08_20_mu4e_Redacted.pdf

djcb commented 3 years ago

mu used to have a little tool msg2pdf which did just that... but it stopped working with some update to webviewgtk, which we were using for that.

For getting the attachment names, I'd recommend check the code for mu4e-view-save-attachments, which loops through the attachments.

Good luck!

Richard-CAID commented 3 years ago

I appreciate your response, but I am saddened a bit. Is there no fix planned for "save to pdf" to once again include attachments?

Richard-CAID commented 3 years ago

I did some legwork mu4e-view-save-attachments and placed code I found in #2090 into my dotspacemacs user init. I can enter an email, select "A", and save attachments to pdf single or multi and they do not overwrite files with the same name - but I do not get an option to rename a file and there is no automatic adding to a file name for it to save. I can work with these limitations, but thought, as a non-programmer, to let you know what I stumbled into.

After I placed code in from #2090, I tried a number of variations (without programming knowledge) to adjust an incorrect snippet I placed in mu4e-action.el (incorrect code was... insert (concat "Attachments: " (mu4e-message-field msg :attachment) "

")) ... Yes, I used mu4e-view-save-attachments in place of mu4e-message-field and played with variations. (bit embarassing to explain this to people who know elisp and how to program). There was no success to have attachment names saved in the email pdf.

I realized that I may have misrepresented what I was trying to do initially. With First Nation work, every email has a potential to end up being an important document in litigation at some future point. In that regard, attachments are saved to pdf and so is the email text and headers. Then these files are merged into one pdf such that the attachments and a "service" email cover are together. That email service cover saved to pdf requires date, to, from, cc, contact information, title, names of file attachments, text of the email message, and (if possible) an email identification number. essentially all that is seen when viewing in mu4e plus the addition of the email number.

My question surrounded saving an email to pdf as I am able to view it in mu4e with all visible information (plus identification number) so that I can attach that email to attachments and file them for future use. I enter an email to view it, select "a", then "S" and a copy is saved, but that pdf does not contain all information I can view in mu4e. Particularly, I need to get names of attachments on the saved pdf to confirm attachments went out with the email.

Can you help narrow down my learning curve a bit so I can get this function up and running?

Chris00 commented 2 years ago

Following this discussion, one can certainly cook up something based on the current HTML output (mu4e-action-view-in-browser) and wkhtmltopdf.

lordpretzel commented 2 years ago

@Richard-CAID https://github.com/lordpretzel/mu4e-views has support for exporting messages into different formats in the mu-1.7-support branch Have a look at the README.org for how to set this up in this branch. For pdf export you have to customize mu4e-views-html-to-pdf-command, e.g., html-pdf %h %p where %h gets replaced with the HTML file of the message and %p will be the name of the pdf file you want to export to.