Spaces:
Runtime error
Runtime error
Commit ·
e0339c8
1
Parent(s): 824e545
Upload 3 files
Browse files- AIConsultantBlackSmall.png +0 -0
- email_automation_V15.py +200 -0
- logoAIHighdefination.png +0 -0
AIConsultantBlackSmall.png
ADDED
|
|
email_automation_V15.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import smtplib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import time
|
| 4 |
+
import openpyxl
|
| 5 |
+
from email.mime.multipart import MIMEMultipart
|
| 6 |
+
from email.mime.text import MIMEText
|
| 7 |
+
from email.mime.image import MIMEImage
|
| 8 |
+
from email.utils import formataddr
|
| 9 |
+
import streamlit as st
|
| 10 |
+
import os
|
| 11 |
+
import base64
|
| 12 |
+
|
| 13 |
+
# Set Streamlit app title and page configuration
|
| 14 |
+
st.set_page_config(page_title="Email Sender and Tracker", layout="wide")
|
| 15 |
+
|
| 16 |
+
# Email Sender Function
|
| 17 |
+
def send_email(server, sender_email, sender_password, receiver_email, subject, email_content):
|
| 18 |
+
message = MIMEMultipart()
|
| 19 |
+
message["From"] = formataddr(("AI Consultant", sender_email))
|
| 20 |
+
message["To"] = receiver_email
|
| 21 |
+
message["Subject"] = subject
|
| 22 |
+
|
| 23 |
+
email_content_formatted = email_content.replace("\n", "<br><br>")
|
| 24 |
+
html_content = f"""
|
| 25 |
+
<html>
|
| 26 |
+
<head>
|
| 27 |
+
<style>
|
| 28 |
+
body {{
|
| 29 |
+
font-family: Arial, sans-serif;
|
| 30 |
+
font-size: 14px;
|
| 31 |
+
line-height: 1.6;
|
| 32 |
+
margin: 0;
|
| 33 |
+
padding: 0;
|
| 34 |
+
}}
|
| 35 |
+
|
| 36 |
+
.email-container {{
|
| 37 |
+
max-width: 600px;
|
| 38 |
+
margin: 0 auto;
|
| 39 |
+
padding: 20px;
|
| 40 |
+
}}
|
| 41 |
+
|
| 42 |
+
.email-header {{
|
| 43 |
+
text-align: center;
|
| 44 |
+
margin-bottom: 20px;
|
| 45 |
+
}}
|
| 46 |
+
|
| 47 |
+
.email-header h2 {{
|
| 48 |
+
color: #333333;
|
| 49 |
+
font-size: 24px;
|
| 50 |
+
font-weight: 700;
|
| 51 |
+
line-height: 1.2;
|
| 52 |
+
margin: 0;
|
| 53 |
+
}}
|
| 54 |
+
|
| 55 |
+
.email-content {{
|
| 56 |
+
background-color: #f9f9f9;
|
| 57 |
+
padding: 20px;
|
| 58 |
+
text-align: justify;
|
| 59 |
+
}}
|
| 60 |
+
|
| 61 |
+
.email-content p {{
|
| 62 |
+
color: #555555;
|
| 63 |
+
font-size: 16px;
|
| 64 |
+
line-height: 1.6;
|
| 65 |
+
margin: 0 0 10px;
|
| 66 |
+
}}
|
| 67 |
+
|
| 68 |
+
.email-content hr {{
|
| 69 |
+
border: none;
|
| 70 |
+
border-top: 1px solid #dddddd;
|
| 71 |
+
margin: 20px 0;
|
| 72 |
+
}}
|
| 73 |
+
|
| 74 |
+
.logo {{
|
| 75 |
+
display: block;
|
| 76 |
+
margin: 0 auto;
|
| 77 |
+
width: 60px;
|
| 78 |
+
}}
|
| 79 |
+
</style>
|
| 80 |
+
</head>
|
| 81 |
+
<body>
|
| 82 |
+
<div class="email-container">
|
| 83 |
+
<div class="email-header">
|
| 84 |
+
<h2>{subject}</h2>
|
| 85 |
+
</div>
|
| 86 |
+
<div class="email-content">
|
| 87 |
+
{email_content_formatted}
|
| 88 |
+
<hr>
|
| 89 |
+
</div>
|
| 90 |
+
<img src="cid:logo_image" alt="Logo" class="logo">
|
| 91 |
+
</div>
|
| 92 |
+
</body>
|
| 93 |
+
</html>
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
# Attach the logo image
|
| 97 |
+
logo_path = "AIConsultantBlackSmall.png"
|
| 98 |
+
with open(logo_path, "rb") as logo_file:
|
| 99 |
+
logo_image = MIMEImage(logo_file.read())
|
| 100 |
+
logo_image.add_header("Content-ID", "<logo_image>")
|
| 101 |
+
message.attach(logo_image)
|
| 102 |
+
|
| 103 |
+
message.attach(MIMEText(html_content, "html"))
|
| 104 |
+
email_message = message.as_string().encode("utf-8")
|
| 105 |
+
try:
|
| 106 |
+
server.sendmail(sender_email, receiver_email, email_message)
|
| 107 |
+
return True
|
| 108 |
+
except smtplib.SMTPException:
|
| 109 |
+
return False
|
| 110 |
+
|
| 111 |
+
# Main Function
|
| 112 |
+
def main():
|
| 113 |
+
st.title("Email Sender and Tracker")
|
| 114 |
+
|
| 115 |
+
# Sidebar Inputs
|
| 116 |
+
sender_email = st.sidebar.text_input("Your Email")
|
| 117 |
+
sender_password = st.sidebar.text_input("Your Email Password", type="password")
|
| 118 |
+
subject = st.sidebar.text_input("Subject")
|
| 119 |
+
email_content = st.sidebar.text_area("Email Content", height=400)
|
| 120 |
+
|
| 121 |
+
# Recipients File Selection
|
| 122 |
+
recipients_file_path = st.sidebar.file_uploader("Select Excel file with recipient emails", type=["xlsx", "xls"])
|
| 123 |
+
|
| 124 |
+
# Send Button
|
| 125 |
+
if st.sidebar.button("Send"):
|
| 126 |
+
if not sender_email or not sender_password or not subject or not email_content:
|
| 127 |
+
st.warning("Incomplete Information. Please fill in all the required fields.")
|
| 128 |
+
elif not recipients_file_path:
|
| 129 |
+
st.warning("Missing Recipients File. Please select an Excel file with recipient emails.")
|
| 130 |
+
else:
|
| 131 |
+
st.info("Sending emails...")
|
| 132 |
+
df = pd.read_excel(recipients_file_path)
|
| 133 |
+
receiver_emails = df.iloc[:, 0].tolist()
|
| 134 |
+
successful_emails = []
|
| 135 |
+
with smtplib.SMTP("smtp.gmail.com", 587) as server:
|
| 136 |
+
server.starttls()
|
| 137 |
+
try:
|
| 138 |
+
server.login(sender_email, sender_password)
|
| 139 |
+
except smtplib.SMTPAuthenticationError:
|
| 140 |
+
st.error("Authentication Failed. Username and password not accepted.")
|
| 141 |
+
return
|
| 142 |
+
|
| 143 |
+
progress_bar = st.progress(0)
|
| 144 |
+
progress_text = st.empty()
|
| 145 |
+
for i, receiver_email in enumerate(receiver_emails[:1000], 1): # Send at most 200 emails from the list
|
| 146 |
+
if send_email(server, sender_email, sender_password, receiver_email, subject, email_content):
|
| 147 |
+
successful_emails.append(receiver_email)
|
| 148 |
+
time.sleep(0.1) # Delay between sending emails
|
| 149 |
+
progress_bar.progress(i / 1000)
|
| 150 |
+
progress_text.text(f"Sending email {i}")
|
| 151 |
+
|
| 152 |
+
st.success("Emails sent successfully!")
|
| 153 |
+
|
| 154 |
+
# Save Successful Emails
|
| 155 |
+
if successful_emails:
|
| 156 |
+
try:
|
| 157 |
+
workbook = openpyxl.Workbook()
|
| 158 |
+
worksheet = workbook.active
|
| 159 |
+
for email in successful_emails:
|
| 160 |
+
worksheet.append([email])
|
| 161 |
+
|
| 162 |
+
# Specify the custom file name
|
| 163 |
+
custom_file_name = "successful_emails.xlsx"
|
| 164 |
+
|
| 165 |
+
excel_file_path = custom_file_name
|
| 166 |
+
workbook.save(excel_file_path)
|
| 167 |
+
workbook.close()
|
| 168 |
+
|
| 169 |
+
# Get the absolute file path
|
| 170 |
+
absolute_file_path = os.path.abspath(excel_file_path)
|
| 171 |
+
|
| 172 |
+
# Rename the file
|
| 173 |
+
new_file_name = "successful_emails.xlsx"
|
| 174 |
+
os.rename(absolute_file_path, new_file_name)
|
| 175 |
+
|
| 176 |
+
st.info(f"Successful emails saved to Excel file: {new_file_name}")
|
| 177 |
+
|
| 178 |
+
# Add button to save or download the Excel file
|
| 179 |
+
download_button_str = create_download_button(new_file_name, new_file_name)
|
| 180 |
+
st.markdown(download_button_str, unsafe_allow_html=True)
|
| 181 |
+
except Exception as e:
|
| 182 |
+
st.error(f"Error saving successful emails: {str(e)}")
|
| 183 |
+
else:
|
| 184 |
+
st.warning("No successful emails to save.")
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
# Helper function to create download button HTML string
|
| 188 |
+
def create_download_button(file_path, file_name):
|
| 189 |
+
button_str = f'<a href="data:application/octet-stream;base64,{get_base64_encoded(file_path)}" download="{file_name}" class="stButton">Download Successful Emails</a>'
|
| 190 |
+
return button_str
|
| 191 |
+
|
| 192 |
+
# Helper function to encode the file to base64
|
| 193 |
+
def get_base64_encoded(file_path):
|
| 194 |
+
with open(file_path, "rb") as file:
|
| 195 |
+
encoded = base64.b64encode(file.read()).decode()
|
| 196 |
+
return encoded
|
| 197 |
+
|
| 198 |
+
# Run the Streamlit app
|
| 199 |
+
if __name__ == "__main__":
|
| 200 |
+
main()
|
logoAIHighdefination.png
ADDED
|