# Email embedding

**URL:** <https://www.ruby-forum.com/t/email-embedding/70789>\
**Category:** Ruby\
**Created:** [October 3, 2006, 12:00am UTC](https://www.ruby-forum.com/t/email-embedding/70789 "2006-10-03T00:00:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [October 3, 2006, 12:00am UTC](https://www.ruby-forum.com/t/email-embedding/70789/1 "2006-10-03T00:00:15Z")

</div>

Hello!

Seeing the XML-like format one of the quiz posters used for sending in  
his program, I wrote two programs that can embed and extract multiple  
files from an email. I’ll include the programs in their own format so  
you can understand 😉

Someone may have already done something like this, but if not, here you  
go.

I know of one problem - the uncruncher adds a blank line at the end of  
the files it extracts, so someone might want to fix that.

`

#/usr/bin/env ruby
email_crunch
usage: email_crunch.rb *.rb > crunchfile.txt
puts “”
ARGV.each do |f|
next if FileTest.directory?(f)
puts “<CRUNCHFILE: #{f}>”
puts File.new(f).read
puts “</CRUNCHFILE: #{f}>”
end
puts “`”

\</CRUNCHFILE: email\_crunch.rb\>  
\<CRUNCHFILE: email\_uncrunch.rb\>  
#/usr/bin/env ruby

# email\_uncrunch

# usage: cat crunch\_file | email\_uncrunch.rb

# or : email\_uncrunch.rb crunch\_file

require ‘breakpoint’

if ARGV.length != 0  
text = File.new(ARGV[0]).read  
else  
text = $stdin.read  
end

text =~ /`(.*)</code>/m
code = $1`

filename = “”  
f = nil

code.each\_line do |line|  
line.chomp!  
if line =~ /^\<CRUNCHFILE: (.+?)\>$/  
filename = $1  
puts “Creating #{filename}”  
f = File.new(filename, “w”)  
elsif !filename.empty?  
if line == “\</CRUNCHFILE: #{filename}\>”  
f.close  
filename = “”  
else  
f.puts(line)  
end  
end  
end

\</CRUNCHFILE: email\_uncrunch.rb\>

- Les
