Over a million developers have joined DZone.

Export POP Email To Text Files

·
require 'net/pop'
require 'tmail'

server = 'mail.yourserverhere.com'
username = 'yourusernamehere'
password = 'yourpasswordhere'

pop_server = Net::POP3.start(server, 110, username, password)
pop_server.each_mail do |raw_mail|

	email = TMail::Mail.parse(raw_mail.pop)

	file_name = email.subject.gsub(/[^0-9a-zA-Z]/, '') + '.txt'
	file = File.new(file_name, "w")

	file.puts("from : #{email.from.join(', ')}")
	file.puts("to   : #{email.to.join(', ')}")
	file.puts("from : #{email.subject}\n\n")	
	file.puts(email.body)	
	file.close
	
	# uncomment to remove from remote server
	# raw_mail.delete
end
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}