-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
53 lines (35 loc) · 1.28 KB
/
lambda_function.py
File metadata and controls
53 lines (35 loc) · 1.28 KB
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
import imaplib
import os
import boto3
sns_client = boto3.client('sns')
topic_arn = os.getenv('TOPIC_ARN')
host = os.getenv('IMAP_SERVER')
port = os.getenv('IMAP_PORT')
password = os.getenv('PASSWORD')
username = os.getenv('USERNAME')
lease_email = os.getenv('LEASE_EMAIL')
salary_email = os.getenv('SALARY_EMAIL')
def lambda_handler(event, context):
publish_notification(topic_arn,
lease_email,
'Invoice from Qasa is here! It is the time to pay')
publish_notification(topic_arn,
salary_email,
'Salary is here it is the time to plan your budget, forecast and invest')
def publish_notification(arn, email, message):
result = find_unread_mail(email)
if result is not None:
publish(target_arn=arn, message=message)
def find_unread_mail(email):
mail = imaplib.IMAP4_SSL(host=host, port=int(port))
try:
mail.login(username, password)
mail.select('inbox')
return mail.search(None, 'From', email, '(UNSEEN)')
except Exception as e:
raise Exception("Failed to authenticate", e)
finally:
mail.close()
mail.logout()
def publish(target_arn, message):
sns_client.publish(TargetArn=target_arn, Message=message)