generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_email.py
119 lines (93 loc) · 3.12 KB
/
send_email.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
import ssl
import smtplib
from environs import Env
from time import sleep
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from pyisemail import is_email
# Create an Env object
env = Env()
env.read_env()
host = env("HOST")
port = env("PORT_GMAIL")
sender_email = env("EMAIL_ADDRESS")
password = env("PASSWORD")
def validate_email_address(email_address):
"""
This function validates the email address
passed to it.
A valid input should be like [email protected]
"""
try:
# Check if the email address is valid using the is_email function
is_a_valid_email = is_email(email_address, check_dns=True)
return is_a_valid_email
except ValueError as err:
print(str(err))
return False
def send_email(receiver_email):
"""
This function sends an email from the email
address specified in the config file to the
email address specified in the function call.
The message will be the conversation log.
"""
message = MIMEMultipart()
message["Subject"] = "Your conversation log"
message["From"] = "Python Bot"
message["To"] = receiver_email
# Create the plain-text version of message
body = """
Hi!
How are you doing today?
Thanks for using our service.
Please see attached file for the above mentioned subject.
Yours sincerely,
Python Bot
"""
# Add body to message
message.attach(MIMEText(body, "plain"))
# Create a secure SSL context
context = ssl.create_default_context()
file = "log.txt"
with open(file, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read(), "base64")
# Add headers as key/value pairs to attachment part
part.add_header("Content-Disposition", f"attachment; filename= {file}")
message.attach(part)
is_a_valid_email = validate_email_address(receiver_email)
if is_a_valid_email:
try:
# try to create a secure SSL context
with smtplib.SMTP_SSL(host, port, context=context) as server:
server.login(sender_email, password=password)
server.sendmail(
to_addrs=receiver_email,
from_addr=sender_email,
msg=message.as_string(),
)
print("\nOne moment, please.")
sleep(3)
print("\nSending email...\n")
sleep(5)
server.quit()
except Exception:
# if the email address is invalid or the server is down
# print an error message and exit the program
print(
"\nCouldn't connect. There may be a problem with "
"the server. Please try again later."
)
return False
else:
# Wait for 3 seconds and then print the error message to the user
# if the email address is invalid
sleep(3)
print(
"Sorry. It appears that you have not entered "
"your email address correctly.\n"
)
return False
return True