|
22 | 22 | SOFTWARE.
|
23 | 23 | """
|
24 | 24 |
|
25 |
| -import ssl as _ssl |
26 |
| -from email.mime.application import MIMEApplication as _MIMEApp |
27 |
| -from email.mime.audio import MIMEAudio as _MIMEAudio |
28 |
| -from email.mime.image import MIMEImage as _MIMEImage |
29 |
| -from email.mime.multipart import MIMEMultipart as _MIMEMultipart |
30 |
| -from email.mime.text import MIMEText as _MIMEText |
31 |
| -from smtplib import SMTP as _SMTP |
32 |
| -from time import sleep as _sleep |
| 25 | +from mailer.mailer import Mailer |
| 26 | +from mailer.utils import example |
33 | 27 |
|
34 | 28 | __all__ = ['Mailer', '__VERSION__', 'example']
|
35 |
| -__VERSION__ = '0.1.0' |
36 |
| - |
37 |
| - |
38 |
| -# Main Class |
39 |
| -class Mailer: |
40 |
| - def __init__(self, email: str, password: str): |
41 |
| - """ |
42 |
| - :param email: Your Email Address |
43 |
| - :param password: Yor Email Password |
44 |
| - """ |
45 |
| - # Variables |
46 |
| - self.email = email |
47 |
| - self.__password = password |
48 |
| - self.__server = None |
49 |
| - self.status = bool() |
50 |
| - self.login = bool() |
51 |
| - self.multi = bool() |
52 |
| - self.__sleep = int() |
53 |
| - self.__repeat = 1 |
54 |
| - self.count_rec = 1 |
55 |
| - self.count_cc = int() |
56 |
| - self.count_bcc = int() |
57 |
| - self.count_msg = self.__repeat*self.count_rec |
58 |
| - self.__port = 587 |
59 |
| - self.GMAIL = 'smtp.gmail.com' |
60 |
| - # self.YAHOO = 'smtp.mail.yahoo.com' # Unsupported Now |
61 |
| - self.MICROSOFT = 'smtp.office365.com' |
62 |
| - self.provider = self.GMAIL |
63 |
| - |
64 |
| - # Apply Settings |
65 |
| - self.settings() |
66 |
| - |
67 |
| - # About Method |
68 |
| - @staticmethod |
69 |
| - def about(): |
70 |
| - info = """ |
71 |
| -About Module: |
72 |
| - This Module help you to send fast Email. |
73 |
| - And you can attach [image, audio, and other files] easily. |
74 |
| -
|
75 |
| -About Developer: |
76 |
| - Name: Ahmed Al-Taie |
77 |
| - Github: https://github.com/Al-Taie |
78 |
| - Pypi: https://pypi.org/user/Altaie |
79 |
| - Instagram: https://www.instagram.com/9_Tay |
80 |
| -
|
81 |
| - Finally Thanks For Use My Module. |
82 |
| - You Can Join Our Discord For Any Question: |
83 |
| - Discord: https://discord.gg/gWdCNv7 |
84 |
| - """ |
85 |
| - print(info) |
86 |
| - return |
87 |
| - |
88 |
| - # File Reader Method |
89 |
| - @staticmethod |
90 |
| - def _file_reader(filename: str): |
91 |
| - with open(filename, 'rb') as f: |
92 |
| - return f.read() |
93 |
| - |
94 |
| - # Settings Method |
95 |
| - def settings(self, |
96 |
| - repeat: int = 1, |
97 |
| - sleep=None, |
98 |
| - provider: str = None, |
99 |
| - multi=False): |
100 |
| - """ |
101 |
| - :param multi: |
102 |
| - :type multi: |
103 |
| - :param repeat: Repeat Number |
104 |
| - :param sleep: Set Sleep Time (In Seconds) |
105 |
| - :param provider: See example Function |
106 |
| - :return: None |
107 |
| - """ |
108 |
| - self.__repeat = repeat |
109 |
| - self.multi = multi |
110 |
| - |
111 |
| - if sleep: |
112 |
| - self.__sleep = sleep |
113 |
| - |
114 |
| - if provider: |
115 |
| - self.provider = provider |
116 |
| - |
117 |
| - # Login Method |
118 |
| - def __login(self): |
119 |
| - context = _ssl.create_default_context() |
120 |
| - |
121 |
| - self.__server = _SMTP(host=self.provider, |
122 |
| - port=self.__port) |
123 |
| - self.__server.ehlo() |
124 |
| - self.__server.starttls(context=context) |
125 |
| - self.__server.ehlo() |
126 |
| - |
127 |
| - try: |
128 |
| - self.__server.login(user=self.email, |
129 |
| - password=self.__password) |
130 |
| - self.login = True |
131 |
| - return True |
132 |
| - except Exception as e: |
133 |
| - if self.provider == self.GMAIL: |
134 |
| - problem = """ |
135 |
| -Error: Email And Password Not Accepted. |
136 |
| -
|
137 |
| -Note: |
138 |
| - Make sure you Allowed less secure apps, |
139 |
| - if you didn't, visit this link: |
140 |
| - ==> https://myaccount.google.com/lesssecureapps |
141 |
| -
|
142 |
| - For More information visit this link: |
143 |
| - ==> https://support.google.com/mail/?p=BadCredentials |
144 |
| - """ |
145 |
| - else: |
146 |
| - problem = e |
147 |
| - print(problem) |
148 |
| - self.login = False |
149 |
| - return False |
150 |
| - |
151 |
| - # Send Method |
152 |
| - def send(self, |
153 |
| - receiver, |
154 |
| - cc=None, |
155 |
| - bcc=None, |
156 |
| - subject: str = None, |
157 |
| - message: str = None, |
158 |
| - image: str = None, |
159 |
| - audio: str = None, |
160 |
| - file: str = None): |
161 |
| - """ |
162 |
| - :param cc: Email Address as String or List. (Carbon Copy) |
163 |
| - :param bcc: Email Address as String or List. (Blind Carbon Copy) |
164 |
| - :param receiver: Email Address as String or List |
165 |
| - :param subject: Message Title |
166 |
| - :param message: Your Message |
167 |
| - :param image: Image File Name |
168 |
| - :param audio: Audio File Name |
169 |
| - :param file: File Name |
170 |
| - :return: Boolean |
171 |
| - """ |
172 |
| - |
173 |
| - msg = _MIMEMultipart() |
174 |
| - msg['Subject'] = subject |
175 |
| - msg['From'] = self.email |
176 |
| - |
177 |
| - try: |
178 |
| - if message is not None: |
179 |
| - text = _MIMEText(message) |
180 |
| - msg.attach(text) |
181 |
| - |
182 |
| - if image is not None: |
183 |
| - image_data = self._file_reader(image) |
184 |
| - image = _MIMEImage(_imagedata=image_data, name=image) |
185 |
| - msg.attach(image) |
186 |
| - |
187 |
| - if audio is not None: |
188 |
| - audio_data = self._file_reader(audio) |
189 |
| - audio = _MIMEAudio(_audiodata=audio_data, name=audio, _subtype='') |
190 |
| - msg.attach(audio) |
191 |
| - |
192 |
| - if file is not None: |
193 |
| - file_data = self._file_reader(file) |
194 |
| - file = _MIMEApp(_data=file_data, name=file) |
195 |
| - msg.attach(file) |
196 |
| - except Exception: |
197 |
| - print('Error: File Not Found!') |
198 |
| - self.status = False |
199 |
| - return False |
200 |
| - |
201 |
| - send_info = [] |
202 |
| - multi_info = {} |
203 |
| - |
204 |
| - if 'list' in str(type(receiver)): |
205 |
| - self.count_rec = len(receiver) |
206 |
| - receiver = ','.join(i for i in receiver) |
207 |
| - |
208 |
| - if 'list' in str(type(cc)): |
209 |
| - self.count_cc = len(cc) |
210 |
| - cc = ','.join(i for i in cc) |
211 |
| - |
212 |
| - if 'list' in str(type(bcc)): |
213 |
| - self.count_bcc = len(bcc) |
214 |
| - bcc = ','.join(i for i in bcc) |
215 |
| - |
216 |
| - if self.__login(): |
217 |
| - msg['To'] = receiver |
218 |
| - msg['CC'] = cc |
219 |
| - msg['BCC'] = bcc |
220 |
| - |
221 |
| - if self.multi: |
222 |
| - for _ in range(self.__repeat): |
223 |
| - try: |
224 |
| - self.__server.sendmail(from_addr=self.email, |
225 |
| - to_addrs=receiver, |
226 |
| - msg=msg.as_string()) |
227 |
| - except Exception: |
228 |
| - if self.__repeat == 1 & self.count_rec == 1: |
229 |
| - self.status = False |
230 |
| - else: |
231 |
| - send_info.append(False) |
232 |
| - |
233 |
| - else: |
234 |
| - if self.__repeat == 1 & self.count_rec == 1: |
235 |
| - self.status = True |
236 |
| - else: |
237 |
| - send_info.append(True) |
238 |
| - self.status = send_info |
239 |
| - |
240 |
| - finally: |
241 |
| - _sleep(self.__sleep) |
242 |
| - else: |
243 |
| - for rec in receiver.split(','): |
244 |
| - send_info = [] |
245 |
| - for _ in range(self.__repeat): |
246 |
| - try: |
247 |
| - self.__server.sendmail(from_addr=self.email, |
248 |
| - to_addrs=rec, |
249 |
| - msg=msg.as_string()) |
250 |
| - except Exception as e: |
251 |
| - if self.__repeat == 1 & self.count_rec == 1: |
252 |
| - self.status = False |
253 |
| - else: |
254 |
| - send_info.append(False) |
255 |
| - |
256 |
| - if 'OutboundSpamException' in str(e): |
257 |
| - print('Error: Please Login To Your Account And Verify it.') |
258 |
| - break |
259 |
| - |
260 |
| - else: |
261 |
| - if self.__repeat == 1 & self.count_rec == 1: |
262 |
| - self.status = True |
263 |
| - else: |
264 |
| - send_info.append(True) |
265 |
| - self.status = send_info |
266 |
| - multi_info[rec] = send_info |
267 |
| - |
268 |
| - finally: |
269 |
| - _sleep(self.__sleep) |
270 |
| - |
271 |
| - if self.count_rec != 1: |
272 |
| - self.status = multi_info |
273 |
| - |
274 |
| - self.__server.close() |
275 |
| - |
276 |
| - |
277 |
| -# Example Function |
278 |
| -def example(): |
279 |
| - ex = """ |
280 |
| -#################### |
281 |
| -# [Copy This Code] # |
282 |
| -#################### |
283 |
| -
|
284 |
| -mail = Mailer(email='[email protected]', |
285 |
| - password='***') |
286 |
| -
|
287 |
| -# IF You Want Repeat Sending, Change repeat Value |
288 |
| -# And IF You Want Change Mail Service |
289 |
| -# Chose One: mail.[GMAIL, MICROSOFT] |
290 |
| -mail.settings(repeat=1, |
291 |
| - sleep=0, |
292 |
| - provider=mail.GMAIL, |
293 |
| - multi=False) |
294 |
| -
|
295 |
| -# Send Message |
296 |
| -mail.send(receiver='[email protected]', # Email From Any service Provider |
297 |
| - |
298 |
| - |
299 |
| - subject='TEST', |
300 |
| - message='HI, This Message From Python :)', |
301 |
| - image=None, # Image File Path |
302 |
| - audio=None, # Audio File Path |
303 |
| - file=None) # Any File Path |
304 |
| -
|
305 |
| -# Login Status Info |
306 |
| -print('login:', mail.login) |
307 |
| -
|
308 |
| -# Sending Status Info |
309 |
| -print('status:', mail.status) |
310 |
| -
|
311 |
| -# CC Receivers Count |
312 |
| -print('CC count:', mail.count_cc) |
313 |
| -
|
314 |
| -# BCC Receivers Count |
315 |
| -print('BCC count:', mail.count_bcc) |
316 |
| -
|
317 |
| -# Receivers Count |
318 |
| -print('Receivers count:', mail.count_rec) |
319 |
| -
|
320 |
| -# Messages Count IF You Allowed Repeat |
321 |
| -print('Messages count:', mail.count_msg) |
322 |
| -
|
323 |
| -# For More Information |
324 |
| -mail.about() |
325 |
| - """ |
326 |
| - print(ex) |
| 29 | +__VERSION__ = '2022.1.19' |
0 commit comments