-
Notifications
You must be signed in to change notification settings - Fork 48
/
mailer.py
48 lines (38 loc) · 1.35 KB
/
mailer.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
"""handles the mailing operations across the app."""
import traceback
from typing import Dict
import requests
from requests.models import Response
from exceptions import FailedToSendMail
class Mailer:
"""Send a mail through the Mailgun API."""
auth = None
api_url = None
sender = None
def __init__(self, domain: str, api_key: str, sender_name: str) -> None:
"""
Initialize the Mailer class.
:param domain: Domain name of the sender
:type domain: str
:param api_key: API key of the Mailgun API
:type api_key: str
:param sender_name: name of the person sending the email
:type sender_name: str
"""
self.auth = ("api", api_key)
self.api_url = f"https://api.mailgun.net/v3/{domain}"
self.sender = f"{sender_name} <noreply@{domain}>"
def send_simple_message(self, data: Dict) -> Response:
"""
Send a message.
:param data: A dict consisting of the data for email
:type data: dict
:return: A Response object
:rtype: requests.Response
"""
data['from'] = self.sender
try:
return requests.post(f"{self.api_url}/messages", auth=self.auth, data=data)
except (requests.HTTPError, requests.ConnectionError):
traceback.print_exc()
raise FailedToSendMail