-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrequest_utils.py
More file actions
60 lines (47 loc) · 1.84 KB
/
request_utils.py
File metadata and controls
60 lines (47 loc) · 1.84 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import buu_config, buu_database
import utils
import io
class request_utils(object):
def __init__(self):
self.config = buu_config.config
def send_email(self, file_name, printer):
destination = [printer.printer_value]
text_subtype = 'plain'
content="""\
Test message
"""
subject="打印"
from os.path import basename
from smtplib import SMTP_SSL as SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
try:
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = self.config.smtp_sender
msg['To'] = COMMASPACE.join(destination)
msg['Date'] = formatdate(localtime=True)
msg.attach(MIMEText(content))
with open(file_name, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name = basename(file_name).decode('utf-8')
)
# After the file is closed
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(file_name).decode('utf-8')
print(part['Content-Disposition'])
msg.attach(part)
conn = SMTP(self.config.smtp_server)
conn.set_debuglevel(False)
conn.login(self.config.smtp_username, self.config.smtp_password)
try:
conn.sendmail(self.config.smtp_sender, destination, msg.as_string())
finally:
conn.quit()
return True
except Exception:
import traceback
traceback.print_exc()
return False