File size: 2,676 Bytes
5b779b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6bd0df
5b779b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import streamlit as st
import json
import schedule
import time
from datetime import datetime

# Function to Generate Email Content
def generate_email(template, customer, product):
    try:
        email_content = template.format(
            name=customer['name'],
            product_name=product['name'],
            features=product['features'],
            benefits=product['benefits']
        )
    except KeyError as e:
        st.error(f"KeyError: {e} in template: {template}")
        raise
    return email_content

# Function to Send Email (for demonstration purposes, it just prints the email)
def send_email(email):
    st.write(f"Subject: {email['subject']}")
    st.write(email['content'])
    st.write("\n---\n")

# Streamlit UI
st.title(" Cold Email Sequence Generator")

uploaded_file = st.file_uploader("Upload a JSON structured like the 'example.json' in the files section. Once 'Generated Emails' are okay, press 'Start Email Scheduling' button at the bottom to start sending each of the generated emails every 10 seconds", type="json")

if uploaded_file is not None:
    # Parse JSON Input
    data = json.load(uploaded_file)
    
    customer = data['customer']
    product = data['product']
    sequence = data['sequence']
    
    emails = []

    # Generate Emails Based on the Sequence
    for email in sequence:
        email_content = generate_email(email['template'], customer, product)
        emails.append({
            "day": email['day'],
            "subject": email['subject'],
            "content": email_content
        })
    
    # Display Generated Emails
    st.subheader("Generated Emails")
    for email in emails:
        send_email(email)
    
    # Schedule Emails (for demonstration purposes, it just prints the emails)
    start_time = datetime.now()
    emails_sent = set()
    emails_sent_count = 0
    max_emails_to_send = len(emails)

    def get_email_identifier(email):
        return (email['subject'], email['content'])

    def schedule_email(e=email):
        global emails_sent_count
        if emails_sent_count < max_emails_to_send:
            if get_email_identifier(e) not in emails_sent:
                send_email(e)
                emails_sent.add(get_email_identifier(e))
                emails_sent_count += 1
                if emails_sent_count >= max_emails_to_send:
                    return schedule.CancelJob

    if st.button("Start Email Scheduling"):
        for i, email in enumerate(emails):
            schedule.every(10 * (i + 1)).seconds.do(schedule_email, e=email)
        
        while emails_sent_count < max_emails_to_send:
            schedule.run_pending()
            time.sleep(1)