-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsend_mail.py
More file actions
196 lines (169 loc) · 7.63 KB
/
send_mail.py
File metadata and controls
196 lines (169 loc) · 7.63 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
from email import encoders
from email.mime.base import MIMEBase
from email.utils import formataddr
import json
import os
from dotenv import load_dotenv
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import date
from db import get_some_thing
from email_template import render_email_template
load_dotenv()
sender_email = os.environ.get("SENDER_EMAIL")
receiver_email = "wasscodeur228@gmail.com"
password = os.environ.get("SENDER_EMAIL_PASSWORD")
smtp_server = os.environ.get("SMTP_SERVER")
smtp_server_port = os.environ.get("SMTP_SERVER_PORT")
def send_email_with_or_without_attachment(body, subject, sender_email=sender_email, receiver_email=receiver_email, password=password, smtp_server=smtp_server, smtp_server_port=smtp_server_port, filename=None):
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = formataddr(('PyCon Togo Team', sender_email))
msg['To'] = receiver_email
msg.attach(MIMEText(body, 'html'))
# Attachment
if filename:
with open(filename, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename={os.path.basename(filename)}')
msg.attach(part)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, smtp_server_port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
def send_welcome_email(first_name="Pythonista", participant_email=None):
"""
Sends a welcome email to the participant.
Args:
first_name (str): The first name of the participant.
"""
from welcome import welcome_msg
email_msgs = welcome_msg(first_name)
for email in email_msgs:
body = render_email_template(
first_name=first_name,
message=email['body']
)
subject = email['subject']
send_email_with_or_without_attachment(body, subject, receiver_email=participant_email)
def send_daily_email(first_name, day_number, fr_title, en_title, fr_link, en_link, participant_email=None):
"""
Sends the daily challenge email to the participant.
"""
from welcome import mail_day_bilingual, mail_day_23 # daily_mail_with_task # daily_task_mail_after_live # get_bilingual_challenge_email , new_new_new_mail_daily_mail #new_daily_challenge_email# , get_daily_challenge_email
# subject, body = get_bilingual_challenge_email(first_name, day_number, fr_title, en_title, fr_link, en_link)
#subject, body = new_daily_challenge_email(first_name, day_number, fr_title, en_title, fr_link, en_link)
subject, body = mail_day_bilingual(
participant=first_name,
day_number=day_number,
fr_title=fr_title,
en_title=en_title,
fr_link=fr_link,
en_link=en_link
)
# subject, body = get_daily_challenge_email(first_name, day_number, fr_title, en_title, fr_link, en_link)
# email_msgs = get_daily_challenge_email(first_name, day_number, fr_title, en_title, fr_link, en_link)
# for email in email_msgs:
# body = render_email_template(
# first_name=first_name,
# message=email['body']
# )
# subject = email['subject']
body = render_email_template(
first_name=first_name,
message=body
)
send_email_with_or_without_attachment(body, subject, receiver_email=participant_email)
def send_pre_challenge_info_email(first_name, participant_email=None):
"""
Sends the pre-challenge information email to the participant.
"""
from welcome import get_pre_challenge_info_email
subject, body = get_pre_challenge_info_email(first_name)
body = render_email_template(
first_name=first_name,
message=body
)
send_email_with_or_without_attachment(body, subject, receiver_email=participant_email)
def submission_instruction_email(first_name, fr_title, en_title, fr_link, en_link, participant_email=None):
"""
Sends the submission instruction email to the participant.
"""
from welcome import get_submission_instruction_email
subject, body = get_submission_instruction_email(first_name, fr_title, en_title, fr_link, en_link)
body = render_email_template(
first_name=first_name,
message=body
)
send_email_with_or_without_attachment(body, subject, receiver_email=participant_email)
if __name__ == "__main__":
already_sent_day_1 = emails_sent_successfully = [
]
failed_emails = []
today = date.today()
day_number = (today - date(2025, 7, 23)).days + 1
index = day_number - 1
participants = get_some_thing("participants")
if not participants:
print("No participants found.")
exit(1)
with open("templates/tasks.json", "r") as file:
tasks = json.load(file)
if 0 <= index < len(tasks):
task = tasks[index]
first_name = "Pythonista"
fr_title = task["title_fr"]
en_title = task["title_en"]
fr_link = task.get("link_fr")
en_link = task.get("link_en")
for participant in participants:
participant_email = participant["email"]
first_name = participant["full_name"]
if not participant_email:
print(f"No email found for participant {participant['full_name']}. Skipping.")
continue
print(f"Sending daily email for day {day_number} to {first_name} at {participant_email}... ")
if participant_email in already_sent_day_1:
print(f"Email already sent to {participant_email} for day {day_number}. Skipping.")
else:
try:
#submission_instruction_email(
# first_name=first_name,
# fr_title=fr_title,
# en_title=en_title,
# fr_link=fr_link,
# en_link=en_link,
# participant_email=participant_email
#)
send_daily_email(
first_name=first_name,
day_number=day_number,
fr_title=fr_title,
en_title=en_title,
fr_link=fr_link,
en_link=en_link,
participant_email=participant_email
)
already_sent_day_1.append(participant_email)
print(f"Email sent to {participant_email} for day {day_number}.")
except Exception as e:
print(f"Failed to send email to {participant_email} for day {day_number}: {e}")
failed_emails.append({"email": participant_email, "reason": str(e)})
continue
len_already_sent_day_1 = len(already_sent_day_1)
print(f"Already sent emails for day 1: {len_already_sent_day_1}")
len_failed_emails = len(failed_emails)
print(f"Failed to send emails: {len_failed_emails}")
if failed_emails:
print(f"Failed to send emails to {len(failed_emails)} participants:")
for failed in failed_emails:
print(f"Email: {failed['email']}, Reason: {failed['reason']}")
else:
print("All emails sent successfully.")
else:
print(f"Invalid day number: {day_number}. No task found for this day.")