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 +