import base64 import re from email.header import Header from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from chalicelib.utils import helper, smtp from decouple import config def __get_subject(subject): return subject def __get_html_from_file(source, formatting_variables): if formatting_variables is None: formatting_variables = {} formatting_variables["frontend_url"] = config("SITE_URL") with open(source, "r") as body: BODY_HTML = body.read() if formatting_variables is not None and len(formatting_variables.keys()) > 0: BODY_HTML = re.sub(r"%(?![(])", "%%", BODY_HTML) BODY_HTML = BODY_HTML % {**formatting_variables} return BODY_HTML def __replace_images(HTML): pattern_holder = re.compile(r'') return HTML, mime_img def send_html(BODY_HTML, SUBJECT, recipient, bcc=None): BODY_HTML, mime_img = __replace_images(BODY_HTML) if not isinstance(recipient, list): recipient = [recipient] msg = MIMEMultipart() msg['Subject'] = Header(__get_subject(SUBJECT), 'utf-8') msg['From'] = config("EMAIL_FROM") msg['To'] = "" body = MIMEText(BODY_HTML.encode('utf-8'), 'html', "utf-8") msg.attach(body) for m in mime_img: msg.attach(m) with smtp.SMTPClient() as s: for r in recipient: msg.replace_header("To", r) r = [r] if bcc is not None and len(bcc) > 0: r += [bcc] try: print(f"Email sending to: {r}") s.sendmail(msg['FROM'], r, msg.as_string().encode('ascii')) except Exception as e: print("!!! Email error!") print(e) def send_text(recipients, text, subject): with smtp.SMTPClient() as s: msg = MIMEMultipart() msg['Subject'] = Header(__get_subject(subject), 'utf-8') msg['From'] = config("EMAIL_FROM") msg['To'] = ", ".join(recipients) body = MIMEText(text) msg.attach(body) try: s.sendmail(msg['FROM'], recipients, msg.as_string().encode('ascii')) except Exception as e: print("!! Text-email failed: " + subject), print(e) def __escape_text_html(text): return text.replace("@", "@").replace(".", ".").replace("=", "=")