A simple Python script to send emails using Gmail's SMTP server.
- Python 3.x installed
- A Gmail account
- App Password (not your regular Gmail password)
- Go to your Google Account settings
- Enable 2-Step Verification
- Visit: https://myaccount.google.com/apppasswords
- Select "Mail" as the app
- Select your device
- Generate and copy the 16-character app password
No additional installation required - uses Python's built-in smtplib library.
-
Edit the script with your credentials:
gmail_user = "your_email@gmail.com" gmail_password = "your_app_password_here"
-
Update recipient email:
to = ["recipient@example.com"]
-
Run the script:
python email_sender.py
- Remove or replace credentials before sharing
- Use environment variables for production
- Consider using
.envfiles for local development
- Secure SSL connection to Gmail SMTP (port 465)
- Basic error handling
- Simple email formatting
- Console feedback for success/failure
Common issues and solutions:
- Authentication Error: Ensure you're using an App Password, not your regular Gmail password
- Connection Refused: Check firewall settings or try enabling "Less secure app access" (deprecated)
- Email Not Received: Check recipient's spam folder
For production use, consider:
- Using environment variables
- Implementing a configuration file
- Adding input validation
- Using email templates for complex emails
This script is for educational purposes. Use responsibly and in compliance with Gmail's terms of service.