|
| 1 | +import smtplib |
| 2 | +from email.mime.text import MIMEText |
| 3 | + |
| 4 | +def main(): |
| 5 | + print("Welcome to the Email Automation Script!") |
| 6 | + |
| 7 | + # Get sender's email and password |
| 8 | + sender_email = input('Enter your email: ') |
| 9 | + |
| 10 | + # Security Warning |
| 11 | + print("\n** SECURITY WARNING **") |
| 12 | + print("Entering your email password directly in the script can be a security risk.") |
| 13 | + print("Consider using app passwords for Gmail or other secure authentication methods.") |
| 14 | + print("Do not share your scripts containing passwords with others.") |
| 15 | + print("Continue only if you understand and accept these risks.\n") |
| 16 | + |
| 17 | + print('If you have 2-factor authentication turned on, make sure to generate an app password. \nOtherwise, enter your password.') |
| 18 | + sender_password = input('Enter the Password: ') |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + # Get receiver's emails |
| 23 | + receivers = input('Enter the receiver\'s emails separated by commas: ') |
| 24 | + receiver_emails = [x.strip() for x in receivers.split(',')] |
| 25 | + |
| 26 | + smtp_server = "smtp.gmail.com" # SMTP server for Gmail only |
| 27 | + smtp_port = 587 # Port 587 is the secure SMTP port for Gmail |
| 28 | + |
| 29 | + # Create email message |
| 30 | + message = MIMEText(input('Enter the mail body: ')) |
| 31 | + message["Subject"] = input("Subject: ") if input("Enter a subject (leave empty for default): ") else "Automated Email" |
| 32 | + message["From"] = sender_email |
| 33 | + message["To"] = ", ".join(receiver_emails) |
| 34 | + |
| 35 | + # Call function to send email |
| 36 | + send_auto_mail(sender_email, sender_password, receiver_emails, smtp_server, smtp_port, message) |
| 37 | + |
| 38 | +def send_auto_mail(sender_email, sender_password, receiver_emails, smtp_server, smtp_port, message): |
| 39 | + try: |
| 40 | + with smtplib.SMTP(smtp_server, smtp_port) as server: |
| 41 | + server.starttls() |
| 42 | + server.login(sender_email, sender_password) |
| 43 | + |
| 44 | + # Send email |
| 45 | + server.sendmail(sender_email, receiver_emails, message.as_string()) |
| 46 | + |
| 47 | + print("Email sent successfully!") |
| 48 | + |
| 49 | + except Exception as e: |
| 50 | + print('Error sending mail:', str(e)) |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments