-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSMTP_Email.py
More file actions
151 lines (128 loc) · 4.37 KB
/
SMTP_Email.py
File metadata and controls
151 lines (128 loc) · 4.37 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
############################################################################
#
# SMTP Email Class
# © 2021 ABDULKADİR GÜNGÖR All Rights Reserved
# Contact email address: abdulkadir_gungor@outlook.com
#
# Developper: Abdulkadir GÜNGÖR (abdulkadir_gungor@outlook.com)
# Date: 04/2021
# All Rights Reserved (Tüm Hakları Saklıdır)
#
############################################################################
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
class Add_File:
''' Add png file to e-mail '''
#
def __init__(self, name, filefullpath):
self.name = name
self.type = ''
self.subtype = ''
self.binary = b''
#
self.__upload(filefullpath)
#
def __upload(self,filefullpath):
with open(filefullpath, 'rb') as file:
self.binary = file.read()
if self.binary is None:
self.binary = b''
return self
# png
def png(self):
self.type = 'image'
self.subtype = 'png'
return self
# jpg
def jpg(self):
self.type = 'image'
self.subtype = 'jpg'
return self
# jpeg
def jpeg(self):
self.type = 'image'
self.subtype = 'jpeg'
return self
# pdf
def pdf(self):
self.type = 'application'
self.subtype = 'pdf'
return self
# swf
def swf(self):
self.type = 'application'
self.subtype = 'swf'
return self
# exe
def exe(self):
self.type = 'application'
self.subtype = 'exe'
return self
# other(self, type, subtype)
# 1) type = 'application'
# 2) subtype = 'exe'
def other(self, type:str, subtype:str):
self.type = type
self.subtype = subtype
return self
#
class SMTP_Email:
''' Sending e-mail using the smtp protocol '''
# __init__(self, smtp, smtp_port, sender, password):
# 1) smtp = 'smtp.gmail.com'
# 2) smtp_port = 587
# 3) sender = 'sender.gungor@gmail.com'
# 4) password = 'abc123DEF456'
def __init__(self, smtp:str, smtp_port:int, sender:str, password:str):
''' SMTP_Email Class Start '''
self.session = smtplib.SMTP(smtp, smtp_port)
self.sender_address = sender
self.sender_pass = password
self.receiver_address = ''
self.message = MIMEMultipart()
# message_body(self,mail_to,mail_subject,mail_message):
# 1) mail_to = 'receiver.example@gmail.com'
# 2) mail_subject = 'A test mail'
# 3) mail_content = ' Hi, a test mail, etc ...'
def message_body(self,mail_to:str,mail_subject:str,mail_content:str):
''' Email Body '''
self.receiver_address = mail_to
#
self.message['From'] = self.sender_address
self.message['To'] = mail_to
self.message['Subject'] = mail_subject
self.message.attach( MIMEText(mail_content, 'plain') )
return self
# message_add_file(self, filename, filefullpath):
# filename = "example.pdf"
# filefullpath = "example.pdf" or "D:\python\examples\example.pdf"
def message_add_file(self, file:Add_File):
''' Add Any Files to Email '''
#
payload = MIMEBase(file.type, _subtype=file.subtype, name=file.name)
payload.set_payload( file.binary )
encoders.encode_base64(payload)
payload.add_header('Content-Decomposition', 'attachment', filename=file.name)
self.message.attach(payload)
return self
# message_send(self):
def message_send(self):
''' Sends the email '''
if self.receiver_address != '':
self.session.starttls()
self.session.login(self.sender_address, self.sender_pass)
self.session.sendmail(self.sender_address,self.receiver_address,self.message.as_string())
self.session.quit()
self.message_clear()
return self, True
else:
return self, False
# message_clear(self):
def message_clear(self):
''' Clears the content to send different e-mails '''
self.receiver_address = ''
self.message = MIMEMultipart()
return self