forked from bytesenseidk/Mixed-Applications
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmail.py
More file actions
42 lines (34 loc) · 1.63 KB
/
Email.py
File metadata and controls
42 lines (34 loc) · 1.63 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
import smtplib, ssl, getpass
class Email(object):
def __init__(self):
self.port = 465 # SSL port
self.smtp_server = "smtp.gmail.com" # smtp server address
creds = self.login_credentials() # Sender credentials
self.sender_email = creds["email"] # Senders email
self.password = creds["pass"] # Senders password
# Create a secure SSL context
self.context = ssl.create_default_context()
def login_credentials(self):
""" The senders login credentials - password is hidden. """
sender_email = str(input(r"Enter your email here: "))
password = getpass.getpass("Enter your password here: ")
return {"email":sender_email, "pass":password}
def message(self):
""" Subject and message content """
subject = str(input(r"Subject: "))
message = str(input(r"Message: "))
return str(f"Subject: {subject}\n"
f"{message}")
def send_mail(self):
""" Email control and forwarding """
receiver_email = str(input(r"Enter receivers email here: ")) # Receivers email
message = self.message()
try:
with smtplib.SMTP_SSL(self.smtp_server, self.port, context=self.context) as server:
server.login(self.sender_email, self.password)
server.sendmail(self.sender_email, receiver_email, message)
print("\nEmail successfully sent!")
except:
print("\nError occurred while trying to send the email.. Please try again.")
if __name__ == "__main__":
Email().send_mail()