-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEmail.py
52 lines (40 loc) · 1.65 KB
/
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
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
# The first step is always the same: import all necessary components:
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
import os
import argparse
import ConfigValues
def Send_Mail(Image_Path, name, label):
try:
port = ConfigValues.ReturnSMTPPort()
smtp_server = ConfigValues.ReturnSMTPServer()
login = ConfigValues.ReturnSMTPLogin()
password = ConfigValues.ReturnSMTPPassword()
fromaddr = ConfigValues.ReturnSMTPLogin()
toaddr = ConfigValues.ReturnSMTPToAddress()
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = name + ": Alert -" + label
body = "Detection Image Attached"
msg.attach(MIMEText(body, 'plain'))
if Image_Path != "":
image_filename = Image_Path #'C:\Build3\RT_OD\Alert_image.jpg'#'./img.jpg'
image = MIMEImage(open(image_filename, 'rb').read(), name=os.path.basename(image_filename))
msg.attach(image)
s = smtplib.SMTP(smtp_server, port)
s.starttls()
s.login(fromaddr, password)
text = msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()
print('Sent')
except:
print("Oops!, Send Email Error: ", sys.exc_info()[0], "occurred.")
print("For GMAIL Authentication errors please ensure allow less secure apps is enabled https://myaccount.google.com/lesssecureapps?pli=1")