Skip to content

Commit bd3ef12

Browse files
committed
Added script for sending interest form emails
1 parent a1c6f88 commit bd3ef12

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

scripts/interest_form_emails.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import sys
4+
import csv
5+
import time
6+
from sendgrid import SendGridAPIClient
7+
from sendgrid.helpers.mail import Mail
8+
9+
FROM_EMAIL = "contact@mchacks.ca"
10+
TEMPLATE_ID = "d-8cbb9f9e07f04712bc88aecba33d49f3"
11+
12+
13+
def send_email(to_email):
14+
15+
message = Mail(
16+
from_email=FROM_EMAIL,
17+
to_emails=to_email,
18+
)
19+
message.template_id = TEMPLATE_ID
20+
21+
try:
22+
sg = SendGridAPIClient(os.getenv("SENDGRID_API_KEY"))
23+
response = sg.send(message)
24+
return True
25+
except Exception as e:
26+
print(f"Error sending to {to_email}: {e}")
27+
return False
28+
29+
30+
def parse_csv(csv_filename):
31+
if not os.path.exists(csv_filename):
32+
print(f"Error: File '{csv_filename}' not found")
33+
return
34+
35+
sent_count = 0
36+
failed_count = 0
37+
38+
try:
39+
with open(csv_filename, "r", newline="", encoding="utf-8") as csvfile:
40+
reader = csv.DictReader(csvfile)
41+
42+
for _, row in enumerate(reader, start=1):
43+
email = row.get("Email", "").strip()
44+
45+
if not email or "@" not in email:
46+
continue
47+
48+
if send_email(email):
49+
sent_count += 1
50+
else:
51+
failed_count += 1
52+
53+
time.sleep(0.1) # Add delay to avoid rate limiting
54+
55+
print(f"\nSent: {sent_count} | Failed: {failed_count}")
56+
57+
except Exception as e:
58+
print(f"Error: {e}")
59+
60+
61+
if __name__ == "__main__":
62+
parse_csv(sys.argv[1])

0 commit comments

Comments
 (0)