Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to backup emails into MySQL database? Say, I have an account and I want to keep record of all the emails inside the locally, in my computer.

From what I've search on the internet, it's said that mail server store emails as text file. So, if I can get my hands on this file, transfer it into my local computer, then I can process it and store it into the database, right?

So, my question is: how to transfer the file from mail server to my local computer? how to even identify the file?

Any example would be much appreciated.

Thank you in advance.

share|improve this question
Why it should be a MySQL database? Most email client are doing this anyway (pulling your mail to your computer) using POP/IMAP/EAS/whatever. Thunderbird for example store your mails inside an internal SQLite database. So if your goal is simply to keep your mail for offline inside, Thunderbird will do the job. If you need it for other processing, you might need to find a plugin for Thunderbird or whip out your own app, which is StackOverflow domain. – Martheen Cahya Paulo Feb 27 '13 at 8:19

Most mail servers store emails either in "Mail Folders" or "Mail Dirs" "Mail Folders" means that each folder/category has all the emails stored in it, one after the other. "Mail Dir" means each folder is actually a directory with each mail message represented as a single file in that directory.

You are correct in saying if you get this mail file (which actually means Mail Folders or Mail Dirs, you can write a script to import it into your database.

As far as identifying the mail dirs/folders this is not something for you to do, its something for the "postmaster" at your domain to advise you, as they could be anywhere. Common places (on Unix Systems) include /var/spool/mail and ~mail - but this is arbitrary.

An alternative to getting the mail folder/dir would be to instruct the server to provide it to you. Depending on your scenario, if you have all the emails sitting in your inbox and your provider uses the "POP3" protocol, you should be able to script a mechanism to pull each mail down and process it. POP3 is quite a simple protocol to speak. You can do something similar with IMAP - which will give you access to all the files - but which is much harder to use and will posibly require a framework/library to help.

Mail Messages themselves are quite simple -

They consist of a header, separated by 2 lines and the body. You can parse the header to get the from, to, subject, date of the message etc. The body can be text or can be broken up into multiple sections, each typically Base64 encoded (but there are alternatives to this) - for example if you get an email with both HTML and text, or emails or attachments in the email.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.