-
Notifications
You must be signed in to change notification settings - Fork 2
/
ses_forwarder.py
65 lines (49 loc) · 1.41 KB
/
ses_forwarder.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
from flask import Flask
from flask import request, jsonify
import sys
import boto3
import logging
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
app = Flask(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)
client = boto3.client('ses', region_name='us-east-1')
config = json.load(file(os.path.join(BASE_DIR, "config.json"),))
FROM_ADDRESS=config['from_address']
TO_ADDRESS=config['to_address']
def send_email(subject, body):
response = client.send_email(
Source=FROM_ADDRESS,
Destination={
'ToAddresses': [
TO_ADDRESS,
]
},
Message={
'Subject': {'Data': subject},
'Body': {
'Text': {'Data': body},
}
},
)
@app.route('/contact', methods=['POST'])
def post_email():
logger.info("Recieved mail post request")
result = {}
print request
try:
json = request.get_json()
subject = "NONE"
if json is not None:
subject = json['subject']
send_email(subject=subject,
body=json['body'])
result['subject'] = subject
result['status'] = 'success'
except Exception as e:
logger.error(e)
result['error'] = e.message
result['status'] = 'error'
return jsonify(**result)
if __name__ == '__main__':
app.run()