-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpymail.py
193 lines (179 loc) · 6.9 KB
/
pymail.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
#!/usr/bin/env python3.7
import smtplib
import os
import sys
from termcolor import colored
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def smtp_filter(email_address):
email_client = email_address.split("@")
email_client = email_client[1].split(".")
if email_client[0].lower() == "gmail":
return ["smtp.gmail.com", 587]
elif email_client[0].lower() == "outlook":
return ["smtp-mail.outlook.com", 587]
elif email_client[0].lower() == "yahoo":
return ["smtp.mail.yahoo.com", 587]
elif email_client[0].lower() == "icloud":
return ["smtp.mail.me.com", 587]
elif email_client[0].lower() == "zoho":
return ["smtp.zoho.com", 587]
elif email_client[0].lower() == "aol":
return ["smtp.aol.com", 587]
elif email_client[0].lower() == "yandex":
return ["smtp.yandex.com", 465]
elif email_client[0].lower() == "fastmail":
return ["smtp.fastmail.com", 465]
elif email_client[0].lower() == "gmx":
return ["mail.gmx.com", 587]
else:
return ["smtp.gmail.com", 587]
return 0
def email_send(
sender_email_address,
sender_email_address_password,
target_email_address,
email_body,
):
print(colored("Sending Email ...", "yellow"))
smtp = smtp_filter(sender_email_address)
server = smtplib.SMTP(smtp[0], smtp[1])
server.starttls()
try:
server.login(sender_email_address, sender_email_address_password)
server.sendmail(sender_email_address, target_email_address, email_body)
server.quit()
print(colored("EMAIL SENT", "yellow"))
except:
print(colored("\n\tERROR: There was some problem while sending email\n", "red"))
def email_header(
sender_email_address,
sender_email_address_password,
target_email_address,
email_subject,
email_body,
email_attachments,
):
print(colored("Creating Email ...", "yellow"))
msg = MIMEMultipart()
msg["From"] = sender_email_address
msg["To"] = target_email_address
msg["Subject"] = email_subject
msg_body = email_body
msg.attach(MIMEText(msg_body, "plain"))
if email_attachments != "":
print(colored("Linking Attachments ...", "yellow"))
try:
filename_split = email_attachments.split("/")
filename = filename_split[len(filename_split) - 1]
attachment = open(email_attachments, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition", "attachment; filename= %s" % filename
)
msg.attach(part)
except:
print(
colored(
"\n\tERROR: Problem faced while linking Attachments. [EXIT CODE: 101]\n",
"red",
)
)
sys.exit(1)
full_message = msg.as_string()
email_send(
sender_email_address,
sender_email_address_password,
target_email_address,
full_message,
)
def main():
input_command = sys.argv
email_subject = ""
email_body = ""
email_attachments = ""
try:
for i in range(len(input_command)):
if input_command[i] == "--from":
if i + 2 <= len(input_command) - 1:
if input_command[i + 2][:2] == "--":
sender_email_address = input_command[i + 1]
else:
print(
"--from parameter can only have 1 value. [EXIT CODE: 102]"
)
sys.exit(102)
else:
sender_email_address = input_command[i + 1]
elif input_command[i] == "--passwd":
if i + 2 <= len(input_command) - 1:
if input_command[i + 2][:2] == "--" or input_command[i + 2] == None:
sender_email_address_password = input_command[i + 1]
else:
print(
"--passwd parameter can only have 1 value. [EXIT CODE: 102]"
)
sys.exit(102)
else:
sender_email_address_password = input_command[i + 1]
elif input_command[i] == "--to":
if i + 2 <= len(input_command) - 1:
if input_command[i + 2][:2] == "--":
target_email_address = input_command[i + 1]
else:
print("--to parameter can only have 1 value. [EXIT CODE: 102]")
sys.exit(102)
else:
target_email_address = input_command[i + 1]
elif input_command[i] == "--subject":
if i + 2 <= len(input_command) - 1:
if input_command[i + 2][:2] == "--":
email_subject = input_command[i + 1]
else:
print(
"--subject parameter can only have 1 value. [EXIT CODE: 102]"
)
sys.exit(102)
else:
email_subject = input_command[i + 1]
elif input_command[i] == "--message":
if i + 2 <= len(input_command) - 1:
if input_command[i + 2][:2] == "--":
email_body = input_command[i + 1]
else:
print(
"--message parameter can only have 1 value. [EXIT CODE: 102]"
)
sys.exit(102)
else:
email_body = input_command[i + 1]
elif input_command[i] == "--attach":
if i + 2 <= len(input_command) - 1:
if input_command[i + 2][:2] == "--":
email_attachments = input_command[i + 1]
else:
print(
"--attach parameter can only have 1 value. [EXIT CODE: 102]"
)
sys.exit(102)
else:
email_attachments = input_command[i + 1]
except:
print("Error handled")
email_header(
sender_email_address,
sender_email_address_password,
target_email_address,
email_subject,
email_body,
email_attachments,
)
if "__main__" == __name__:
main()