forked from penn201500/python-auto-discover
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
80 lines (63 loc) · 2.81 KB
/
Copy pathmail.py
File metadata and controls
80 lines (63 loc) · 2.81 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
import os
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
class EmailSender:
def __init__(self, email_from, email_to, username, password, smtp_server):
self.email_from = email_from
self.email_to = email_to
self.username = username
self.password = password
self.smtp_server = smtp_server
def send_email(self, file_to_send, subject):
if not os.path.isfile(file_to_send):
raise ValueError('Invalid file path')
if not self.email_from:
raise ValueError('Missing sender email')
if not self.email_to:
raise ValueError('Missing recipient email')
if not self.username:
raise ValueError('Missing email username')
if not self.password:
raise ValueError('Missing email password')
if not self.smtp_server:
raise ValueError('Missing SMTP server information')
msg = MIMEMultipart()
msg["From"] = self.email_from
msg["To"] = self.email_to
msg["Subject"] = subject
msg.preamble = subject
text_part = MIMEText('Check for zenoss devices in attachment, please see it', 'plain')
msg.attach(text_part)
ctype, encoding = mimetypes.guess_type(file_to_send)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
with open(file_to_send, 'rb') as fp:
if maintype == "text":
attachment = MIMEText(fp.read(), _subtype=subtype)
elif maintype == "image":
attachment = MIMEImage(fp.read(), _subtype=subtype)
elif maintype == "audio":
attachment = MIMEAudio(fp.read(), _subtype=subtype)
else:
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=file_to_send)
msg.attach(attachment)
with smtplib.SMTP(self.smtp_server) as server:
server.starttls()
server.login(self.username, self.password)
server.sendmail(self.email_from, self.email_to, msg.as_string())
if __name__ == '__main__':
email_sender = EmailSender(email_from='user@gmail.com',
email_to='user@icloud.com',
username='user@gmail.com',
password='password',
smtp_server='smtp.gmail.com:587')
email_sender.send_email(file_to_send='path/to/file',
subject='Check for zenoss devices')