-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdelivery.py
81 lines (67 loc) · 2.07 KB
/
delivery.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
"""
Text and email functions to be called by other modules.
"""
# Non-local imports
import nexmo
# Local imports
import keys
import smtplib # emails
import ssl # emails
# Project modules
from config import Config
# Instantiate Nexmo client
nexmo_client = nexmo.Client(
key = keys.Nexmo.api_key,
secret = keys.Nexmo.api_secret
)
sms = nexmo.Sms(client = nexmo_client)
def text_me(*messages: str):
"""
Concatenates all given arguments. Doesn't add spaces between them.
Sends the message using the Nexmo account and user phone number
given in the _keys.py module.
Returns True if the first (usually only) message sent had successful
delivery. Otherwise, returns False.
"""
# Create the message
text_content = 'Message from Allocator: '
for item in messages:
text_content += item
sms.send_message(
{
"from": keys.Nexmo.sender,
"to": keys.User.phone_number,
"text": text_content
}
)
def email_me(*messages: str, subject: str = "Message From Allocator") -> None:
"""
Basic email framework is taken care of, including signature.
Concatenates all args, like `text_me.`
`messages` can be generic text, or whatever is passed to `text_me` function.
"""
# Unpack messages into a string
messages = "".join(messages)
content = (
f"From: Allocator <{keys.Gmail.email_address}>\n"
f"To: {keys.User.name.title()} <{keys.User.email_address}>\n"
f"Subject: {subject} \n\n"
f"Dear {keys.User.name.title()},\n\n"
f"{messages} \n\n"
"-Allocator"
)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(
host = keys.Gmail.smtp_host,
port = keys.Gmail.smtp_port,
context = context
) as server:
server.login(
user = keys.Gmail.email_address,
password = keys.Gmail.password
)
server.sendmail(
from_addr = keys.Gmail.email_address,
to_addrs = Config.email_recipients,
msg = content
)