-
Notifications
You must be signed in to change notification settings - Fork 0
/
media-mail.py
100 lines (83 loc) · 2.73 KB
/
media-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
import sys
import smtplib
import getpass
import webbrowser
import imghdr
from email.message import EmailMessage
from colored import fg, bg, attr
color = fg('green')
reset = attr('reset')
try:
file1 = open('media-mail-header.txt', 'r')
print(' ')
print (color + file1.read() + reset)
file1.close()
except IOError:
print('\nBanner File not found!')
userid = input("\nEnter your G-Mail id :\t")
passwd = getpass.getpass("\nEnter your Password :\t")
target = input("\nEnter target G-Mail id :\t")
subj = input("\nEnter the Subject (optional) :\n\n")
body = input("\nEnter the Message :\n\n")
msg = EmailMessage()
msg["Subject"] = subj
msg["From"] = userid
msg["To"] = target
msg.set_content(body)
media = input("\nDo you want to attach any Media File (y/n)?\n\n")
if(media == "y" or media == "Y"):
choice = int(input("\nEnter Media File type :\n\nPress 1 for IMG file.\nPress 2 for PDF file.\nPress 3 for HTML file.\n\n"))
if (choice == 1):
n = int(input("\nEnter no. of IMG file(s) you want to attach :\t"))
for i in range (0,n):
img = input("\nEnter IMG filepath no." + str(i+1) + " :\t")
try:
f = open(img, "rb")
except:
print("\nNo file found!!!\n")
sys.exit(0)
with open(img, "rb") as f:
file_data = f.read()
file_type = imghdr.what(f.name)
file_name = f.name
msg.add_attachment(file_data, maintype="image", subtype=file_type, filename=file_name)
print("\nAttached " + str(i+1) + " IMG file(S)!!!\n")
print("\nPlease Wait!!!\n")
elif (choice == 2):
n = int(input("\nEnter no. of PDF file(s) you want to attach :\t"))
for i in range (0,n):
pdf = input("\nEnter PDF filepath no." + str(i+1) + " :\t")
try:
f = open(pdf, "rb")
except:
print("\nNo file found!!!\n")
sys.exit(0)
with open(pdf, "rb") as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype="application", subtype="octet-stream", filename=file_name)
print("\nAttached " + str(i+1) + " PDF file(s)!!!\n")
print("\nPlease Wait!!!\n")
else :
print("\nWrong choice!!!\n")
else:
print("\nNo Media File attached!!!\n")
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
try:
smtp.login(userid, passwd)
except smtplib.SMTPAuthenticationError:
print("\nYour G-Mail id or Password maybe incorrect!!!\n")
print("\nOr maybe you have disabled Less-Secure-Apps on your G-Mail account!!!\n")
resp = int(input("\nEnter 1 to enable Less-Secure-Apps now or Enter 0 to ignore :\n\n"))
if (resp == 1):
webbrowser.open('http://myaccount.google.com/lesssecureapps', new=2)
sys.exit()
try:
smtp.send_message(msg)
print ("\nSuccessfully sent Mail!!!\n")
except KeyboardInterrupt:
print ("\nCanceled!!!\n")
sys.exit()
except:
print ("\nFailed to Send!!!\n")
server.close()