169 lines
5.1 KiB
Python
169 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Email sender for user credentials
|
|
Sends username and password to all users in user_data.csv
|
|
"""
|
|
|
|
import csv
|
|
import smtplib
|
|
import time
|
|
import os
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.header import Header
|
|
from datetime import datetime
|
|
|
|
# ============ CONFIGURATION ============
|
|
# TODO: Fill in your NetEase email credentials before running
|
|
SENDER_EMAIL = "sofomoupc@163.com" # Your NetEase email address
|
|
SMTP_PASSWORD = "FERFPaJFJ89ckKw5" # SMTP authorization code (NOT login password)
|
|
SMTP_SERVER = "smtp.163.com" # Use smtp.126.com for @126.com, smtp.yeah.net for @yeah.net
|
|
SMTP_PORT = 465 # SSL port
|
|
|
|
# Email settings
|
|
EMAIL_SUBJECT = "“海察”软件下载网址用户名、密码"
|
|
# Use absolute path to CSV file
|
|
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
CSV_FILE = os.path.join(SCRIPT_DIR, "user_data.csv")
|
|
|
|
# Delay between emails (seconds) to avoid being flagged as spam
|
|
DELAY_BETWEEN_EMAILS = 2
|
|
# =======================================
|
|
|
|
|
|
def create_email_body(username, password):
|
|
"""Create email body with user credentials"""
|
|
return f"""用户名:{username}
|
|
密码:{password}"""
|
|
|
|
|
|
def send_email(sender, password, recipient, subject, body):
|
|
"""Send email via NetEase SMTP server"""
|
|
try:
|
|
# Create message
|
|
msg = MIMEMultipart()
|
|
msg['From'] = Header(sender)
|
|
msg['To'] = Header(recipient)
|
|
msg['Subject'] = Header(subject, 'utf-8')
|
|
|
|
# Attach body
|
|
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
|
|
|
# Connect to SMTP server with SSL
|
|
server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
|
|
server.login(sender, password)
|
|
|
|
# Send email
|
|
server.sendmail(sender, recipient, msg.as_string())
|
|
server.quit()
|
|
|
|
return True, None
|
|
except Exception as e:
|
|
return False, str(e)
|
|
|
|
|
|
def read_users_from_csv(filename):
|
|
"""Read user data from CSV file"""
|
|
users = []
|
|
try:
|
|
with open(filename, 'r', encoding='utf-8') as f:
|
|
reader = csv.DictReader(f)
|
|
for row in reader:
|
|
users.append({
|
|
'username': row['username'],
|
|
'passwd': row['passwd'],
|
|
'email': row['email']
|
|
})
|
|
return users
|
|
except Exception as e:
|
|
print(f"Error reading CSV file: {e}")
|
|
return None
|
|
|
|
|
|
def main():
|
|
"""Main function to send emails to all users"""
|
|
print("=" * 60)
|
|
print("User Credentials Email Sender")
|
|
print("=" * 60)
|
|
|
|
# Validate configuration
|
|
if SENDER_EMAIL == "your_email@163.com" or SMTP_PASSWORD == "your_smtp_auth_code":
|
|
print("\n❌ ERROR: Please configure your email credentials first!")
|
|
print("Edit the CONFIGURATION section at the top of this script:")
|
|
print(" - SENDER_EMAIL: Your NetEase email address")
|
|
print(" - SMTP_PASSWORD: Your SMTP authorization code")
|
|
print(" - SMTP_SERVER: smtp.163.com (or smtp.126.com, smtp.yeah.net)")
|
|
return
|
|
|
|
# Read users from CSV
|
|
print(f"\n📂 Reading users from {CSV_FILE}...")
|
|
users = read_users_from_csv(CSV_FILE)
|
|
|
|
if not users:
|
|
print("❌ Failed to read users from CSV file")
|
|
return
|
|
|
|
print(f"✓ Found {len(users)} users")
|
|
|
|
# Confirm before sending
|
|
print(f"\n📧 Ready to send {len(users)} emails")
|
|
print(f" From: {SENDER_EMAIL}")
|
|
print(f" Subject: {EMAIL_SUBJECT}")
|
|
response = input("\nProceed? (yes/no): ")
|
|
|
|
if response.lower() not in ['yes', 'y']:
|
|
print("Cancelled.")
|
|
return
|
|
|
|
# Send emails
|
|
print(f"\n🚀 Sending emails...\n")
|
|
success_count = 0
|
|
failed_users = []
|
|
|
|
for i, user in enumerate(users, 1):
|
|
username = user['username']
|
|
passwd = user['passwd']
|
|
email = user['email']
|
|
|
|
print(f"[{i}/{len(users)}] Sending to {username} ({email})...", end=" ")
|
|
|
|
# Create email body
|
|
body = create_email_body(username, passwd)
|
|
|
|
# Send email
|
|
success, error = send_email(SENDER_EMAIL, SMTP_PASSWORD, email, EMAIL_SUBJECT, body)
|
|
|
|
if success:
|
|
print("✓")
|
|
success_count += 1
|
|
else:
|
|
print(f"✗ Failed: {error}")
|
|
failed_users.append({'user': username, 'email': email, 'error': error})
|
|
|
|
# Delay between emails
|
|
if i < len(users):
|
|
time.sleep(DELAY_BETWEEN_EMAILS)
|
|
|
|
# Summary
|
|
print("\n" + "=" * 60)
|
|
print(f"✓ Successfully sent: {success_count}/{len(users)}")
|
|
|
|
if failed_users:
|
|
print(f"✗ Failed: {len(failed_users)}")
|
|
print("\nFailed users:")
|
|
for failed in failed_users:
|
|
print(f" - {failed['user']} ({failed['email']}): {failed['error']}")
|
|
|
|
# Save failed users to log
|
|
log_file = f"failed_emails_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
|
|
with open(log_file, 'w', encoding='utf-8') as f:
|
|
for failed in failed_users:
|
|
f.write(f"{failed['user']},{failed['email']},{failed['error']}\n")
|
|
print(f"\n📝 Failed users saved to: {log_file}")
|
|
|
|
print("=" * 60)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|