-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendEmail.py
More file actions
91 lines (79 loc) · 2.73 KB
/
Copy pathsendEmail.py
File metadata and controls
91 lines (79 loc) · 2.73 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import smtplib
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from config import emailFrom
from email.mime.base import MIMEBase
from email import encoders
from sys import getsizeof
def hasItemsAboveMaxSize(attachmentArray, maxSize):
isBigger = False
for element in attachmentArray:
if int(getsizeof(element)) > maxSize:
isBigger = True
break
return isBigger
def splitAttachment(attachment):
lines = attachment.split("\n")
numberOfLines = len(lines)
half = numberOfLines//2
firstHalf = lines[:half]
secondHalf = lines[half:]
firstHalf = "\n".join(firstHalf)
secondHalf = "\n".join(secondHalf)
return [firstHalf,secondHalf]
def generateAttachmentArray(attachment, maxSize):
attachmentArray = splitAttachment(attachment)
while hasItemsAboveMaxSize(attachmentArray, maxSize):
for index, value in enumerate(attachmentArray):
itemSize = int(getsizeof(value))
if itemSize > maxSize:
attachmentArray.pop(index)
attachmentArray.extend(splitAttachment(value))
return attachmentArray
def sendEmail(emailTo, fromAddr, messageBody, subject):
message = EmailMessage()
message.set_content(messageBody)
message['From'] = fromAddr
message['To'] = emailTo
message['Subject'] = subject
try:
session = smtplib.SMTP('smtp.gvsu.edu', 25) #use gmail with port
session.send_message(message)
session.quit()
except Exception as e:
print("Unable to send mail: " + str(e))
return
def sendEmailWithAttachment(emailTo, fromAddr, subject, attachment):
filename = "report.csv"
try:
message = MIMEMultipart()
message['From'] = fromAddr
message['To'] = emailTo
message['Subject'] = subject #The subject line
session = smtplib.SMTP('smtp.gvsu.edu', 25)
session.ehlo()
maxLimit = int( session.esmtp_features['size'] )
fileSize = int(getsizeof(attachment))
if fileSize > maxLimit:
bodyList = generateAttachmentArray(attachment, maxLimit)
length = len(bodyList)
current = 1
for chunk in bodyList:
newSubject = subject + " " + str(current) + " of " + str(length)
sendEmailWithAttachment(emailTo, fromAddr, newSubject, chunk)
current += 1
return
part = MIMEBase('application', "octet-stream")
part.set_payload(attachment)
encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment; filename=%s" % filename)
message.attach(part)
text = message.as_string()
session.sendmail(emailFrom, emailTo, text)
session.quit()
except Exception as e:
msg = "Unable to send mail: " + str(e)
subject = "Error: cannot email report"
sendEmail(emailTo, fromAddr, msg, subject)
return