-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·74 lines (58 loc) · 2.08 KB
/
main.py
File metadata and controls
executable file
·74 lines (58 loc) · 2.08 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
import os
import csv
import ssl
import certifi
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from dotenv import load
import smtplib
from smtplib import SMTP, SMTPAuthenticationError, SMTPException
os.environ["SSL_CERT_FILE"] = certifi.where()
# Load environment variables from .env file
load()
# Accessing environment variables
SMTP_SERVER = os.getenv("SMTP_SERVER")
SMTP_PORT = os.getenv("SMTP_PORT")
EMAIL = os.getenv("EMAIL")
PASSWORD = os.getenv("PASSWORD")
SENDER_EMAIL = "djdiptayan1@gmail.com"
def read_template(filename):
with open(filename, "r", encoding="utf-8") as file:
template = file.read()
return template
def send_email(receiver_email, recipient_name, subject, message):
personalized_message = message.replace("[Recipient's Name]", recipient_name)
msg = MIMEMultipart()
msg["From"] = SENDER_EMAIL
msg["To"] = receiver_email
msg["Subject"] = subject
msg.attach(MIMEText(personalized_message, "html"))
# Create an SSL context
context = ssl.create_default_context()
try:
# Connect to the SMTP server using STARTTLS
with SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls(context=context) # Secure the connection
server.login(EMAIL, PASSWORD)
server.send_message(msg)
except SMTPAuthenticationError:
print(
"Failed to authenticate with the SMTP server. Check your username/password."
)
except SMTPException as e:
print(f"An SMTP error occurred: {e}")
except Exception as e:
print(f"An error occurred: {e}")
def main():
# Read HTML template
html_template = read_template("template.html")
csv_file = "test.csv"
with open(csv_file, "r", encoding="utf-8") as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
recipient_name = row["Name"]
receiver_email = row["Email"]
subject = "Your Email Subject Here"
send_email(receiver_email, recipient_name, subject, html_template)
if __name__ == "__main__":
main()