• XSS.stack #1 – первый литературный журнал от юзеров форума

Статья Python split mail by domain

p0tsu

CD-диск
Пользователь
Регистрация
25.04.2023
Сообщения
19
Реакции
15
Simple python script to run to split logs and combo by domain. There are no need to download sketchy tool on the web.


Код:
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os

def filter_emails_by_domains(emails, domains):
    filtered_emails = set()
    for email in emails:
        for domain in domains:
            if domain.lower() in email.lower():
                filtered_emails.add(email)
                break
    return filtered_emails

def export_emails_to_txt(emails, filename):
    with open(filename, 'w', encoding='utf-8') as file:
        for email in emails:
            file.write(email + '\n')
    print(f"Filtered emails exported to {filename} successfully!")

def main():
    domains_input = input("Enter the email domains to filter (comma-separated, e.g., gmail.com,hotmail.com): ")
    email_domains = [domain.strip() for domain in domains_input.split(',')]

    Tk().withdraw()  # Hide the Tkinter root window
    input_file = askopenfilename(filetypes=[("Text files", "*.txt")])  # Open file dialog for selecting .txt file
    with open(input_file, 'r', encoding='utf-8') as file:
        all_emails = file.read().splitlines()
    filtered_emails = filter_emails_by_domains(all_emails, email_domains)
    output_file = asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")], initialfile=f"{os.path.basename(input_file)}_{email_domains}", initialdir=os.path.dirname(input_file))

    if output_file:
        export_emails_to_txt(filtered_emails, output_file)
    else:
        print("Export canceled.")

if __name__ == '__main__':
    main()
 
Better use this instead because if domain.lower() in email.lower() matches any substring — for example, mail will match mailinator.com.
By splitting the email and comparing only the real domain part after @, you get accurate filtering without false positives
Python:
parts = email.rsplit('@', 1)
if len(parts) == 2:
    domain_part = parts[1].lower()
    if domain_part == domain.lower() or domain_part.endswith('.' + domain.lower()):
        filtered_emails.add(email)
        break
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх