import imaplib, smtplib, pprint, sys, os, sqlite3 sys.path.append('/home/anonymous/Documents/netnwork/') from email import message_from_bytes from email.header import decode_header from email.mime.text import MIMEText as mt from utils.process_pdf import txt_from_pdf from utils.process_docx import txt_from_docx from utils.extract_resume import extract, process_resume import string as s PATH = "/home/anonymous/Documents/netnwork/" imap_host = 'disroot.org' imap_user = 'yadnesh@disroot.org' imap_pass = '@1netNwork$2'#password here ALLOWED_EXTENSIONS = ['pdf', 'docx', 'txt'] def allowed_format(fname): return '.' in fname and fname.split('.')[-1] in ALLOWED_EXTENSIONS sanit = {} sanit['names'] = s.ascii_letters+s.digits+'_.' def sanitize(unsanitized, typ): if unsanitized is None: return None try: return ''.join(c for c in unsanitized if c in sanit[typ]) except: return "Invalid" def send_mail(email_id, subject, body): user_mail = mt(body) user_mail["From"] = imap_user user_mail["To"] = email_id user_mail["Subject"] = subject server = smtplib.SMTP("disroot.org", 587) server.starttls() server.login(imap_user,imap_pass) server.sendmail(imap_user, email_id, user_mail.as_string()) server.quit() def clean(text): # clean text for creating a folder return " ".join(c for c in subject.split() if c.isalpha()) def get_body_and_attachment(msg): body = ''; filename = ''; attachment = '' if msg.is_multipart(): for part in msg.walk(): # extract content type of email content_type = part.get_content_type() content_disposition = str(part.get("Content-Disposition")) try: # get the email body body = part.get_payload(decode=True).decode() except: pass if content_type == "text/plain" and "attachment" not in content_disposition: body = body elif "attachment" in content_disposition: # download attachment filename = part.get_filename() filename = sanitize(filename, 'names').lower() if filename: cwd = os.getcwd() filepath = os.path.join(cwd, filename) open(filepath, "wb").write(part.get_payload(decode=True)) else: # extract content type of email content_type = msg.get_content_type() # get the email body body = msg.get_payload(decode=True).decode() if content_type == "text/plain": body = body return body, filename, filepath def get_email_sender(msg): # decode email sender from_, encoding = decode_header(msg.get("From"))[0] if isinstance(from_, bytes): from_ = from_.decode(encoding) return from_[from_.find('<')+1:from_.find('>')] def get_subject(msg): # decode the email subject subject, encoding = decode_header(msg["Subject"])[0] if isinstance(subject, bytes): subject = subject.decode(encoding) subject = clean(subject) return subject def insert_to_table(tablename, json): conn = sqlite3.connect(PATH+"resume.db") c = conn.cursor() c.execute("SELECT * FROM "+tablename) colnames = [i[0] for i in c.description if i[0] in inputs] ques = ['?']*len(colnames) colnames = ','.join(colnames) #colname contains headers of db #change necessary fields to institute, company, #create input tuple_of_colname_values in that order according to table input values # ques = ','.join(ques) c.execute("SELECT * FROM "+tablename+" WHERE email=?", (out['email'],)) temp = c.fetchone() if temp!=None: c.execute("DELETE FROM "+tablename+" WHERE email=?", (out['email'],)) c.execute("INSERT INTO "+tablename+" ("+colnames+") VALUES ("+ques+")", (tuple_of_colname_values,)) conn.commit() c.close() conn.close() def get_unread(): # connect to host using SSL imap = imaplib.IMAP4_SSL(imap_host) # login to server imap.login(imap_user, imap_pass) # read mails from inbox, try imap.list() for other options imap.select("INBOX") status, response = imap.search(None, 'UNSEEN') if response == [b'']: print("No new mails.") return 0 if status == 'OK': messages = [int(i) for i in response[0].split()] else: messages = [] for i in messages: res, msg = imap.fetch(str(i), "(RFC822)") for response in msg: if isinstance(response, tuple): # parse a bytes email into a message object msg = message_from_bytes(response[1]) # get sender of msg from_ = get_email_sender(msg) # get subject of mail subject = get_subject(msg) body, filename, filepath = get_body_and_attachment(msg) if filename and filepath: out = process_resume(filename,filepath) insert_to_table('kundli', out) insert_to_table('edx', out) if out: if out['email'][0]==from_: subject = 'Application received' body = 'Thank you. Your application has been received. Your information is as follows:\n'+str(out) send_mail(get_email_sender(msg), subject, body) else: subject = 'Email in resume different from yours' body = 'Email id in your resume is different from the one you are sending your application from.\n Kindly send the application from the resume email id.' send_mail(get_email_sender(msg), subject, body) else: subject = 'Error in Application' body = 'Your application contains error.\nKindly send a single attachment of resume in pdf, docx or txt format with subject, [netnwork]' send_mail(get_email_sender(msg), subject, body) imap.close() get_unread() # PDF https://cryptpad.disroot.org/file/#/2/file/zkldxVHvGXoDhooPHCA7RurN/ # AWT https://cryptpad.disroot.org/file/#/2/file/4PyjwSu0BLdnTkTnuIhkO8Be/ #