Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
1
Indexable
Never
import smtplib
from email.mime.text import MIMEText

def send_email(smtp_server, smtp_port, smtp_username, smtp_password, sender_email, sender_name, receiver_email, subject, message):
    msg = MIMEText(message, 'html')
    msg['Subject'] = subject
    msg['From'] = f"{sender_name} <{sender_email}>"
    msg['To'] = ', '.join(receiver_email)

    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.login(smtp_username, smtp_password)
        server.sendmail(sender_email, receiver_email, msg.as_string())

def read_smtp_servers(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return [line.strip().split(':') for line in file.readlines()]
    except FileNotFoundError:
        print(f"Erreur: Le fichier {file_path} n'a pas été trouvé.")
        return []
    except Exception as e:
        print(f"Erreur lors de la lecture du fichier {file_path}: {e}")
        return []

def read_recipients(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return [line.strip().split(':') for line in file.readlines()]
    except FileNotFoundError:
        print(f"Erreur: Le fichier {file_path} n'a pas été trouvé.")
        return []
    except Exception as e:
        print(f"Erreur lors de la lecture du fichier {file_path}: {e}")
        return []

if __name__ == "__main__":
    # Lire les serveurs SMTP à partir du fichier
    smtp_servers = read_smtp_servers('smtp_servers.txt')

    # Lire la liste des destinataires et expéditeurs à partir du fichier
    recipients = read_recipients('recipients.txt')

    if not smtp_servers or not recipients:
        print("Abandon de l'envoi d'e-mails en raison d'erreurs de configuration.")
    else:
        for recipient_and_sender in recipients:
            try:
                recipient, sender_email = recipient_and_sender
                sender_name = 'TelsSTRAAA'  # Ajouter le nom de l'expéditeur
                subject = 'Test subject'

                with open('lettre.html', 'r', encoding='utf-8') as file:
                    message = file.read()

                for server_info in smtp_servers:
                    try:
                        smtp_server, smtp_port, smtp_username, smtp_password = server_info

                        send_email(
                            smtp_server,
                            int(smtp_port),
                            smtp_username,
                            smtp_password,
                            sender_email,
                            sender_name,
                            [recipient],
                            subject,
                            message
                        )
                        print(f"Email envoyé avec succès à {recipient} via {smtp_server}:{smtp_port}. Expéditeur: {sender_name} <{sender_email}>")
                    except smtplib.SMTPAuthenticationError:
                        print(f"Échec de l'envoi d'e-mail à {recipient} via {smtp_server}:{smtp_port}. Erreur d'authentification.")
                    except Exception as e:
                        print(f"Échec de l'envoi d'e-mail à {recipient} via {smtp_server}:{smtp_port}. Erreur: {e}")
            except ValueError:
                print("Erreur: Le fichier recipients.txt doit contenir des lignes avec le format 'destinataire:expediteur'.")
Leave a Comment