forked from mominalix/bulk-email-generator-using-ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_sales_email_generator.py
More file actions
108 lines (87 loc) · 4.29 KB
/
bulk_sales_email_generator.py
File metadata and controls
108 lines (87 loc) · 4.29 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import csv
import requests
from bs4 import BeautifulSoup
from openai import OpenAI
client = OpenAI(api_key="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")
# Define your OpenAI API key here ---------------------------------------- Customization required ----------------------------------------
# Function to scrape website data and clean tags
def scrape(url):
try:
if url:
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "lxml")
text = []
# <p> tags
p_tags = []
max_tags = 20
names = soup.find_all("p")
for i, tag in enumerate(names):
if i >= max_tags:
break # Stop after extracting the first 20 tags
if tag.text != '':
name = tag.text
p_tags.append(name)
text += [str(item) for item in p_tags]
# Add additional cleaning process to the scraped text as needed
# Return the cleaned text
return " ".join(text)
return ""
except Exception as e:
print(f"An error occurred while scraping {url}: {str(e)}")
return ""
# Function to generate a customized email using OpenAI
def generate_customized_email(client_name, client_website, scraped_data, sender_name, sender_product_detail, sender_product_name):
try:
# Compose a prompt for ChatGPT
prompt = f'''##Instruction:##
Write a customized email to {client_name} based on the following information:
- Client's website: {client_website}
- Client's website data: {scraped_data}
- Sender's product: {sender_product_detail}
- Sender's product name: {sender_product_name}
- Sender's name: {sender_name}
make sure the email is from sender's product, personalized and relevant to the client's business.'''
# Call the OpenAI API to generate the customized email
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=prompt,
max_tokens=150, # Adjust the max_tokens as needed for your email length
stop=None,
temperature=0.7)
# Extract the generated email text from the API response
generated_email = response.choices[0].text
print(f"Generated email for {client_name}: {generated_email}")
return generated_email
except Exception as e:
print(f"An error occurred while generating the email for {client_name}: {str(e)}")
return ""
# Input and output file paths
input_csv_file = 'client_data.csv'
output_csv_file = 'customized_emails.csv'
# Read client data from the input CSV file
with open(input_csv_file, 'r') as csv_file:
reader = csv.DictReader(csv_file)
rows = list(reader)
# Define sender information ---------------------------------------- Customization required ----------------------------------------
sender_name = "Usman Khan"
sender_product_detail = "Tech consultation, Digital product developments and markeeting."
sender_product_name = "2BTech LLC"
# Create or open the output CSV file for writing
with open(output_csv_file, 'w', newline='') as csv_output_file:
fieldnames = ['Name', 'Email Address', 'Customized Email']
writer = csv.DictWriter(csv_output_file, fieldnames=fieldnames)
writer.writeheader()
# Process each client's data and generate customized emails
for row in rows:
client_name = row['Name']
client_email = row['Email Address']
# Extract website URL from the email address (you may need to modify this logic)
client_website = client_email.split('@')[1]
# Scrape website data and clean tags
scraped_data = scrape("http://" + client_website) # You may need to modify the URL format
# Generate a customized email
customized_email = generate_customized_email(client_name, client_website, scraped_data, sender_name, sender_product_detail, sender_product_name)
# Write the data to the output CSV file
writer.writerow({'Name': client_name, 'Email Address': client_email, 'Customized Email': customized_email})
print(f"Customized emails have been generated and saved to {output_csv_file}.")