Purpose:
This Python script sends an email using Gmail's SMTP server. It leverages the smtplib, email.mime.text, and email.mime.multipart modules to construct a MIME email object and send it securely.
Before You Run:
Security: Avoid storing sensitive credentials like passwords directly in the code. Instead, use environment variables or secure configuration files. Email Account: Ensure you're using a Gmail account that allows app passwords for less secure apps. Create one by following Google's instructions: [[invalid URL removed]]([invalid URL removed]) App Password: Generate an app password for your Gmail account and replace your app name in the code with it. Keep this password confidential. Code Breakdown:
Imports:
smtplib: Connects to the SMTP server. email.mime.text: Creates plain text email content. email.mime.multipart: Creates a multipart email structure. Email Configuration:
sender_email: Your Gmail address. receiver_email: The recipient's email address. subject: The email subject line. body: The email body content. MIME Object Creation:
message: Creates a MIME multipart object for the email. message["From"], message["To"], message["Subject"]: Sets the sender, recipient, and subject headers. message.attach(MIMEText(body, "plain")): Attaches the plain text body to the email. SMTP Connection and Login:
smtp_server: Gmail's SMTP server address (smtp.gmail.com). smtp_port: The SMTP port used by Gmail (587). username: Your Gmail address. Replace your email username with your actual address. password: Replace your app name with the app password generated earlier. with smtplib.SMTP(...) as server: Establishes a secure connection to the SMTP server using a context manager. server.starttls(): Enables TLS encryption for secure communication. server.login(username, password): Logs in to the SMTP server using your credentials. Send Email and Print Confirmation:
server.sendmail(sender_email, receiver_email, message.as_string()): Sends the email to the recipient. print("done"): Prints a confirmation message. Additional Notes:
This code is designed for educational purposes. Use it responsibly and ethically. Consider using a dedicated email library like smtplib-tls for improved security and features. Always test your code thoroughly before sending emails to real recipients.