Dhahlan2000 commited on
Commit
9d2fe8f
·
1 Parent(s): 8241f94

Add email sending functionality to app.py

Browse files

Implemented a new send_email function to allow users to send job application emails with CV attachments directly from the application. Enhanced the UI to include input fields for sender and receiver email addresses, subject, and an editable email body. This update improves user experience by streamlining the email generation and sending process, making it more efficient and user-friendly.

Files changed (1) hide show
  1. app.py +52 -0
app.py CHANGED
@@ -6,6 +6,11 @@ import os
6
  from PyPDF2 import PdfReader
7
  import docx
8
  import re
 
 
 
 
 
9
  from typing import Dict
10
 
11
  def extract_cv_text(file):
@@ -124,6 +129,37 @@ CV Summary:
124
  response += token
125
  yield response
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  # Streamlit UI section
128
  st.title("AI Job Application Email Generator")
129
 
@@ -153,6 +189,22 @@ def update_ui(message, cv_file, cv_text):
153
  st.error(f"Error during email generation: {str(e)}")
154
  else:
155
  st.warning("Please upload a CV and enter a job description.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
  # Add tabs for different sections
158
  tab1, tab2 = st.tabs(["Generate Email", "View CV Details"])
 
6
  from PyPDF2 import PdfReader
7
  import docx
8
  import re
9
+ import smtplib
10
+ from email.mime.multipart import MIMEMultipart
11
+ from email.mime.text import MIMEText
12
+ from email.mime.base import MIMEBase
13
+ from email import encoders
14
  from typing import Dict
15
 
16
  def extract_cv_text(file):
 
129
  response += token
130
  yield response
131
 
132
+ # Function to send the email with attachment
133
+ def send_email(sender_email: str, receiver_email: str, subject: str, body: str, attachment_path: str):
134
+ """Send email with CV attachment."""
135
+ try:
136
+ msg = MIMEMultipart()
137
+ msg['From'] = sender_email
138
+ msg['To'] = receiver_email
139
+ msg['Subject'] = subject
140
+
141
+ msg.attach(MIMEText(body, 'plain'))
142
+
143
+ # Attach the CV file
144
+ if attachment_path:
145
+ attachment = open(attachment_path, "rb")
146
+ part = MIMEBase('application', 'octet-stream')
147
+ part.set_payload(attachment.read())
148
+ encoders.encode_base64(part)
149
+ part.add_header('Content-Disposition', f'attachment; filename={os.path.basename(attachment_path)}')
150
+ msg.attach(part)
151
+
152
+ # Set up the server and send the email
153
+ server = smtplib.SMTP('smtp.gmail.com', 587)
154
+ server.starttls()
155
+ server.login(sender_email, os.getenv('EMAIL_PASSWORD')) # Replace with your email credentials
156
+ text = msg.as_string()
157
+ server.sendmail(sender_email, receiver_email, text)
158
+ server.quit()
159
+ st.success("Email sent successfully!")
160
+ except Exception as e:
161
+ st.error(f"Error sending email: {str(e)}")
162
+
163
  # Streamlit UI section
164
  st.title("AI Job Application Email Generator")
165
 
 
189
  st.error(f"Error during email generation: {str(e)}")
190
  else:
191
  st.warning("Please upload a CV and enter a job description.")
192
+
193
+ # Email input fields
194
+ st.markdown("### Sender & Receiver Information")
195
+ sender_email = st.text_input("Sender's Email Address")
196
+ receiver_email = st.text_input("Receiver's Email Address")
197
+
198
+ # Email subject
199
+ subject = st.text_input("Subject", value="Job Application for [Position Name]")
200
+
201
+ # Option to edit the generated email
202
+ email_body = st.text_area("Edit the Generated Email (if needed)", value=email_text, height=400)
203
+
204
+ # Send email button
205
+ if st.button("Send Email"):
206
+ if sender_email and receiver_email and email_body:
207
+ send_email(sender_email, receiver_email, subject, email_body, cv_file.name)
208
 
209
  # Add tabs for different sections
210
  tab1, tab2 = st.tabs(["Generate Email", "View CV Details"])