Python Email Sending Script
This script demonstrates how to send an email using Python's smtplib
and email
modules. It utilizes a secure connection with SSL for enhanced security.
Requirements
- Python 3 (tested with 3.x)
smtplib
module (included in standard library)email
module (included in standard library)ssl
module (included in standard library)
Instructions
-
Install dependencies (if necessary):
You can usually skip this step if you're using a modern Python environment.
pip install smtplib email
-
Configuration
- Replace the placeholders in the script with your actual email credentials and recipient address.
email_sender
(your Gmail address)email_password
(your Gmail app password - see instructions below)email_receiver
(recipient's email address)
- Optionally, modify the email subject (
subject
) and body (body
) as desired.
- Replace the placeholders in the script with your actual email credentials and recipient address.
-
Generating a Gmail App Password:
Since storing your actual Gmail password in plain text is insecure, it's recommended to create a dedicated app password for this script. Here's how:
- Go to your Google Account security settings: https://myaccount.google.com/intro/security
- Under "Signing in to Google," select "App passwords."
- Choose "Select app" and "Other (Custom name)."
- Enter a name for this script (e.g., "Python Email Script").
- Click "Generate" and copy the app password. Replace the
email_password
placeholder with this copied password.
-
Run the script:
Save the code as a Python file (e.g.,
email_sender.py
) and execute it from your terminal:python email_sender.py
Disclaimer
- This script is provided for educational purposes only. Please use it responsibly and adhere to ethical email sending practices.
- Be mindful of potential spam filters when sending automated emails.
Additional Notes
- The script utilizes
smtplib.SMTP_SSL
for a secure connection over port 465. - The
email
module is used to construct the email message with headers (From, To, Subject) and body content. - The script establishes a context using
ssl.create_default_context()
for secure communication.
Happy coding!