-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_email.py
More file actions
76 lines (62 loc) · 2.4 KB
/
send_email.py
File metadata and controls
76 lines (62 loc) · 2.4 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
#!/usr/bin/env python3
"""
Default Ports: Server: Authentication: Port:
============= ====== ============== ====
SMTP Server (Outgoing Messages) Non-Encrypted AUTH 25 (or 587)
Secure (TLS) StartTLS 587
Secure (SSL) SSL 465
POP3 Server (Incoming Messages) Non-Encrypted AUTH 110
Secure (SSL) SSL 995
"""
import smtplib
# ------------------------------------------------------------
# Currently only text body message is supported.
# Future work:
# - add attachment
# - add image(s)
# ------------------------------------------------------------
class EmailGmail:
def __init__(self, fromaddr=None, username=None, password=None):
self.fromaddr = fromaddr
self.username = username
self.password = password
self.subject = ""
self.message = []
def set_fromaddr(self, text):
self.fromaddr = text
def set_usename(self, text):
self.username = text
def set_password(self, text):
self.password = text
def setselfubject(self, text):
self.subject = text
def set_body_message(self, text_list):
self.message = text_list
def send_email(self, toaddr):
print("[=] sending email...")
if not self.fromaddr or not self.username or not self.password or not self.subject:
print("[=] some required parameter is not set")
return False
if not self.message:
self.message.append("[=] sending empty message")
print(self.message[0])
self.message.insert(0, "From: "+ self.fromaddr)
self.message.insert(1, "To: "+ toaddr)
self.message.insert(2, "Subject: "+ self.subject)
self.message.insert(3, " ")
msg = "\r\n".join(self.message)
#
# start sending email
#
server = smtplib.SMTPselfSL('smtp.gmail.com:465')
server.ehlo()
#
# no server.starttls() as we are using
# SMTPselfSL function
#
server.login(self.username, self.password)
server.sendmail(self.fromaddr, toaddr, msg)
server.quit()
print("[=] email sending done")
return True
# -- end --