-
Notifications
You must be signed in to change notification settings - Fork 2
/
mail.py
71 lines (63 loc) · 2.1 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""
Module to manage sending emails
"""
from __future__ import print_function
from os import environ as env
from datetime import datetime
import requests
from scrapers import internal_scraper, public_scraper
from scrapers.settings import load_env
load_env()
REQUESTS_SESSION = requests.Session()
def get_attachment(attachment_url):
"""
Return PDF given attachment URL
"""
response = REQUESTS_SESSION.get(attachment_url)
return response.content
def get_new_notices():
"""
Fetch new notices from internal & public noticeboards
"""
new_notices = internal_scraper.scrape_internal()
new_notices['public'] = public_scraper.scrape_public()
return new_notices
def send_mail():
"""
Method to send mail
"""
print(
"""
**************************************
%s
**************************************
""" % datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
)
target_emails = [target.strip() for target in env['TARGET_EMAIL'].split(',')]
new_notices = get_new_notices()
mailgun_base_url = 'https://api.mailgun.net/v3/%s' % env['MAILGUN_DOMAIN']
data = {
'from': (None, 'Hermes <no-reply@%s>' % env['MAILGUN_DOMAIN']),
'fromname': (None, 'Hermes')
}
for section in new_notices:
for notice in new_notices[section]:
files = []
data['subject'] = (None, notice['title'])
data['html'] = (None, notice['html'])
if 'attachment' in notice:
attachment_name = notice['attachment'].split('/')[-1]
files = [('attachment', (attachment_name, get_attachment(notice['attachment'])))]
print(data)
for target in target_emails:
data['to'] = (None, target)
response = REQUESTS_SESSION.post(
mailgun_base_url + '/messages',
data=data,
auth=('api', env['MAILGUN_API_KEY']),
files=files
)
print(response.text)
if __name__ == "__main__":
send_mail()