-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendmail.py
More file actions
184 lines (132 loc) · 5.24 KB
/
sendmail.py
File metadata and controls
184 lines (132 loc) · 5.24 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
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
import os, sys
# email sending
import smtplib
import ssl
from email.message import EmailMessage
import random
import time
# Loading function
def Loading(value=True):
loading = ['|||||', '/////', '\\\\\\']
# loading = ['🌟🌟🌟🌟🌟', '💫💫💫💫💫', '✨✨✨✨✨', '🌈🌈🌈🌈🌈', '🌸🌸🌸🌸🌸', '🌼🌼🌼🌼🌼', '🌞🌞🌞🌞🌞', '🌛🌛🌛🌛🌛', '🌟💫✨💫🌟']
while value:
random.shuffle(loading) # Shuffle the loading icons randomly
for i in loading:
# print(f'Please wait...{i}', end="\r")
print(f'\rPlease wait...{i}', end="")
time.sleep(0.1)
value = False
EMAIL_PROVIDERS = ['@gmail.com', '@yahoo.com', '@outlook.com'] # Add other familiar email hosts here
# For development purpose
DEVELOPMENT = False # means production
config_file = 'config.py' # means production
config_path = ''
if os.path.exists('dev_config.py'): # development condition
from dev_config import EMAIL, PASSWORD
DEVELOPMENT = True
config_file = 'dev_config.py'
config_path = os.path.join(os.path.dirname(__file__), config_file)
else: # production condition
from config import EMAIL, PASSWORD
# config_file = 'config.py'
config_path = os.path.join(os.path.dirname(__file__), config_file)
def help():
commands = f"\n-help, -h : Help command\n-reset, -r : Resetting your current email and password"
print(commands)
def config():
while True:
if EMAIL == '' or PASSWORD == '':
print('Please configure your Email and Password:-')
else:
print('Please reset your Email and Password:-')
email = input("Enter you Email: ").replace(' ', '') # Remove all spaces from the input string.
psd = input("Enter you Password: ")
if email == '' or psd == '':
print('\nEmail and Password required')
continue
elif not any(email.endswith(domain) for domain in EMAIL_PROVIDERS):
print("\nInvalid email, only supports email providers like 'abc@gmail.com', 'abc@yahoo.com', 'abc@outlook.com'.\nPlease try again.\n")
continue
else:
# config_file = 'dev_config.py' if DEVELOPMENT else 'config.py'
config_path = os.path.join(os.path.dirname(__file__), config_file)
with open(config_path, 'w') as file:
file.write(f"EMAIL = '{email}'\nPASSWORD = '{psd}'")
if EMAIL == '' or PASSWORD == '':
print('Your Email and Password have been successfully added.')
exit(0)
# break
else:
print('You have successfully updated your Email and Password.')
exit(0)
# break
break
def get_email_subject_body():
def get_valid_email():
# To email valid
while True:
to_email = input("Enter recipient email: ").replace(' ', '')
if not to_email:
print('\nEmail recipient required!')
continue
if not any(to_email.endswith(domain) for domain in EMAIL_PROVIDERS):
print("\nInvalid recipient email. Supported email providers include 'gmail.com', 'yahoo.com', and 'outlook.com'.\nPlease try again.\n")
continue
return to_email
def get_valid_subject():
while True:
subject = input("Enter email subject: ")
if not subject:
print("Subject required!")
continue
return subject
def get_valid_message():
while True:
body = input("Enter email message: ")
if not body:
print("Message required!")
continue
return body
to_email = get_valid_email()
subject = get_valid_subject()
body = get_valid_message()
return to_email, subject, body
def email_sender(to_email, subject, body):
# message setting area
message=EmailMessage()
message['From'] = EMAIL
message['To'] = to_email
message['Subject'] = subject
# message.set_content(body)
html = f"""
<h3>{body}</h3>
"""
message.add_alternative(html, subtype="html")
context=ssl.create_default_context() # it securing connection
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(EMAIL, PASSWORD)
server.sendmail(EMAIL, to_email, message.as_string())
Loading()
print("\nYour email was sent successfully.")
def main():
if EMAIL == '' or PASSWORD == '':
config()
# Handling command-line arguments
if len(sys.argv) > 1:
command = sys.argv[1]
# re-setting the email and password
if command in ('-r', '-reset'):
config()
elif command in ('-h', '-help'):
help()
sys.exit(0)
else:
print("Unknown command. Use -h or -help for the list of available commands.")
sys.exit(1)
# print(len(sys.argv))
else:
# print('main code')
to_email, subject, body = get_email_subject_body()
email_sender(to_email, subject, body)
if __name__ == "__main__":
main()