forked from Py-Contributors/awesomeScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_sending_script.py
More file actions
95 lines (90 loc) · 3.7 KB
/
email_sending_script.py
File metadata and controls
95 lines (90 loc) · 3.7 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
import ssl
import smtplib
import mimetypes
import re
import getpass
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
PORT = 465
def check_email(email):
regex = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
if(re.search(regex, email.strip())):
return True
else:
return False
print("Welcome to Email sending script!\n \
Read the README.md file for more details\n\n")
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", PORT, context=context) as server:
while True:
# checking sender's email and exit from middle of program
senders_email = input("Enter the sender's \
gmail email('' to exit): ").lower()
if senders_email == '':
break
while not check_email(senders_email):
senders_email = input("Enter the sender's gmail email \
correctly('' to exit): ").lower()
password = getpass.getpass(prompt="Enter the serder's \
gmail password: ")
try:
server.login(senders_email, password)
print("Login sucessfull!\n\n")
except Exception:
print("Login was unsucessfull! Try again! \n\n")
continue
recivers_email = input("Enter the reciever(s) email. \
Seperate be comma(',')('' to exit): ").lower()
if recivers_email == '':
break
while not all(map(lambda x: check_email(x),
recivers_email.split(","))):
recivers_email = input("Enter the reciever(s) email correctly. \
Seperate be comma(',')('' to exit): ")
subject = input("Enter e-mail subject: ")
message = input("Enter e-mail body: ")
msg = MIMEMultipart()
msg["Subject"] = subject
msg["To"] = recivers_email
msg["From"] = senders_email
msg.attach(MIMEText(message, "plain"))
Attachement = input("\nDo you want to add attachement?(Y/N): ")
Attachement = Attachement.strip().lower()
if Attachement == "y":
numberOfAttachment = input("How many attachement to add:")
numberOfAttachment = int(numberOfAttachment)
for i in range(numberOfAttachment):
path = input(f"Enter absolute path to the attachement {i+1}: ")
filename = ""
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
if maintype == "text":
fb = open(path)
files = MIMEText(fb.read(), _subtype=subtype)
filename = fb.name
fb.close()
elif maintype == "image" or maintype == "audio":
fb = open(path, "rb")
files = MIMEImage(fb.read(), _subtype=subtype)
filename = fb.name
fb.close()
else:
fp = open(path, "rb")
files = MIMEBase(maintype, subtype)
files.set_payload(fp.read())
filename = fp.name
fp.close()
encoders.encode_base64(files)
files.add_header("Content-Disposition",
"attachment", filename=filename)
msg.attach(files)
server.send_message(msg)
print("Email sent!\n\n")
choice = input("Send another email?(y/n): ").strip().lower()
if choice != 'y':
break