DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Export POP Email To Text Files

03.15.2010
| 3843 views |
  • submit to reddit
        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