|
5 | 5 | from email.mime.text import MIMEText
|
6 | 6 | from email.mime.base import MIMEBase
|
7 | 7 | from email.mime.multipart import MIMEMultipart
|
8 |
| -from env import FROM_EMAIL, FROM_EMAIL_PASS, BCC_EMAIL_S, HOSTER_EMAIL |
| 8 | +from env import FROM_EMAIL, FROM_EMAIL_PASS, BCC_EMAIL_S, HOSTER_EMAIL, HOSTER_INTERESTED_ROLLS, ROLL_MAIL, ROLL_NAME |
9 | 9 |
|
10 | 10 |
|
11 |
| -def format_shortlist(): |
12 |
| - pass |
| 11 | +def send_shortlists(mails, gmail_api, smtp): |
| 12 | + print('[SENDING SHORTLIST UPDATES]', flush=True) |
| 13 | + |
| 14 | + if gmail_api: |
| 15 | + import base64 |
| 16 | + |
| 17 | + try: |
| 18 | + service = generate_send_service() |
| 19 | + except Exception as e: |
| 20 | + logging.error(f" Failed to generate GMAIL API creds ~ {str(e)}") |
| 21 | + return |
| 22 | + |
| 23 | + for mail in mails: |
| 24 | + try: |
| 25 | + response = service.users().messages().send( |
| 26 | + userId="me", |
| 27 | + body={"raw": base64.urlsafe_b64encode(mail.as_bytes()).decode()} |
| 28 | + ).execute() |
| 29 | + except Exception as e: |
| 30 | + logging.error(f" Failed to send request to GMAIL API : {mail['Subject']} -x-> ({mail['Bcc']}) ~ {str(e)}") |
| 31 | + break |
| 32 | + |
| 33 | + if 'id' in response: |
| 34 | + logging.info(f" [MAIL SENT] ~ {mail['Subject']} -> ({mail['Bcc']})") |
| 35 | + else: |
| 36 | + logging.error(f" Failed to Send Mail : {mail['Subject']} -x-> ({mail['Bcc']}) ~ {response}") |
| 37 | + break |
| 38 | + elif smtp: |
| 39 | + import ssl |
| 40 | + import smtplib |
| 41 | + context = ssl.create_default_context() |
| 42 | + |
| 43 | + logging.info(" [Connecting to smtp.google.com] ...") |
| 44 | + with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: |
| 45 | + logging.info(" [Connected!]") |
| 46 | + try: |
| 47 | + server.login(FROM_EMAIL, FROM_EMAIL_PASS) |
| 48 | + logging.info(" [Logged In!]") |
| 49 | + except Exception as e: |
| 50 | + logging.error(f" Failed to log in ~ {str(e)}") |
| 51 | + return |
| 52 | + |
| 53 | + for mail in mails: |
| 54 | + try: |
| 55 | + server.sendmail(mail["From"], mail["Bcc"].split(', '), mail.as_string()) |
| 56 | + logging.info(f" [MAIL SENT] ~ {mail['Subject']} -> ({mail['Bcc']})") |
| 57 | + except smtplib.SMTPException as e: |
| 58 | + logging.error(f" Failed to Send Mail : {mail['Subject']} -x-> ({mail['Bcc']}) ~ {str(e)}") |
| 59 | + break |
| 60 | + |
| 61 | + |
| 62 | +def format_shortlists(shortlists): |
| 63 | + print('[FORMATTING SHORTLIST UPDATES]', flush=True) |
| 64 | + |
| 65 | + table_body = """ |
| 66 | + <html> |
| 67 | + <body> |
| 68 | + <div style="font-family: Arial, sans-serif; width: 90%; margin: 0 auto; border: 1px solid #333; padding: 20px; margin-bottom: 20px; border-radius: 10px; box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);"> |
| 69 | + <div align="center"> |
| 70 | + <table style="width: 100%; border-collapse: collapse;"> |
| 71 | + <thead> |
| 72 | + <tr style="background-color: #f2f2f2;"> |
| 73 | + {columns} |
| 74 | + </tr> |
| 75 | + </thead> |
| 76 | + <tbody> |
| 77 | + {rows} |
| 78 | + </tbody> |
| 79 | + </table> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </body> |
| 83 | + </html> |
| 84 | + """ |
| 85 | + |
| 86 | + host_interested_message = MIMEMultipart() |
| 87 | + host_interested_message["Subject"] = "People you are interested in are shortlisted!" |
| 88 | + host_interested_message["From"] = f'MFTP < {FROM_EMAIL} >' |
| 89 | + host_interested_message["Bcc"] = ", ".join(HOSTER_EMAIL) |
| 90 | + |
| 91 | + host_interested_cols = """ |
| 92 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Name (count)</th> |
| 93 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Company (notice_id)</th> |
| 94 | + """ |
| 95 | + |
| 96 | + def generate_row(company_shortlist): |
| 97 | + return f""" |
| 98 | + <tr> |
| 99 | + <td style="border: 1px solid #ddd; padding: 8px;">{company_shortlist['company']} (#{company_shortlist['id']})</td> |
| 100 | + <td style="border: 1px solid #ddd; padding: 8px;">{company_shortlist['count']}</td> |
| 101 | + </tr> |
| 102 | + """ |
| 103 | + |
| 104 | + def generate_host_interested_row(name, company_shortlist): |
| 105 | + return f""" |
| 106 | + <tr> |
| 107 | + <td style="border: 1px solid #ddd; padding: 8px;">{name} ({company_shortlist['count']})</td> |
| 108 | + <td style="border: 1px solid #ddd; padding: 8px;">{company_shortlist['company']} (#{company_shortlist['id']})</td> |
| 109 | + </tr> |
| 110 | + """ |
| 111 | + |
| 112 | + host_interested_rows_list = [] |
| 113 | + |
| 114 | + mails = [] |
| 115 | + for roll, shortlist in shortlists.items(): |
| 116 | + if roll in HOSTER_INTERESTED_ROLLS: |
| 117 | + name = ROLL_NAME.get(roll) |
| 118 | + host_interested_rows_list += [generate_host_interested_row(name, company_shortlist) for company_shortlist in shortlist] |
| 119 | + |
| 120 | + student_mails = ROLL_MAIL.get(roll) |
| 121 | + if student_mails is None: |
| 122 | + continue |
| 123 | + |
| 124 | + message = MIMEMultipart() |
| 125 | + message["Subject"] = "You have been shortlisted!" |
| 126 | + message["From"] = f'MFTP < {FROM_EMAIL} >' |
| 127 | + message["Bcc"] = ", ".join(student_mails) |
| 128 | + |
| 129 | + rows = ''.join(generate_row(company_shortlist) for company_shortlist in shortlist) |
| 130 | + columns = """ |
| 131 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Company (notice_id)</th> |
| 132 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Count</th> |
| 133 | + """ |
| 134 | + |
| 135 | + shortlist_table = table_body.format(columns=columns, rows=rows) |
| 136 | + message.attach(MIMEText(shortlist_table, "html")) |
| 137 | + |
| 138 | + mails.append(message) |
| 139 | + |
| 140 | + host_interested_rows = ''.join(host_interested_rows_list) |
| 141 | + host_interested_shortlist_table = table_body.format(columns=host_interested_cols, rows=host_interested_rows) |
| 142 | + host_interested_message.attach(MIMEText(host_interested_shortlist_table, "html")) |
| 143 | + mails.append(host_interested_message) |
| 144 | + |
| 145 | + return mails |
13 | 146 |
|
14 | 147 |
|
15 | 148 | def send_companies(mail, gmail_api, smtp):
|
|
0 commit comments