|
| 1 | +import smtplib |
| 2 | +import ssl |
| 3 | +from email.message import EmailMessage |
| 4 | + |
| 5 | +def send_bulk_email(sender_email, sender_name, sender_password, subject, body, recipients_file): |
| 6 | + try: |
| 7 | + with open(recipients_file, 'r') as file: |
| 8 | + recipients = [line.strip() for line in file if line.strip()] |
| 9 | + except FileNotFoundError: |
| 10 | + print("Error: The recipients file was not found.") |
| 11 | + return |
| 12 | + |
| 13 | + if not recipients: |
| 14 | + print("Error: No recipients found in the file.") |
| 15 | + return |
| 16 | + |
| 17 | + smtp_server = "smtp.gmail.com" |
| 18 | + smtp_port = 465 |
| 19 | + |
| 20 | + context = ssl.create_default_context() |
| 21 | + |
| 22 | + try: |
| 23 | + with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server: |
| 24 | + server.login(sender_email, sender_password) |
| 25 | + |
| 26 | + for recipient in recipients: |
| 27 | + msg = EmailMessage() |
| 28 | + msg['From'] = f"{sender_name} <{sender_email}>" |
| 29 | + msg['To'] = recipient |
| 30 | + msg['Subject'] = subject |
| 31 | + msg.set_content(body) |
| 32 | + |
| 33 | + server.send_message(msg) |
| 34 | + print(f"Email sent to {recipient}") |
| 35 | + |
| 36 | + except smtplib.SMTPAuthenticationError: |
| 37 | + print("Error: Authentication failed. Check your email and app password.") |
| 38 | + except Exception as e: |
| 39 | + print(f"Error: {e}") |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + sender_email = input("Enter your Gmail address: ") |
| 43 | + sender_password = input("Enter your Gmail app password: ") |
| 44 | + sender_name = input("Enter sender name: ") |
| 45 | + subject = input("Enter email subject: ") |
| 46 | + body = input("Enter email body: ") |
| 47 | + recipients_file = input("Enter the path to the recipients text file: ") |
| 48 | + |
| 49 | + send_bulk_email(sender_email, sender_name, sender_password, subject, body, recipients_file) |
0 commit comments