-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail_sender.py
More file actions
37 lines (28 loc) · 1.24 KB
/
mail_sender.py
File metadata and controls
37 lines (28 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import email.utils
import logging
import smtplib
from datetime import timedelta
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class MailSender:
def __init__(self, mailconfig: dict, log: logging.Logger):
self.mailconfig = mailconfig
self.log = log
def send_finish_info(self, printer: str, file: str, print_time: int) -> None:
"""
Sends a mail to the recipient specified in the config.json file
"""
msg = MIMEMultipart()
msg['From'] = self.mailconfig['sender']
msg['To'] = self.mailconfig['recipient']
msg['Subject'] = 'Print finished: ' + file
msg['Message-ID'] = email.utils.make_msgid()
msg['Date'] = email.utils.formatdate(localtime=True)
body = 'Print ' + file + ' finished on ' + printer + " (print time: " + str(timedelta(seconds=print_time)) + ")."
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP(self.mailconfig['host'], self.mailconfig['port'])
server.ehlo()
server.starttls()
server.ehlo()
server.login(self.mailconfig['user'], self.mailconfig['password'])
server.sendmail(self.mailconfig['sender'], self.mailconfig['recipient'], msg.as_string())