-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
49 lines (43 loc) · 1.56 KB
/
utils.py
File metadata and controls
49 lines (43 loc) · 1.56 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
import pytz
from datetime import datetime
from O365 import Account
import json
import smtplib
from email.mime.text import MIMEText
with open('config.json') as f:
config = json.load(f)
TZ = pytz.timezone(config['TIMEZONE'])
def now_arg():
return datetime.now(TZ)
def today_str():
return now_arg().strftime('%Y-%m-%d')
def is_weekday():
return now_arg().weekday() < 5
def send_mail(subject, body):
sender = config['EXCHANGE_SENDER']
recipients = [r.strip() for r in config['EXCHANGE_RECIPIENT'].split(',')]
if sender.endswith('@gmail.com'):
# Enviar por Gmail SMTP
password = config.get('GMAIL_APP_PASSWORD', None)
if not password:
raise Exception('Falta GMAIL_APP_PASSWORD en config.json')
msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ', '.join(recipients)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender, password)
server.sendmail(sender, recipients, msg.as_string())
else:
# Enviar por Microsoft Exchange (O365)
credentials = (config['EXCHANGE_CLIENT_ID'], config['EXCHANGE_CLIENT_SECRET'])
account = Account(credentials, tenant_id=config['EXCHANGE_TENANT_ID'])
if not account.is_authenticated:
account.authenticate(scopes=['basic', 'message_all'])
m = account.new_message()
for r in recipients:
m.to.add(r)
m.subject = subject
m.body = body
m.sender.address = sender
m.send()