A Python script that creates email addresses from names, saves them to a file, and sends them via email using Gmail's SMTP service.
- File Creation: Generates email addresses by appending "@gmail.com" to names
- File Reading: Reads email addresses from a text file
- Email Sending: Sends the list of emails via Gmail with user confirmation
- User Interaction: Prompts user before sending emails
- Error Handling: Includes try-catch blocks for email operations
- Python 3.x
- Gmail account with App Password enabled
- Enable 2-Factor Authentication on your Google Account
- Generate App Password at: https://myaccount.google.com/apppasswords
- Select "Mail" β Choose device β Generate
- Copy the 16-character password
Edit these variables in the script:
sender_email = "your_email@gmail.com" # Your Gmail address
sender_password = "your_app_password_here" # Your 16-char App Password
receiver_email = "recipient@example.com" # Who receives the emailEdit the lines list to add your names:
lines = [
"John",
"Jane",
"Mike",
"Sarah"
]Creates abc.txt with email addresses:
John@gmail.com
Jane@gmail.com
Mike@gmail.com
Sarah@gmail.com
Reads the created file and displays emails in console.
- Prompts user for confirmation
- Connects to Gmail SMTP server
- Sends email containing all addresses from the file
- Uses secure SSL connection (port 465)
File 'abc.txt' created successfully!
Emails read from file:
John@gmail.com
Jane@gmail.com
Mike@gmail.com
Sarah@gmail.com
Do you want to send the email? (yes/no): yes
Email sent successfully!
email-list-automation/
βββ email_script.py # Main script
βββ abc.txt # Generated email file
βββ README.md # This file
# β UNSAFE (in production code):
sender_password = "your_app_password_here"
# β
SAFE Alternatives:
# Option 1: Environment variables
import os
sender_password = os.getenv("GMAIL_APP_PASSWORD")
# Option 2: User input
sender_password = input("Enter your app password: ")
# Option 3: .env file (using python-dotenv)- Replace real email addresses with placeholders
- Remove or replace actual passwords
- Add sensitive files to
.gitignore:*.txt .env
| Issue | Solution |
|---|---|
Authentication failed |
Verify App Password is correct |
File not found |
Script must be run from correct directory |
SMTP connection error |
Check firewall/internet connection |
Permission denied |
Ensure you can write to current directory |
SSL errors |
Try port 587 with starttls() instead |
If SSL doesn't work, modify the sending function:
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls() # Upgrade to TLS
server.login(sender_email, sender_password)
server.send_message(message)# Replace "@gmail.com" with any domain
file.write(line + "@company.com\n")
file.write(line + "@yahoo.com\n")# Read names from user input
names = []
while True:
name = input("Enter name (or 'done' to finish): ")
if name.lower() == 'done':
break
names.append(name)# Customize email body
email_content = "Employee Email Directory:\n\n"
for index, email in enumerate(emails, 1):
email_content += f"{index}. {email}"- Creating contact lists from name databases
- Sending email directories to HR departments
- Generating test email accounts
- Educational demonstrations of file I/O and email automation
- Test with your own email as receiver first
- Send to a test inbox before production use
- Check spam folder if email doesn't arrive
- Verify file permissions in your directory
For educational purposes only. Ensure compliance with:
- Gmail's Terms of Service
- Anti-spam regulations (CAN-SPAM Act)
- Data privacy laws (GDPR, CCPA)
- Enable 2FA on Google Account
- Generate App Password
- Update script with your credentials
- Modify name list as needed
- Run script in safe environment
- Verify email receipt