From 5ce90668242243c6554be2fe650182fbed2ff670 Mon Sep 17 00:00:00 2001 From: Phuoc CAO Date: Sun, 28 May 2023 20:16:28 +0200 Subject: [PATCH] added scripts for testing smtp and imap --- cao_blogr/static/theme.css | 3 ++- email_avnes_purge.py | 50 ++++++++++++++++++++++++++++++++++++ gmail_caotek_purge.py | 50 ++++++++++++++++++++++++++++++++++++ gmail_ctphuoc_purge.py | 51 +++++++++++++++++++++++++++++++++++++ imap-APP-office365-dumas.py | 45 ++++++++++++++++++++++++++++++++ imap-SSL-alinto-dumas.py | 20 +++++++++++++++ smtp-SSL-alinto-dumas.py | 33 ++++++++++++++++++++++++ smtp-TLS-office365-dumas.py | 35 +++++++++++++++++++++++++ 8 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 email_avnes_purge.py create mode 100644 gmail_caotek_purge.py create mode 100644 gmail_ctphuoc_purge.py create mode 100644 imap-APP-office365-dumas.py create mode 100644 imap-SSL-alinto-dumas.py create mode 100644 smtp-SSL-alinto-dumas.py create mode 100644 smtp-TLS-office365-dumas.py diff --git a/cao_blogr/static/theme.css b/cao_blogr/static/theme.css index 402c78e..4d17881 100644 --- a/cao_blogr/static/theme.css +++ b/cao_blogr/static/theme.css @@ -156,4 +156,5 @@ textarea { /* On hover, the dropdown links will turn red */ .dropdown-menu li a:hover { background-color: red !important; -} \ No newline at end of file +} + diff --git a/email_avnes_purge.py b/email_avnes_purge.py new file mode 100644 index 0000000..2e63666 --- /dev/null +++ b/email_avnes_purge.py @@ -0,0 +1,50 @@ +# -*- coding: utf8 -*- +# +# Compter les emails BEFORE DATE +# + +from pprint import pprint +import datetime +import imaplib + +# connecter au serveur IMAP +conn = imaplib.IMAP4_SSL('mail.gandi.net') +conn.login('bureau@avnes.org', 'ConCho#56') +# lister les dossiers +typ, data = conn.list() +print('Liste des dossiers :') +pprint(data) + +# delete mails before 14 years +before_date = (datetime.date.today() - datetime.timedelta(365.25 * 13)).strftime("%d-%b-%Y") +print("Delete emails before " + before_date) + +# select ALL +conn.select('INBOX') + +rv, data = conn.search(None, '(BEFORE {0})'.format(before_date)) +nb_mails = str(len(data[0])) +print(nb_mails + " emails founded") + +resp = input ("Enter 'c' to continue, or 'a' to abort : ") +if resp=="c": + print("Moving " + nb_mails + " emails to Trash") + messages = data[0].split(b' ') + for mail in messages: + # move to trash + conn.store(mail, '+X-GM-LABELS', '\\Trash') + + #This block empties trash, remove if you want to keep, Gmail auto purges trash after 30 days. + print("Emptying Trash & Expunge...") + conn.select('[Gmail]/Corbeille') + conn.store("1:*", '+FLAGS', '\\Deleted') + # delete all the selected messages + conn.expunge() + print("Script completed") +else: + print("Script aborted") + +# deconnexion du serveur +conn.close() +conn.logout() + diff --git a/gmail_caotek_purge.py b/gmail_caotek_purge.py new file mode 100644 index 0000000..5dc4ef8 --- /dev/null +++ b/gmail_caotek_purge.py @@ -0,0 +1,50 @@ +# -*- coding: utf8 -*- +# +# Compter les emails BEFORE DATE +# + +from pprint import pprint +import datetime +import imaplib + +# connecter au serveur IMAP +conn = imaplib.IMAP4_SSL('imap.gmail.com') +conn.login('phuoc@caotek.fr', 'pcao.8211') +# lister les dossiers +typ, data = conn.list() +print('Liste des dossiers :') +pprint(data) + +# delete mails before 14 years +before_date = (datetime.date.today() - datetime.timedelta(365.25 * 13)).strftime("%d-%b-%Y") +print("Delete emails before " + before_date) + +# select ALL +conn.select('INBOX') + +rv, data = conn.search(None, '(BEFORE {0})'.format(before_date)) +nb_mails = str(len(data[0])) +print(nb_mails + " emails founded") + +resp = input ("Enter 'c' to continue, or 'a' to abort : ") +if resp=="c": + print("Moving " + nb_mails + " emails to Trash") + messages = data[0].split(b' ') + for mail in messages: + # move to trash + conn.store(mail, '+X-GM-LABELS', '\\Trash') + + #This block empties trash, remove if you want to keep, Gmail auto purges trash after 30 days. + print("Emptying Trash & Expunge...") + conn.select('[Gmail]/Corbeille') + conn.store("1:*", '+FLAGS', '\\Deleted') + # delete all the selected messages + conn.expunge() + print("Script completed") +else: + print("Script aborted") + +# deconnexion du serveur +conn.close() +conn.logout() + diff --git a/gmail_ctphuoc_purge.py b/gmail_ctphuoc_purge.py new file mode 100644 index 0000000..3ac980f --- /dev/null +++ b/gmail_ctphuoc_purge.py @@ -0,0 +1,51 @@ +# -*- coding: utf8 -*- +# +# Compter les emails BEFORE DATE +# + +from pprint import pprint +import datetime +import imaplib + +# connecter au serveur IMAP +conn = imaplib.IMAP4_SSL('imap.gmail.com') +conn.login('ctphuoc@gmail.com', 'ztwciswzhxxogcfv') + +# lister les dossiers +typ, data = conn.list() +print('Liste des dossiers :') +pprint(data) + +# delete mails before 14 years +before_date = (datetime.date.today() - datetime.timedelta(365.25 * 13)).strftime("%d-%b-%Y") +print("Delete emails before " + before_date) + +# select ALL +conn.select('INBOX') + +rv, data = conn.search(None, '(BEFORE {0})'.format(before_date)) +nb_mails = str(len(data[0])) +print(nb_mails + " emails founded") + +resp = input ("Enter 'c' to continue, or 'a' to abort : ") +if resp=="c": + print("Moving " + nb_mails + " emails to Trash") + messages = data[0].split(b' ') + for mail in messages: + # move to trash + conn.store(mail, '+X-GM-LABELS', '\\Trash') + + #This block empties trash, remove if you want to keep, Gmail auto purges trash after 30 days. + print("Emptying Trash & Expunge...") + conn.select('[Gmail]/Corbeille') + conn.store("1:*", '+FLAGS', '\\Deleted') + # delete all the selected messages + conn.expunge() + print("Script completed") +else: + print("Script aborted") + +# deconnexion du serveur +conn.close() +conn.logout() + diff --git a/imap-APP-office365-dumas.py b/imap-APP-office365-dumas.py new file mode 100644 index 0000000..4fb2a3a --- /dev/null +++ b/imap-APP-office365-dumas.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +import imaplib +import msal +import pprint + +conf = { + "authority": "https://login.microsoftonline.com/47f02128-acf0-47a7-a3bf-27a3619d4a4f", + "client_id": "4fdf2634-a260-4576-a442-684998f0187b", #AppID + "scope": ['https://outlook.office365.com/.default'], + "secret": "dZD8Q~GmG_PFi.t_uRiyBEzwLeMZeemQGHUFta~J", #Key-Value +} + +def generate_auth_string(user, token): + return f"user={user}\x01auth=Bearer {token}\x01\x01" + +if __name__ == "__main__": + app = msal.ConfidentialClientApplication(conf['client_id'], authority=conf['authority'], + client_credential=conf['secret']) + + result = app.acquire_token_silent(conf['scope'], account=None) + + if not result: + print("No suitable token in cache. Get new one.") + result = app.acquire_token_for_client(scopes=conf['scope']) + + if "access_token" in result: + print(result['token_type']) + pprint.pprint(result) + else: + print(result.get("error")) + print(result.get("error_description")) + print(result.get("correlation_id")) + + imap = imaplib.IMAP4('outlook.office365.com') + imap.starttls() + imap.authenticate("XOAUTH2", lambda x: generate_auth_string("peinture-dumas@entreprise-dumas.com", result['access_token']).encode("utf-8")) + + # Print list of mailboxes on server + code, mailboxes = imap.list() + for mailbox in mailboxes: + print(mailbox.decode("utf-8")) + # Select mailbox + imap.select("INBOX") + # Cleanup + imap.close() \ No newline at end of file diff --git a/imap-SSL-alinto-dumas.py b/imap-SSL-alinto-dumas.py new file mode 100644 index 0000000..20aba48 --- /dev/null +++ b/imap-SSL-alinto-dumas.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +import imaplib + +if __name__ == "__main__": + # connecter au serveur IMAP avec SSL + # authentification basique (email / password) + imap = imaplib.IMAP4_SSL('imap.entreprise-dumas.com') + # se connecter à la mailbox + mbx_name = 'peinture-dumas@entreprise-dumas.com' + mbx_pwd = 'S@sdumas69' + imap.login(mbx_name, mbx_pwd) + + # Print list of mailboxes on server + code, mailboxes = imap.list() + for mailbox in mailboxes: + print(mailbox.decode("unicode")) + # Select mailbox + imap.select("INBOX") + # Cleanup + imap.close() \ No newline at end of file diff --git a/smtp-SSL-alinto-dumas.py b/smtp-SSL-alinto-dumas.py new file mode 100644 index 0000000..04f97eb --- /dev/null +++ b/smtp-SSL-alinto-dumas.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +import smtplib +import ssl + +# Fabrication du corps du email_passwordMessage +sender = "sasdumas@entreprise-dumas.com" +receiver = "phuoc@caotek.fr" +message = """\ +Subject: Envoi d'un message en SSL + +Voici le contenu du message. Phuoc""" + +# Create a secure SSL context +context = ssl.create_default_context() +smtp_server = "gatewayxl.alinto.net" +smtp_port = 465 # For SSL +smtp_user = "smtpsasdumas@entreprise-dumas.com" +smtp_pass = "S@sdumas69" + +# Try to log in to server and send email +try: + server = smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) + server.login(smtp_user, smtp_pass) + # envoyer l'email + server.sendmail(sender, receiver, message) + print("sendmail -> OK") + +except Exception as e: + # Print any error messages to stdout + print(e) +finally: + server.quit() + diff --git a/smtp-TLS-office365-dumas.py b/smtp-TLS-office365-dumas.py new file mode 100644 index 0000000..63f6873 --- /dev/null +++ b/smtp-TLS-office365-dumas.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +import smtplib +import ssl + +# Fabrication du corps du email_passwordMessage +sender = "peinture-dumas@entreprise-dumas.com" +receiver = "phuoc@caotek.fr" +message = """\ +Subject: Envoi d'un message en TLS + +Voici le contenu du message. Phuoc""" + +# Create a secure SSL context +context = ssl.create_default_context() +smtp_server = "smtp.office365.com" +smtp_port = 587 # For TLS +smtp_user = "peinture-dumas@entreprise-dumas.com" +smtp_pass = "Nar50611" + +# Try to log in to server and send email +try: + server = smtplib.SMTP(smtp_server,smtp_port) + # server.ehlo() # Can be omitted + server.starttls(context=context) # Secure the connection + # server.ehlo() # Can be omitted + server.login(smtp_user, smtp_pass) + # envoyer l'email + server.sendmail(sender, receiver, message) + print("sendmail -> OK") + +except Exception as e: + # Print any error messages to stdout + print(e) +finally: + server.quit()