From cad694836cf2a0c84db80f90bab3f5f6918b4923 Mon Sep 17 00:00:00 2001 From: Rob Miller Date: Tue, 16 May 2017 19:25:52 +0100 Subject: [PATCH] Add eml2html script Converts an EML file to HTML. I'm using it to drag a load of emails out of my mail client and have them automatically converted to PDF (a second step not listed here, that I use wkhtmltopdf for). --- bin/eml2html | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 bin/eml2html diff --git a/bin/eml2html b/bin/eml2html new file mode 100755 index 0000000..faf4110 --- /dev/null +++ b/bin/eml2html @@ -0,0 +1,38 @@ +#!/usr/bin/env ruby +# +# eml2html +# +# Given an EML file, extracts the first text/html part from it and +# outputs it as an HTML file with the same name as the original EML +# file, along with some basic headers. This is to simulate printing the +# email. + +gem "mail", "~> 2.6" +require "mail" +require "pathname" + +eml = ARGV[0] + +unless eml + abort "Usage: #{$0} EML_FILE" +end + +eml = Pathname(eml) +Dir.chdir(eml.dirname) + +mail = Mail.read(eml) + +html_part = mail.parts.find { |p| p.content_type =~ /^text\/html/ } +unless html_part + abort "There doesn't seem to be a text/html part in this message." +end + +html_file = Pathname(eml.basename(".eml").to_s + ".html") +File.open(html_file, "w") do |file| + file.puts("

From: #{mail.from.first}

") + file.puts("

To: #{mail.to.first}

") + file.puts("

Subject: #{mail.subject}

") + file.puts("

Date: #{mail.date.strftime("%F %T")}

") + file.write(html_part.body) +end +