Coverage for src/email_message.py: 100%
33 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 19:45 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 19:45 +0000
1"""Email message module."""
3import time
4import uuid
5from email.mime.text import MIMEText
8class EmailMessage:
9 """Email message class."""
11 def __init__(self, **kwargs):
12 """Init with defaults."""
13 params = {}
14 for item in kwargs.items():
15 params[item[0]] = item[1]
17 self.to = params.get("to")
18 self.rto = params.get("rto")
19 self.cc = params.get("cc")
20 self.bcc = params.get("bcc")
21 self.sender = params.get("from")
22 self.subject = params.get("subject", "")
23 self.body = params.get("body")
24 self.html = params.get("html")
25 self.date = params.get("date", time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime()))
26 self.charset = params.get("charset", "us-ascii")
27 self.headers = params.get("headers", {})
29 self.message_id = self.make_key()
31 def make_key(self):
32 """Generate unique key."""
33 return str(uuid.uuid4())
35 def as_string(self):
36 """Return plaintext content."""
37 return self._plaintext()
39 def _plaintext(self):
40 """Create plaintext content."""
41 msg = MIMEText(self.body, "plain", self.charset)
42 self._set_info(msg)
43 return msg.as_string()
45 def _set_info(self, msg):
46 """Set email information."""
47 msg["Subject"] = self.subject
48 msg["From"] = self.sender
49 msg["To"] = self.to
50 msg["Date"] = self.date