added scripts for testing smtp and imap

This commit is contained in:
2023-05-28 20:16:28 +02:00
parent 92142854df
commit 5ce9066824
8 changed files with 286 additions and 1 deletions

View File

@@ -157,3 +157,4 @@ textarea {
.dropdown-menu li a:hover { .dropdown-menu li a:hover {
background-color: red !important; background-color: red !important;
} }

50
email_avnes_purge.py Normal file
View File

@@ -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()

50
gmail_caotek_purge.py Normal file
View File

@@ -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()

51
gmail_ctphuoc_purge.py Normal file
View File

@@ -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()

View File

@@ -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()

20
imap-SSL-alinto-dumas.py Normal file
View File

@@ -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()

33
smtp-SSL-alinto-dumas.py Normal file
View File

@@ -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()

View File

@@ -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()