Skip to content

Instantly share code, notes, and snippets.

@nicksnell
Created September 2, 2012 20:11
Show Gist options
  • Select an option

  • Save nicksnell/3604131 to your computer and use it in GitHub Desktop.

Select an option

Save nicksnell/3604131 to your computer and use it in GitHub Desktop.
Bulk Password Generator & Mailer
"""Utility to send passwords to a list of addresses in a CSV file"""
import os
import sys
import csv
import string
import smtplib
from email.mime.text import MIMEText
from random import choice, randint, seed
# Variables
SMTP_HOST = 'localhost'
SMTP_USER = ''
SMTP_PASSWD = ''
FROM_ADDRESS = ''
SUBJECT = ''
def generate_random_password(length, choices=string.letters + string.digits):
"""Create a random password"""
seed(os.urandom(64))
return ''.join([choice(choices) for i in range(length)]).upper()
def send_bulk_passwords(csv_file, email_file, length=randint(6, 8)):
mail_csv_reader = csv.reader(open(csv_file, 'rb'))
email_message = open(email_file, 'r').read()
counter = 0
for row in mail_csv_reader:
email = row[0]
name = row[1] if len(row) > 1 else ''
passwd = generate_random_password(length)
msg = MIMEText(email_message.format(
email=email,
name=name,
passwd=passwd
))
msg['Subject'] = SUBJECT
msg['From'] = FROM_ADDRESS
msg['To'] = email
s = smtplib.SMTP(SMTP_HOST)
if SMTP_USER and SMTP_PASSWD:
s.login(SMTP_USER, SMTP_PASSWD)
s.sendmail(FROM_ADDRESS, [email], msg.as_string())
s.quit()
counter += 1
print 'Sent messages: %s' % counter
if __name__ == '__main__':
send_bulk_passwords(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment