Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Last active May 10, 2016 13:03
Show Gist options
  • Save fernandojunior/cf09a0c133f181452a2982a54a7c3b49 to your computer and use it in GitHub Desktop.
Save fernandojunior/cf09a0c133f181452a2982a54a7c3b49 to your computer and use it in GitHub Desktop.
Backup all emails
import re
import shutil
import requests
USERNAME = '***'
PASSWORD = '***'
MAILBOX_ID = '***'
OUTPUT_FOLDER = 'inbox'
MESSAGE_START = 1
MESSAGE_END = 149
LOGIN_URL = 'https://***/egroupware/login.php'
INDEX_URL = 'https://***/egroupware/index.php'
def login(session, username, password):
return session.post(LOGIN_URL, verify=False, data={
'passwd_type': 'text',
'account_type': 'u',
'logindomain': 'default',
'login': username,
'passwd': password,
'submitit': 'Conectar'
})
def get_message(session, id):
return session.get(INDEX_URL, stream=True, verify=False, params={
'menuaction': 'felamimail.uidisplay.saveMessage',
'uid': id,
'mailbox': MAILBOX_ID
})
def save_message(filename, data):
with open(filename, 'wb') as f:
shutil.copyfileobj(data, f)
with requests.Session() as session:
login(session, USERNAME, PASSWORD)
for message_id in reversed(range(MESSAGE_START, MESSAGE_END)):
response = get_message(session, message_id)
subject = response.headers['Content-Disposition']
subject = re.findall(r'filename="(.*?)"', subject)[0]
subject = re.sub('[\\/:*?"<>|]', '', subject)
filename = '%s/[%s] %s' % (OUTPUT_FOLDER, message_id, subject)
save_message(filename, response.raw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment