Skip to content

Instantly share code, notes, and snippets.

@laxyapahuja
Created June 15, 2020 10:27
Show Gist options
  • Save laxyapahuja/41a628c2566a1296b0dc510fbfac1049 to your computer and use it in GitHub Desktop.
Save laxyapahuja/41a628c2566a1296b0dc510fbfac1049 to your computer and use it in GitHub Desktop.
v dirty code
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import xlrd
workbook = xlrd.open_workbook(r'PATH-TO-EXCEL-FILE')
encryptidwork = xlrd.open_workbook(r'PATH-TO-EXCEL-FILE')
sheet = workbook.sheet_by_index(0)
sheetencryptid = encryptidwork.sheet_by_index(0)
dictMerit = {}
lst = []
sender = "ENTER-EMAIL-ADDRESS-HERE"
password = "ENTER-PASSWORD-HERE"
positionDict = {1:"1st",2:"2nd",3:"3rd"}
def mail(sender, password, name, email, event, position):
fromaddr = sender
toaddr = email
# instance of MIMEMultipart
msg = MIMEMultipart()
# storing the senders email address
msg['From'] = fromaddr
# storing the receivers email address
msg['To'] = toaddr
# storing the subject
msg['Subject'] = "Certificate of Merit - inTech'20"
# string to store the body of the mail
if position == '':
body = "EMAIL-BODY"
else:
body = "EMAIL-BODY"
# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = name + ".pdf"
if event == "Encryptid":
if position == '':
attachment = open("PATH-TO-ATTACHMENT", "rb")
else:
attachment = open("PATH-TO-ATTACHMENT", "rb")
else:
if position == '':
attachment = open("PATH-TO-ATTACHMENT", "rb")
else:
attachment = open("PATH-TO-ATTACHMENT", "rb")
p = MIMEApplication(attachment.read(),_subtype="pdf")
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.ehlo()
s.login(fromaddr, password)
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
s.sendmail(fromaddr, toaddr, text)
# terminating the session
s.quit()
def participation(sheet):
print("ok")
for i in range(44,73):
position = sheet.cell_value(i,7)
if position == '':
name = sheet.cell_value(i,2)
email = sheet.cell_value(i, 1)
event = sheet.cell_value(i, 4)
position = ''
if name not in lst:
lst.append(name)
a = name.split()
firstName, lastName = a[0].capitalize(), a[1].capitalize()
name = firstName + " " + lastName
mail(sender, password, name, email, event, position)
print(name + " done")
def merit(sheet):
print("ok")
for i in range(1,73):
position = sheet.cell_value(i,7)
if position != '':
name = sheet.cell_value(i,2)
email = sheet.cell_value(i, 1)
event = sheet.cell_value(i, 4)
lst.append(name)
a = name.split()
firstName, lastName = a[0].capitalize(), a[1].capitalize()
name = firstName + " " + lastName
mail(sender, password, name, email, event, position)
print(name + " done")
def encryptidmerit(sheet):
print("ok")
for i in range(1,30):
position = sheet.cell_value(i,3)
if position != '':
name = sheet.cell_value(i,1)
email = sheet.cell_value(i, 0)
event = sheet.cell_value(i, 2)
lst.append(name)
a = name.split()
print(a)
firstName, lastName = a[0].capitalize(), a[1].capitalize()
name = firstName + " " + lastName
mail(sender, password, name, email, event, position)
print(name + " done")
def encryptidpart(sheet):
print("ok")
for i in range(1,30):
position = sheet.cell_value(i,3)
if position == '':
name = sheet.cell_value(i,1)
email = sheet.cell_value(i, 0)
event = sheet.cell_value(i, 2)
if name not in lst:
lst.append(name)
a = name.split()
firstName, lastName = a[0].capitalize(), a[1].capitalize()
name = firstName + " " + lastName
mail(sender, password, name, email, event, position)
print(name + " done")
encryptidpart(sheetencryptid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment