Skip to content

Instantly share code, notes, and snippets.

@priyanshnama
Last active May 1, 2020 06:43
Show Gist options
  • Save priyanshnama/8e2a5a17d0ac4671247118753dd8150e to your computer and use it in GitHub Desktop.
Save priyanshnama/8e2a5a17d0ac4671247118753dd8150e to your computer and use it in GitHub Desktop.
Send messages to thousands of recipients stored in excel via python.!!
import pip
xl_driver = pip.main(["install", "openpyxl"])
smtp_driver = pip.main(["install","smtplib"])
import openpyxl as xl
import smtplib
# Mail_from_Excel.py
# Send messages to thousands of recipients stored in excel via python
print("Welcome to Mails from Excel")
print("Fetching Data...")
workbook = input("Enter file name with extension:").strip()
wb = xl.load_workbook(workbook)
print("Done loading Excel file:")
sheet = input("Enter sheet name ").strip()
sheet = wb[sheet]
print("Done loading Sheet")
print("Getting Data from Excel Sheet...")
email_first_cell = input("Enter Cell Address of first Email:").strip()
email_col = ord(email_first_cell[0].upper()) - 64
mail_start_row = int(email_first_cell[1])
name_first_cell = input("Enter Cell Address of first Name:").strip()
name_col = ord(name_first_cell[0].upper()) - 64
name_start_row = int(name_first_cell[1])
if mail_start_row != name_start_row:
k = input(f"Mismatch in Excel Workbook... Check {workbook} ... Press any key to exit.")
exit()
names = []
emails = []
for row in range(mail_start_row, sheet.max_row + 1):
email = sheet.cell(row, email_col).value
name = sheet.cell(row, name_col).value
names.append(name)
emails.append(email)
print()
print("Scanned emails shown below")
print(emails)
print()
print("Scanned names shown below")
print(names)
print()
my_name = input("Enter your Name:")
initials = input("Enter your Initials:")
print()
message_file = input("Enter Message file name with extension:")
message_file = open(message_file, encoding='ascii', errors='ignore')
message = message_file.read()
print("Message loaded successfully... message in next line.")
print()
message = message.replace("{my_name}", my_name)
print(message)
print()
subject_file = input("Enter Subject file name with extension:")
subject_file = open(subject_file, encoding='ascii', errors='ignore')
subject = subject_file.read()
print("Subject loaded successfully... message in next line.")
subject = subject.replace("{initials}", initials)
subject = subject.replace("worlds impending problems", "impending problems of worlds")
print(subject)
print()
print()
print("Starting Mail service...")
print()
email_user = input("Enter your email id:")
print()
server_name = input('''Enter the SMTP server name ("smtp.gmail.com" for Gmail users) ''')
print()
server = smtplib.SMTP(server_name, 587)
server.starttls()
print("Server Started...")
print("Ready to send emails.. Template below...")
print()
print(f'''From: {email_user}\nTo: %s\nSubject: {subject}\n\n{message}''' % (", ".join(emails)))
req = input("Are you Ready to send emails (Y or N):")
if req != 'Y':
exit()
print()
user_password = input("Enter Password:")
server.login(email_user, user_password)
print("Login Successful...")
for row in range(0, len(emails)):
email = emails[row]
name = names[row]
message_send = message
message_send = message_send.replace("{name}", name)
mes = f'''From: {email_user}\nTo: {email}\nSubject: {subject}\n\n{message_send}'''
print(f'''sending mail {row + 1} of {len(emails)}''')
print(mes)
server.sendmail(email_user, email, mes)
print(f'''email sent {row + 1} of {len(emails)}''')
d = input(f"All mails sent successfully. Press any key and hit enter to exit.")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment