""" AUTHOR: Khushal P Soonderji DATE: Tuesday, 16th Jul, 2024 OBJECTIVE: To be able to send out mails from code. REFERENCES: N/A DOWNLOADS: N/A """ # ***************************************************************************************************************** # ***** **** # *** IMPORT *** # ***** **** # ***************************************************************************************************************** # To make sibling directories accessible for imports: import sys sys.path.append(".") sys.path.append("..") # For working with mails: import aiosmtplib from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders # My utils: from utils import rate_limit_utils # Common: from shared.statuses import StatusCodes # For random strings: import string import random # For system-level activities: import os # For working with files in RAM: import io # For debugging: from icecream import IceCreamDebugger # ***************************************************************************************************************** # ***** **** # *** MACROS / ONE-TIME INIT *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** VARIABLES *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** FUNCTIONS *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** CLASSES *** # ***** **** # ***************************************************************************************************************** class MailMessage: def __init__(self, to_email, subject): """ Create an instance of the message that you would like to send. :param to_email: The EMail ID of th recipient. :param subject: The subject of the mail. """ self.message = MIMEMultipart() self.message["To"] = to_email self.message["Subject"] = subject def add_text(self, text): """ Add plain-text to the mail body. :param text: The text to add to the mail body. :return: None. """ self.message.attach(MIMEText(text, "plain")) def add_html(self, html_text): """ Add HTML text to the mail body. :param html_text: The HTML text to add to the mail body. :return: None. """ self.message.attach(MIMEText(html_text, "html")) def add_inline_image(self, image_file, content_id = None): """ Add an inline image to the body of the mail. NOTE: This is NOT the same as sending an image as an attachment. :param image_file: The image data to attach to the mail body. :param content_id: Inline images are inserted via HTML bocks. This field identifies the image resource. If not specified, I will generate a random string. You may write a custom value here if you know what you are doing. For most use cases, please ignore this field. :return: None. """ # Read the image as bytes: image_bytes = None if type(image_file) is str: with open(image_file, "rb") as opened_image_file: image_bytes = opened_image_file.read() if type(image_file) is io.BytesIO: image_file.seek(0) image_bytes = image_file.getvalue() # Declare the part to be attached to the multipart message: if image_bytes is not None: # Create the HTML block if the image pointer is blank: if content_id is None: content_id = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8)) self.add_html(f"""