2 The file in charge of managing the emissions of e-mails.
8from email
import encoders
9from email.message
import EmailMessage
10from email.utils
import make_msgid
11from email.mime.base
import MIMEBase
12from display_tty
import Disp, TOML_CONF, FILE_DESCRIPTOR, SAVE_TO_FILE, FILE_NAME
13from .
import constants
as CONST
20 def __init__(self, error: int = 84, success: int = 0, debug: bool =
False) ->
None:
22 The class in charge of allowing the user to send e-mails.
25 error (int, optional): _description_. Defaults to 84.
26 success (int, optional): _description_. Defaults to 0.
27 debug (bool, optional): _description_. Defaults to False.
35 self.
host = CONST.SENDER_HOST
37 self.
port = CONST.SENDER_PORT
46 logger=self.__class__.__name__
49 def _send(self, em: EmailMessage) -> int:
51 Internal method to handle the actual sending of an email.
54 em (EmailMessage): The email message to be sent.
57 int: The status of the email sending operation.
59 context = ssl.create_default_context()
62 with smtplib.SMTP_SSL(self.
host, self.
port, context=context)
as smtp:
65 self.
disp.log_debug(
"Email sent successfully",
"_send")
67 except Exception
as e:
68 self.
disp.log_critical(f
"An error occurred: {e}",
"_send")
71 def send_email(self, receiver: str, subject: str, body: str, body_type: str =
"html") -> int:
73 Sends a simple email to a single receiver.
76 receiver (str): The recipient's email address.
77 subject (str): The subject of the email.
78 body (str): The content of the email.
79 body_type (str, optional): The MIME type of the email content ('html' or 'plain'). Defaults to 'html'.
82 int: The status of the email sending operation.
87 em[
'Subject'] = subject
89 if body_type.lower() ==
"html":
90 em.add_alternative(body, subtype=
'html')
98 Sends an email with one or more attachments.
101 receiver (str): The recipient's email address.
102 subject (str): The subject of the email.
103 body (str): The content of the email.
104 attachments (List[str]): List of file paths for attachments.
105 body_type (str, optional): The MIME type of the email content ('html' or 'plain'). Defaults to 'html'.
108 int: The status of the email sending operation.
113 em[
'Subject'] = subject
115 if body_type ==
"html":
116 em.add_alternative(body, subtype=
'html')
120 for file
in attachments:
122 with open(file,
'rb')
as f:
124 file_name = file.split(
'/')[-1]
126 part = MIMEBase(
'application',
'octet-stream')
127 part.set_payload(file_data)
128 encoders.encode_base64(part)
130 'Content-Disposition',
131 f
'attachment; filename={file_name}'
134 part.get_payload(decode=
True),
135 maintype=
'application',
136 subtype=
'octet-stream',
140 except Exception
as e:
141 self.
disp.log_critical(
142 f
"Error reading attachment {file}: {e}",
143 "send_email_with_attachment"
147 return self.
_send(em)
151 Sends an email to multiple recipients (To, Cc, or Bcc).
154 receivers (List[str]): A list of recipients' email addresses.
155 subject (str): The subject of the email.
156 body (str): The content of the email.
157 body_type (str, optional): The MIME type of the email content ('html' or 'plain'). Defaults to 'html'.
160 int: The status of the email sending operation.
164 em[
'To'] =
', '.join(receivers)
165 em[
'Subject'] = subject
167 if body_type ==
"html":
168 em.add_alternative(body, subtype=
'html')
172 return self.
_send(em)
176 Sends an email with an inline image embedded in the body.
179 receiver (str): The recipient's email address.
180 subject (str): The subject of the email.
181 body (str): The content of the email, including a placeholder for the image.
182 image_path (str): The path to the image to be embedded.
183 body_type (str, optional): The MIME type of the email content ('html' or 'plain'). Defaults to 'html'.
186 int: The status of the email sending operation.
191 em[
'Subject'] = subject
193 if body_type ==
"html":
194 em.add_alternative(body, subtype=
'html')
199 with open(image_path,
'rb')
as img:
200 img_data = img.read()
201 img_cid = make_msgid()[1:-1]
208 em.set_content(body.format(img_cid=img_cid))
209 except Exception
as e:
210 self.
disp.log_critical(
211 f
"Error embedding inline image: {e}",
212 "send_email_with_inline_image"
216 return self.
_send(em)
int send_email_with_inline_image(self, str receiver, str subject, str body, str image_path, str body_type="html")
int send_email(self, str receiver, str subject, str body, str body_type="html")
int _send(self, EmailMessage em)
int send_email_with_attachment(self, str receiver, str subject, str body, List[str] attachments, str body_type="html")
int send_email_to_multiple(self, List[str] receivers, str subject, str body, str body_type="html")
None __init__(self, int error=84, int success=0, bool debug=False)