Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Email message module.""" 

2 

3import time 

4import uuid 

5from email.mime.text import MIMEText 

6 

7 

8class EmailMessage: 

9 """Email message class.""" 

10 

11 def __init__(self, **kwargs): 

12 """Init with defaults.""" 

13 params = {} 

14 for item in kwargs.items(): 

15 params[item[0]] = item[1] 

16 

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", {}) 

28 

29 self.message_id = self.make_key() 

30 

31 def make_key(self): 

32 """Generate unique key.""" 

33 return str(uuid.uuid4()) 

34 

35 def as_string(self): 

36 """Return plaintext content.""" 

37 return self._plaintext() 

38 

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() 

44 

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