-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailer.py
More file actions
296 lines (233 loc) · 9.33 KB
/
emailer.py
File metadata and controls
296 lines (233 loc) · 9.33 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
"""
Email Sender - Send personalized emails to Product Hunt launches
Uses Resend API to send emails based on CSV data and template
"""
import csv
import os
import sys
import time
import argparse
from datetime import datetime
from pathlib import Path
try:
import resend
except ImportError:
print("❌ Please install resend: pip3 install resend")
sys.exit(1)
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
print("❌ Please install python-dotenv: pip3 install python-dotenv")
sys.exit(1)
# Configuration from .env
RESEND_API_KEY = os.getenv("RESEND_API_KEY", "")
FROM_EMAIL = os.getenv("FROM_EMAIL", "")
TEST_EMAIL_DEFAULT = os.getenv("TEST_EMAIL", "")
TEMPLATE_FILE = "template.txt"
if not RESEND_API_KEY or not FROM_EMAIL:
print("❌ Missing RESEND_API_KEY or FROM_EMAIL in .env file")
print(" Copy .env.example to .env and fill in your credentials")
sys.exit(1)
def validate_test_email(test_email: str) -> bool:
"""Validate that test email is configured when running in test mode"""
if not test_email:
print("❌ TEST_EMAIL not configured")
print(" Set TEST_EMAIL in .env or use --test-email flag")
return False
return True
def load_template(template_path: str) -> str:
"""Load email template from file"""
with open(template_path, 'r', encoding='utf-8') as f:
return f.read()
def extract_first_name(maker_name: str) -> str:
"""Extract first name from maker name"""
if not maker_name:
return "there" # Fallback if no name
# Split by space and take first part
parts = maker_name.strip().split()
if parts:
return parts[0]
return "there"
def personalize_email(template: str, product: dict) -> tuple[str, str]:
"""
Replace placeholders in template with actual values
Returns (subject, body)
"""
first_name = extract_first_name(product.get('maker_name', ''))
product_name = product.get('name', 'your product')
launch_platform = product.get('source', 'Product Hunt')
# Map source to friendly name
platform_names = {
'producthunt': 'Product Hunt',
'hackernews': 'Hacker News',
'indiehackers': 'Indie Hackers'
}
launch_platform = platform_names.get(launch_platform.lower(), launch_platform)
# Replace placeholders in body
body = template.replace('{{FirstName}}', first_name)
body = body.replace('{{ProductName}}', product_name)
body = body.replace('{{LaunchPlatform}}', launch_platform)
# Create subject
subject = f"Congrats on launching {product_name}!"
return subject, body
def send_email(to_email: str, subject: str, body: str, test_mode: bool = False, test_email: str = None) -> tuple[bool, str]:
"""
Send email using Resend API
Returns (success, message)
"""
resend.api_key = RESEND_API_KEY
# In test mode, send to test email instead
actual_recipient = test_email if test_mode else to_email
try:
params = {
"from": FROM_EMAIL,
"to": [actual_recipient],
"subject": subject,
"text": body,
}
response = resend.Emails.send(params)
if response and response.get('id'):
return True, f"Sent (ID: {response['id']})"
else:
return False, f"Failed: {response}"
except Exception as e:
return False, f"Error: {str(e)}"
def process_csv(csv_path: str, limit: int = None, test_mode: bool = False, test_email: str = None):
"""
Process CSV file and send emails
Args:
csv_path: Path to CSV file
limit: Maximum number of emails to send
test_mode: If True, send all emails to test_email instead
test_email: Email address for testing
"""
# Validate test email in test mode
actual_test_email = test_email or TEST_EMAIL_DEFAULT
if test_mode and not validate_test_email(actual_test_email):
sys.exit(1)
# Load template from script directory
script_dir = Path(__file__).parent
template_path = script_dir / TEMPLATE_FILE
if not template_path.exists():
print(f"❌ Template file not found: {template_path}")
sys.exit(1)
template = load_template(str(template_path))
print(f"✅ Loaded template from {template_path}")
# Read CSV
rows = []
fieldnames = []
with open(csv_path, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
fieldnames = reader.fieldnames or []
rows = list(reader)
print(f"📊 Found {len(rows)} products in CSV")
# Add email_sent and email_sent_at columns if not present
if 'email_sent' not in fieldnames:
fieldnames.append('email_sent')
if 'email_sent_at' not in fieldnames:
fieldnames.append('email_sent_at')
# Filter products with valid emails that haven't been sent yet
to_send = []
for i, row in enumerate(rows):
email = row.get('email', '').strip()
email_sent = row.get('email_sent', '').strip().lower()
# Skip if no email or already sent
if not email:
continue
if email_sent == 'sent':
continue
# Skip placeholder/invalid emails
invalid_patterns = ['example.com', 'your@', 'you@', 'noreply@', 'no-reply@',
'test@', 'demo@', '.webp', 'footer.email']
if any(p in email.lower() for p in invalid_patterns):
rows[i]['email_sent'] = 'skipped'
rows[i]['email_sent_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
continue
to_send.append((i, row))
print(f"📧 Found {len(to_send)} products with valid emails to send")
if limit and limit < len(to_send):
print(f"⚠️ Limiting to first {limit} emails")
to_send = to_send[:limit]
if test_mode:
print(f"🧪 TEST MODE: All emails will be sent to {actual_test_email}")
# Send emails
sent_count = 0
failed_count = 0
for idx, (row_index, product) in enumerate(to_send):
email = product.get('email', '')
name = product.get('name', 'Unknown')
maker = product.get('maker_name', 'Unknown')
print(f"\n[{idx + 1}/{len(to_send)}] {name}")
print(f" Maker: {maker}")
print(f" Email: {email}" + (f" → {actual_test_email}" if test_mode else ""))
# Personalize email
subject, body = personalize_email(template, product)
# Send email
success, message = send_email(email, subject, body, test_mode, actual_test_email)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if success:
print(f" ✅ {message}")
rows[row_index]['email_sent'] = 'sent'
rows[row_index]['email_sent_at'] = timestamp
sent_count += 1
else:
print(f" ❌ {message}")
rows[row_index]['email_sent'] = 'failed'
rows[row_index]['email_sent_at'] = timestamp
failed_count += 1
# Save CSV after each email to prevent duplicates if interrupted
with open(csv_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
for field in fieldnames:
if field not in row:
row[field] = ''
writer.writerow(row)
# Add delay between emails (except for the last one)
if idx < len(to_send) - 1:
print(f" ⏳ Waiting 5 seconds...")
time.sleep(5)
print(f"\n{'=' * 50}")
print(f"✅ Done!")
print(f" 📤 Sent: {sent_count}")
print(f" ❌ Failed: {failed_count}")
print(f" 📝 CSV updated: {csv_path}")
def main():
parser = argparse.ArgumentParser(
description='Send personalized emails to Product Hunt launches',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Test mode - send 2 emails to your test email
python3 emailer.py launches-2026-01-08.csv 2 --test
# Production - send 5 emails to actual recipients
python3 emailer.py launches-2026-01-08.csv 5
# Send all emails (no limit)
python3 emailer.py launches-2026-01-08.csv
"""
)
parser.add_argument('csv_file', help='Path to CSV file with product launches')
parser.add_argument('limit', nargs='?', type=int, default=None,
help='Maximum number of emails to send (optional)')
parser.add_argument('--test', action='store_true',
help='Test mode - send all emails to test email instead')
parser.add_argument('--test-email', default=TEST_EMAIL_DEFAULT,
help=f'Email address for test mode (default: {TEST_EMAIL_DEFAULT})')
args = parser.parse_args()
# Validate CSV file exists
if not Path(args.csv_file).exists():
print(f"❌ CSV file not found: {args.csv_file}")
sys.exit(1)
print("\n📧 Product Launch Emailer")
print("=" * 50)
process_csv(
csv_path=args.csv_file,
limit=args.limit,
test_mode=args.test,
test_email=args.test_email
)
if __name__ == "__main__":
main()