diff --git a/extractor.py b/extractor.py index 3f1b878..02db35b 100644 --- a/extractor.py +++ b/extractor.py @@ -16,7 +16,9 @@ import sys import re import ssl import os +import smtplib from dotenv import load_dotenv +from email.message import EmailMessage from pprint import pprint as pp # Load environment variables from .env file @@ -32,6 +34,12 @@ DEFAULT_MAIL_SERVER = os.getenv("EMAIL_SERVER") # Output file name OUTPUT_FILE = "recipient_list.txt" +# Email settings +SENDER_SMTP = os.getenv("SENDER_SMTP") +SENDER_EMAIL = os.getenv("SENDER_EMAIL") +SENDER_PASSWORD = os.getenv("SENDER_PASSWORD") +RECIPIENT_EMAIL = os.getenv("RECIPIENT_EMAIL") + # No user parameters below this line ADDR_PATTERN = re.compile("<(.+)>") # Finds email as @@ -126,6 +134,7 @@ if __name__ == "__main__": # Get unique recipients unique_recipients = set(all_recipients) + unique_recipient_count = len(unique_recipients) # Write each recipient on a new line in the output file for recipient in unique_recipients: @@ -141,4 +150,16 @@ if __name__ == "__main__": mail_conn.close() mail_conn.logout() - print("Recipient list generated successfully and analyzed emails erased.") + # Send email with the count of unique addresses + msg = EmailMessage() + msg['Subject'] = 'Mail extractor unique recipient count' + msg['From'] = SENDER_EMAIL + msg['To'] = RECIPIENT_EMAIL + msg.set_content(f"The count of unique recipients is: {unique_recipient_count}") + + with smtplib.SMTP(SENDER_SMTP, 587) as server: + server.starttls() + server.login(SENDER_EMAIL, SENDER_PASSWORD) + server.send_message(msg) + + print("Recipient list generated successfully, analyzed emails erased, and email sent.")