-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.py
57 lines (46 loc) · 1.63 KB
/
mail.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
import smtplib
import email.utils
from email.mime.text import MIMEText
from .mailconfig import *
class R3Mail():
def send(
self,
subject,
text,
to_emails,
from_name='r3bot',
from_email='r3bot@more.systems'):
msg = MIMEText(text)
msg.set_unixfrom('author')
msg['To'] = email.utils.formataddr(
('HungryHacker', 'devnull@realraum.at'))
msg['From'] = email.utils.formataddr((from_name, from_email))
msg['Subject'] = subject
server = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
try:
# server.set_debuglevel(True)
# identify ourselves, prompting server for supported features
server.ehlo()
# check for STARTTLS
if server.has_extn('STARTTLS'):
print("## STARTTLS! :)")
server.starttls()
server.ehlo() # re-identify ourselves over TLS connection
else:
print("## no STARTTLS!? fail.")
exit()
# print("logging in with", SMTP_USER, ':', SMTP_PASSWORD)
# Pretend the SMTP server supports some forms of authentication.
server.esmtp_features['auth'] = 'PLAIN'
server.login(SMTP_USER, SMTP_PASSWORD)
server.sendmail(from_email, to_emails, msg.as_string())
finally:
server.quit()
if __name__ == '__main__':
mail = R3Mail()
print('sending testmail ...')
mail.send(
'Test Mail',
'This is a test mail from r3bot\'s mail client!',
['dev+r3bot@2904.cc'])
print('done!')