-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.py
55 lines (39 loc) · 1.31 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
from flask import Flask, request, jsonify
from config import config
import logging
import yagmail
# Flask API email sender
mail = Flask(__name__)
logging.basicConfig(
filename=config['log_file'],
level=config['log_level']
)
username = config['sender_mail']
password = config['password']
yag = yagmail.SMTP(username)
yagmail.register(username, password)
def send_mail(request):
receiverData = request.args.get('receiver')
subjectData = request.args.get('subject')
contentsData = request.args.get('contents')
logging.info(f"{receiverData} {subjectData} {contentsData}")
return yag.send(to=receiverData,
subject=subjectData,
contents=contentsData)
@mail.route('/', methods=['GET'])
def get_name():
return jsonify({"developer": "Enes Turan"},
{"version": "1.0.0"}), 200
@mail.route('/send', methods=['POST'])
def sender():
if request.method == 'POST':
logging.info(f"{request.data}")
try:
send_mail(request)
return jsonify({"message": "Mail sent successfully"},
{"status": "success"}), 200
except Exception as e:
logging.error(f"{e}")
return jsonify({"message": "Mail not sent"}), 500
if __name__ == "__main__":
mail.run(debug=True, host="localhost", port=5000)