-
Notifications
You must be signed in to change notification settings - Fork 4
/
sendgrid_api.py
64 lines (49 loc) · 1.77 KB
/
sendgrid_api.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
import json
import logging
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from oanda import get_datetime_now
def get_credentials():
loc = "sendgrid_api.py:get_credentials"
try:
with open("credentials.json") as credentials_json:
credentials = (json.load(credentials_json))["sendgrid"]
except:
logging.exception("{}: Could not read credentials from credentials.json"
.format(loc))
raise
return credentials
def send_mail(subject, message):
# https://github.com/sendgrid/sendgrid-python
loc = "sendgrid_api.py:send_mail"
credentials = get_credentials()
mail_to_send = Mail(
from_email=credentials["email_address"],
to_emails=credentials["email_address"],
subject=subject,
plain_text_content=message,
html_content=None)
try:
sendgrid_client = SendGridAPIClient(credentials["api_key"])
response = sendgrid_client.send(mail_to_send)
return response
except Exception as e:
logging.exception("{}: Email could not be sent: {}".format(loc, e))
raise
def success_mail(message):
response = send_mail("TradingView to OANDA: Success", message)
return response
def fail_mail(message):
response = send_mail("TradingView to OANDA: Fail", message)
return response
if __name__ == "__main__":
# Set logging parameters
logging.basicConfig(level=logging.INFO)
loc = "sendgrid_api.py"
response = send_mail(
subject="SendGrid test from within {}".format(loc),
message=get_datetime_now()
)
logging.info("{}: Email sent successfully. SendGrid answered with a {} "
"statuscode: {}".format(
loc, response.status_code, response.body))