Skip to content

Commit f4bdfee

Browse files
authored
Merge pull request #2735 from IFRCGo/chore/update-notification
Fix notification config
2 parents 7f96e64 + c093624 commit f4bdfee

1 file changed

Lines changed: 31 additions & 12 deletions

File tree

notifications/notification.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ def run(self):
5656
CronJob.sync_cron(cron_rec)
5757

5858

59-
def construct_msg(subject, html, message_id=None, in_reply_to=None):
59+
def construct_msg(cc_addresses, subject, html, message_id=None, in_reply_to=None):
6060
msg = MIMEMultipart("alternative")
6161

6262
msg["Subject"] = subject
6363
msg["From"] = settings.EMAIL_USER.upper()
6464
msg["To"] = "no-reply@ifrc.org"
65+
if cc_addresses:
66+
msg["Cc"] = ",".join(cc_addresses)
6567

6668
if message_id:
6769
msg["Message-ID"] = message_id
@@ -78,15 +80,29 @@ def construct_msg(subject, html, message_id=None, in_reply_to=None):
7880
return msg
7981

8082

81-
def send_notification(subject, recipients, html, message_id=None, in_reply_to=None, mailtype="", files=None):
83+
def clean_emails(emails):
84+
return [e for e in emails if isinstance(e, str) and e.strip()]
85+
86+
87+
def send_notification(subject, recipients, html, mailtype="", message_id=None, in_reply_to=None, cc_recipients=None, files=None):
8288
"""Generic email sending method, handly only HTML emails currently"""
89+
cc_recipients = cc_recipients or []
8390
if not settings.EMAIL_USER or not settings.EMAIL_API_ENDPOINT:
8491
logger.warning("Cannot send notifications.\n" "No username and/or API endpoint set as environment variables.")
8592
if settings.DEBUG:
8693
print("-" * 22, "EMAIL START", "-" * 22)
8794
print(f"subject={subject}\nrecipients={recipients}\nhtml={html}\nmailtype={mailtype}")
8895
print("-" * 22, "EMAIL END -", "-" * 22)
8996
return
97+
98+
to_addresses = clean_emails(recipients)
99+
cc_addresses = clean_emails(cc_recipients)
100+
addresses = to_addresses + cc_addresses
101+
102+
if not addresses:
103+
logger.info("No valid email addresses provided for sending the notification.")
104+
return
105+
90106
if settings.DEBUG_EMAIL:
91107
print("-" * 22, "EMAIL START", "-" * 22)
92108
print(f"\n{html}\n")
@@ -95,19 +111,18 @@ def send_notification(subject, recipients, html, message_id=None, in_reply_to=No
95111
if settings.FORCE_USE_SMTP:
96112
logger.info("Forcing SMPT usage for sending emails.")
97113
msg = construct_msg(
98-
subject=subject,
99-
html=html,
114+
cc_addresses,
115+
subject,
116+
html,
100117
message_id=message_id,
101118
in_reply_to=in_reply_to,
102119
)
103-
SendMail(recipients, msg).start()
120+
SendMail(addresses, msg).start()
104121
return
105122

106123
if "?" not in settings.EMAIL_API_ENDPOINT: # a.k.a dirty disabling email sending
107124
return
108125

109-
to_addresses = recipients if isinstance(recipients, list) else [recipients]
110-
111126
# if not IS_PROD:
112127
# logger.info('Using test email addresses...')
113128
# to_addresses = []
@@ -127,6 +142,7 @@ def send_notification(subject, recipients, html, message_id=None, in_reply_to=No
127142
# to_addresses.append(eml)
128143

129144
recipients_as_string = ",".join(to_addresses)
145+
cc_recipients_as_string = ",".join(cc_addresses)
130146
if not recipients_as_string:
131147
if len(to_addresses) > 0:
132148
warn_msg = "Recipients failed to be converted to string, 1st rec.: {}".format(to_addresses[0])
@@ -142,7 +158,7 @@ def send_notification(subject, recipients, html, message_id=None, in_reply_to=No
142158
payload = {
143159
"FromAsBase64": str(base64.b64encode(settings.EMAIL_USER.encode("utf-8")), "utf-8"),
144160
"ToAsBase64": str(base64.b64encode(EMAIL_TO.encode("utf-8")), "utf-8"),
145-
"CcAsBase64": "",
161+
"CcAsBase64": str(base64.b64encode(cc_recipients_as_string.encode("utf-8")), "utf-8"),
146162
"BccAsBase64": str(base64.b64encode(recipients_as_string.encode("utf-8")), "utf-8"),
147163
"SubjectAsBase64": str(base64.b64encode(subject.encode("utf-8")), "utf-8"),
148164
"BodyAsBase64": str(base64.b64encode(html.encode("utf-8")), "utf-8"),
@@ -171,7 +187,9 @@ def send_notification(subject, recipients, html, message_id=None, in_reply_to=No
171187
# Saving GUID into a table so that the API can be queried with it to get info about
172188
# if the actual sending has failed or not.
173189
NotificationGUID.objects.create(
174-
api_guid=res_text, email_type=mailtype, to_list=f"To: {EMAIL_TO}; Bcc: {recipients_as_string}"
190+
api_guid=res_text,
191+
email_type=mailtype,
192+
to_list=f"To: {EMAIL_TO}; Cc: {cc_recipients_as_string}; Bcc: {recipients_as_string}",
175193
)
176194

177195
logger.info("E-mails were sent successfully.")
@@ -185,10 +203,11 @@ def send_notification(subject, recipients, html, message_id=None, in_reply_to=No
185203
# Try sending with Python smtplib, if reaching the API fails
186204
logger.warning(f"Authorization/authentication failed ({res.status_code}) to the e-mail sender API.")
187205
msg = construct_msg(
188-
subject=subject,
189-
html=html,
206+
cc_addresses,
207+
subject,
208+
html,
190209
message_id=message_id,
191210
in_reply_to=in_reply_to,
192211
)
193-
SendMail(to_addresses, msg).start()
212+
SendMail(addresses, msg).start()
194213
return res.text

0 commit comments

Comments
 (0)