-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.py
92 lines (78 loc) · 2.53 KB
/
send.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /usr/bin/env python3
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import pyqrcode
import smtplib
def email(code, recipient, config):
# Create message container.
msg = MIMEMultipart('related')
msg['Subject'] = 'QR Code per la registrazione al laboratorio'
msg['From'] = config["SMTP"]["sender"]
msg['To'] = recipient
html = """\
<html>
<head></head>
<body>
<p>
<h3>
Mostra questo qr-code ai responsabili di laboratorio per
registrarti.
</h3>
</p>
<p>
<img src="cid:qrcode">
</p>
Ricordati di compilare il Google form con i dati del tuo gruppo,
sono un membro del gruppo deve compilarlo, con i dati di tutti e tre
i componenti.
Il form lo trovate al seguente url: <a href="https://goo.gl/forms/HSIQ9z6jpsgQIoVz1">https://goo.gl/forms/HSIQ9z6jpsgQIoVz1</a>
</body>
</html>
"""
msgHtml = MIMEText(html, 'html')
msgImg = MIMEImage(create_qrcode(code), name="qrcode.png")
msgImg.add_header('Content-ID', '<qrcode>')
msgImg.add_header('Content-Disposition',
'attachment',
filename="qrcode.png")
msgImg.add_header('Content-Disposition',
'inline',
filename="qrcode.png")
msg.attach(msgHtml)
msg.attach(msgImg)
while True:
try:
s = smtplib.SMTP(config["SMTP"]["server"])
s.starttls()
s.login(config["SMTP"]["login"], config["SMTP"]["password"])
s.send_message(msg)
s.quit()
break
except Exception as e:
print(str(e)+"...retrying!")
continue
def message(subject, text, recipient, config):
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = config["SMTP"]["sender"]
msg['To'] = recipient
while True:
try:
s = smtplib.SMTP(config["SMTP"]["server"])
s.starttls()
s.login(config["SMTP"]["login"], config["SMTP"]["password"])
s.send_message(msg)
s.quit()
break
except Exception as e:
print(str(e)+"...retrying!")
continue
def create_qrcode(data):
qr = pyqrcode.create(data)
qr.png("code.png",
scale=12,
module_color=[0x00, 0x00, 0x00],
background=[0xff, 0xff, 0xff])
with open("code.png", "rb") as f:
return f.read()