-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send mail.py
289 lines (247 loc) · 10.6 KB
/
Send mail.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import time
from colorama import Fore, Style
import openai
# Setup port number and server name
smtp_port = 587 # Standard secure SMTP port
smtp_server = "smtp.gmail.com" # Google SMTP Server
# OpenAI key to access OpenAI account
openai.api_key = "" #your API key
# Password to access Gmail account
pswd = '' #Password to your gmail account
# Email id from which the mails have to be sent
email_from = "" #Your Email ID
# prints the intro of the program
def intro():
he = '''###
Hey want to send an EMAIL?
Dont worry we got you!!!
Just type the required fields below.
'''
for char in he:
print(Fore.BLUE + Style.BRIGHT + char, end='', flush=True)
print()
intro()
# asks the user for the number of recipients to send the mail.
def no_of_recepients():
while True:
try:
x = input(Fore.GREEN + Style.BRIGHT + 'No of recipients you want to send - ')
if x.isdigit():
pass
else:
raise ValueError
return int(x)
except ValueError:
print(Fore.RED + Style.BRIGHT + 'ERROR, please enter again')
recipient_count = no_of_recepients()
# asks the user for the emailid's of the respective recipients
def taking_recepient_emailid():
time.sleep(2)
d = '@gmail.com'
email_list = []
# for single recipient
if recipient_count == 1:
for i in range(1, recipient_count + 1):
def tried1():
while True:
try:
t = input(Fore.GREEN + Style.BRIGHT + f'Recipient - ') + d
for j in t:
if j.isdigit() or j.isalpha() or j == '@' or j == '.' :
pass
else:
raise ValueError
return t
except ValueError:
print(Fore.RED + Style.BRIGHT + 'ERROR, please enter again')
t1 = tried1()
email_list.append(t1)
# for multiple recipients
else:
for i in range(1, recipient_count + 1):
def tried2():
while True:
try:
g = input(Fore.GREEN + Style.BRIGHT + f'Recipient {i} - ') + d
for v in g:
if v.isdigit() or v.isalpha() or v == '@' or v == '.':
pass
else:
raise ValueError
return g
except ValueError:
print(Fore.RED + Style.BRIGHT + 'ERROR, please enter again')
t2 = tried2()
email_list.append(t2)
return email_list
recipient_list = taking_recepient_emailid()
print()
print(Fore.RED + "Connecting to server...")
print(Fore.RED + "Successfully connected to server....", time.sleep(2))
print()
# creates the subject for the mail.
def mail_subject():
subject = input(Fore.CYAN + Style.BRIGHT + 'write the subject of the email - ')
return subject
subject_of_mail = mail_subject()
# creates the body for the mail
def mail_body():
while True:
try:
print()
input1 = input(Fore.CYAN + Style.BRIGHT + '''Do you want to write email on your own or use chat gpt
(say yes to not use gpt and say gpt to use chatgpt) - ''')
print()
if input1 == 'yes' or input1 == 'YES' or input1 == 'Yes':
response1 = input(Fore.GREEN + Style.BRIGHT + 'Write your body - ')
return response1
elif input1 == 'gpt' or input1 == 'GPT' or input1 == 'Gpt':
def chatgpt():
while True:
try:
print()
# telling chatgpt to generate the body of the email
prompt = input(Fore.CYAN + Style.BRIGHT + 'Write your prompt - ')
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], n=3, stop=None, temperature=0)
response2 = completion.choices[0].message.content
print()
print(response2)
print()
x = input('Are you satisfied with the result(Yes/No) - ')
if x == 'Yes' or x == 'YES' or x == 'yes':
pass
elif x == 'NO' or x == 'No' or x == 'no':
raise ValueError
else:
raise TypeError
return response2
except TypeError:
print(Fore.RED + Style.BRIGHT + 'ERROR, please enter again')
except ValueError:
print(Fore.RED + Style.BRIGHT + 'Sorry for the inconvenience caused. please type your prompt again')
response2 = chatgpt()
return response2
else:
raise ValueError
except ValueError:
print(Fore.RED + Style.BRIGHT + 'ERROR, please enter again')
body_of_mail = mail_body()
print()
# Attaching files to the email
fileattachment = input(Fore.MAGENTA + Style.BRIGHT + 'Do you want to send an attachment(Yes/No) - ')
if fileattachment == 'yes':
# shows the files available to send on the console
def show_files():
print()
print(Fore.MAGENTA + Style.BRIGHT + 'These are the available files you can send:- ')
l = 1
file_list = {}
for i in os.listdir(r''): # add your own directory from which the files are being displayed
file_list[l] = i
l = l + 1
time.sleep(2)
po = 1
for g in file_list:
print(f'{po}.', file_list[g])
po = po + 1
print()
return file_list
filelist = show_files()
time.sleep(2)
# asks the user the files he/she wants to send from the list shown on the console
def file_you_want_to_send():
while True:
try:
fileinput = input(Fore.GREEN + Style.BRIGHT + '''which files do you want to send
(input in the form - fileno,fileno,....) - ''')
file_inp = fileinput.split(',')
if len(file_inp) <= len(filelist):
for i in file_inp:
if i.isdigit() and int(i) > 0:
pass
else:
raise ValueError
return file_inp
else:
raise ValueError
except ValueError:
print(Fore.RED + Style.BRIGHT + 'ERROR, please enter again')
file_input = file_you_want_to_send()
else:
file_input = 'false'
# this function sends email to the respective recipients in a for loop.
def send_emails(email_list,subject):
for person in email_list:
# Making the structure of the body of the mail
body = f"""
Dear: {person}
{body_of_mail}"""
# making a MIME object to define parts of the email
msg = MIMEMultipart()
msg['From'] = email_from
msg['To'] = person
msg['Subject'] = subject
# Attaching the body to the mail
msg.attach(MIMEText(body, 'plain'))
print()
# if the user has said 'yes' to attaching of files
if file_input != 'false':
# attaching multiple files
def attaching_the_file():
FILELIST = []
for j in file_input:
FILELIST.append(filelist[int(j)])
for v in FILELIST:
filename = f'' # file path in this format (Keeep the variable) C:\\Users\\OneDrive\\Desktop\\code\\{v}
# Open the file in python as a binary
attachment = open(filename, 'rb') # r for read and b for binary
# Encode as base 64
attachment_package = MIMEBase('application', 'octet-stream')
attachment_package.set_payload((attachment).read())
encoders.encode_base64(attachment_package)
attachment_package.add_header('Content-Disposition', "attachment; filename= " + v)
msg.attach(attachment_package)
# Cast as string
text = msg.as_string()
# msg is a MIMEMultipart object that i have created to compose the email.
# It includes the sender, recipient, subject, body, and any attachments.
# However, for the email to be sent, it needs to be converted into a specific
# format that adheres to the email standards.
# This is where the as_string() method comes into play.
return text
text = attaching_the_file()
# if the user has said 'no' to attaching of files
else:
# we are rewriting the same body cus giving text variable to a string
# rewrites the whole code without the subject and the body.
# Making the outline of the body of the email
body = f"""
Dear: {person}
{body_of_mail}"""
# Making a MIME object to define parts of the email
msg = MIMEMultipart()
msg['From'] = email_from
msg['To'] = person
msg['Subject'] = subject
# Attaching the body of the message
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
# Connecting to the server
TIE_server = smtplib.SMTP(smtp_server, smtp_port)
TIE_server.starttls()
TIE_server.login(email_from, pswd)
# Sending emails to the respective "person" as list is iterated
print(Fore.RED + Style.BRIGHT + f"Sending email to: {person}")
TIE_server.sendmail(email_from, person, text)
print(Fore.RED + Style.BRIGHT + f"Email sent to: {person}")
# Closing the port
TIE_server.quit()
send_emails(recipient_list, subject_of_mail)
print()
print(Fore.RED + Style.BRIGHT + 'Finished')