-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-email.py
executable file
·43 lines (39 loc) · 1.19 KB
/
send-email.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
#!/usr/bin/python3
import sys
import sendgrid
import os
from datetime import datetime
import base64
from python_http_client.exceptions import HTTPError
today = str(datetime.now())
sendgrid_api_key = os.environ['SG_API_KEY']
from_email = os.environ['FROM_EMAIL']
to_email = os.environ['TO_EMAIL']
from sendgrid import Email, Attachment, Content
from sendgrid.helpers.mail import Mail
sg = sendgrid.SendGridAPIClient(sendgrid_api_key)
from_email = (from_email)
subject = "Motion Detected!"
to_email = (to_email)
content = "I detected a movement at {}!".format(today)
file = None
with open(sys.argv[1], "rb") as video:
file=video.read()
video.close()
file = base64.b64encode(file).decode()
print(file)
attachment = Attachment()
attachment.file_content=(file)
attachment.file_type=("video/x-matroska")
attachment.file_name=("video.mkv")
attachment.disposition=("attachment")
attachment.content_id=("Motion Video")
mail = Mail(from_email=from_email, subject=subject, to_emails=to_email, html_content=content)
mail.add_attachment(attachment)
try:
response = sg.send(mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
except HTTPError as e:
print(e.to_dict)