Utilisation d'un fichier .env pour encoder les paramètres sensibles.

This commit is contained in:
MJ Antipode 2023-06-21 23:26:25 +02:00
parent 6c2a2b2a71
commit 3ed2fb7b9d

View File

@ -1,11 +1,11 @@
"""Create a connection to Gmail and do something with the results
"""Create a connection to IMAPS mail account extract all unique adresses and output a single file with the results.
References:
http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/
and
https://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/
Grabbed from : https://gist.github.com/abought/15a1e08705b121c1b7bd
Initially grabbed from : https://gist.github.com/abought/15a1e08705b121c1b7bd
"""
__author__ = 'mj'
@ -15,15 +15,19 @@ import getpass
import sys
import re
import ssl
import os
from dotenv import load_dotenv
from pprint import pprint as pp
# Load environment variables from .env file
load_dotenv()
# User may want to change these parameters if running script as-is
# Search folders, multiple directories can be given
# TODO: A user will want to change this
SEARCH_FOLDER = ['"Trash"', '"INBOX"']
DEFAULT_MAIL_SERVER = 'mail.antipode.net'
DEFAULT_MAIL_SERVER = os.getenv("EMAIL_SERVER")
# Output file name
OUTPUT_FILE = "recipient_list.txt"
@ -34,8 +38,7 @@ ADDR_PATTERN = re.compile("<(.+)>") # Finds email as <nospam@nospam.com>
def connect(user, pwd, server=DEFAULT_MAIL_SERVER):
"""Connect to [the specified] mail server. Return an open connection"""
conn = imaplib.IMAP4_SSL(host=server,
ssl_context=ssl.create_default_context())
conn = imaplib.IMAP4_SSL(host=server, ssl_context=ssl.create_default_context())
try:
conn.login(user, pwd)
except imaplib.IMAP4.error:
@ -46,9 +49,9 @@ def connect(user, pwd, server=DEFAULT_MAIL_SERVER):
def print_folders(conn):
"""Print a list of open mailbox folders"""
for f in conn.list():
for i in f:
print("\t", i)
for f in conn.list()[1]:
folder_name = f.decode().split(' "/" ')[1]
print("\t", folder_name)
def get_mails_from_folder(conn, folder_name):
@ -99,8 +102,13 @@ def get_recipients(msg):
if __name__ == "__main__":
username = input("Enter username: ")
password = getpass.getpass("Enter password: ") # Use getpass to hide the password input
# Retrieve username and password from environment variables
username = os.getenv("EMAIL_USERNAME")
password = os.getenv("EMAIL_PASSWORD")
if username is None or password is None:
print("Error: Email username or password not found in environment variables.")
sys.exit(1)
# Connect
mail_conn = connect(username, password)
@ -110,26 +118,27 @@ if __name__ == "__main__":
# Open folders and get list of email message uids
all_recipients = []
for folder in SEARCH_FOLDER:
# switch to folder
# Switch to folder
for mail_id in get_mails_from_folder(mail_conn, folder):
data = fetch_message(mail_conn, mail_id)
recip_list = get_recipients(data)
all_recipients.extend(recip_list)
# Get unique recipients
unique_recipients = set(all_recipients)
# Write each recipient on a new line in the output file
for recipient in unique_recipients:
file.write(recipient + '\n')
# Delete the analyzed emails
for folder in SEARCH_FOLDER:
for mail_id in get_mails_from_folder(mail_conn, folder):
mail_conn.store(mail_id, '+FLAGS', '\\Deleted') # Mark the email as deleted
mail_conn.expunge() # Permanently remove the deleted emails
# Delete the analyzed emails
for folder in SEARCH_FOLDER:
for mail_id in get_mails_from_folder(mail_conn, folder):
mail_conn.store(mail_id, '+FLAGS', '\\Deleted') # Mark the email as deleted
mail_conn.expunge() # Permanently remove the deleted emails
# Close the connection
mail_conn.close()
mail_conn.logout()
# Close the connection
mail_conn.close()
mail_conn.logout()
print("Recipient list generated successfully and analyzed emails erased.")
print("Recipient list generated successfully and analyzed emails erased.")