-
Notifications
You must be signed in to change notification settings - Fork 18
/
mail.py
53 lines (41 loc) · 2.61 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
"""
ccextractor-web | mail.py
Author : Saurabh Shrivastava
Email : saurabh.shrivastava54+ccextractorweb[at]gmail.com
Link : https://github.com/saurabhshri
"""
import requests
from flask import current_app as app
from logger import Logger
from config_parser import general_config
mail_logger = Logger(log_level=general_config['LOG_LEVEL'],
dir=general_config['LOG_FILE_DIR'],
filename="mail",
format='[%(asctime)s] [%(name)s] [%(levelname)s] [%(pathname)s#L%(lineno)d] | %(message)s\n\n',
console_level='DEBUG')
mail_log = mail_logger.get_logger("mail")
def get_api_url(domain):
return "https://api.mailgun.net/v3/{domain}/messages".format(domain=domain)
def send_simple_message(receiver, subject, body):
api_key = app.config['MAILGUN_PVT_API_KEY']
domain = app.config['EMAIL_DOMAIN']
sender = 'CCExtractor Web <no-reply@{domain}>'.format(domain=domain)
response = requests.post(get_api_url(domain),
auth=("api", api_key),
data={"from": sender,
"to": receiver,
"subject": subject,
"text": body})
if response.status_code is not 200:
mail_log.debug('\n[{response}] \nTO : {to}, \nfrom : {sender}, \nsubject : {subject}, \ntext: {text}'.format(response=response,
to=receiver,
sender=sender,
subject=subject,
text=body))
else:
mail_log.info('\n[{response}] \nTO : {to}, \nfrom : {sender}, \nsubject : {subject}, \ntext: {text}'.format(response=response,
to=receiver,
sender=sender,
subject=subject,
text=body))
return response