forked from MakersTeam/Edison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendSimpleText.py
143 lines (112 loc) · 3.63 KB
/
SendSimpleText.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
#!/usr/bin/env python
"""
SendSimpleText.py
This example shows how to send a simple text by email.
This example code is in the public domain.
Revision History
----------------------------------------------------------
Author Date Description
----------------------------------------------------------
Diego Villalobos 01-29-2015 Example created
Diego Villalobos 06-09-2015 Minor details added
"""
# Libraries required
import os
import smtplib
from email.mime.text import MIMEText
import getpass
# Color class for output messages
class text_colors:
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
END = '\033[0m'
#Set the local host to use
"""
If you want to send emails from a Google account
use as local host: smtp.gmail.com
If you want you send emails from a Hotmail account
use as local host: smtp.live.com
The local host will be selected properly once the user
enter the sender email.
"""
localHost = ""
def enter_credentials():
print ""
print text_colors.GREEN + "Provide the sender (password) and receiver email address." + text_colors.END
print ""
print text_colors.YELLOW + "Note: Currently the email can be sent only from gmail and hotmail accounts." + text_colors.END
while True:
print ""
emailSender = raw_input("Email sender: ")
if "@gmail." in emailSender:
localHost = "smtp.gmail.com"
break
elif "@hotmail." in emailSender or "@live." in emailSender:
localHost = "smtp.live.com"
break
else:
print ""
print text_colors.RED + "Error: The email entered is not validated. It has to be a gmail or hotmail account." + text_colors.END
password = getpass.getpass(prompt="Password: ")
emailReceiver = raw_input("Email receiver: ")
return emailSender, password, emailReceiver, localHost
def build_message():
print ""
print text_colors.GREEN + "Build the email's content." + text_colors.END
print ""
emailSubject = raw_input("Email subject: ")
emailContent = raw_input("Email content: ")
return MIMEText(emailContent), emailSubject
def send_email():
global emailSender, password, emailReceiver, localHost, message
# Try to connect to the SMTP Server
try:
print ""
print text_colors.GREEN + "Connecting to SMTP Server..." + text_colors.END
mailServer = smtplib.SMTP(localHost,587)
mailServer.starttls()
mailServer.login(emailSender,password)
except smtplib.SMTPAuthenticationError:
print ""
print text_colors.RED + "Authentication Error. Can't login." + text_colors.END
mailServer.quit()
exit()
except:
print ""
print text_colors.RED + "Error. Can't connect to the SMTP Server." + text_colors.END
exit()
else:
print ""
print text_colors.GREEN + "Login successfully." + text_colors.END
# Try to send the email
try:
print ""
print text_colors.GREEN + "Sending email..." + text_colors.END
mailServer.sendmail(emailSender,emailReceiver,message.as_string())
except:
print ""
print text_colors.RED + "Error. The email couldn't be sent." + text_colors.END
else:
print ""
print text_colors.GREEN + "Email sent successfully." + text_colors.END
finally:
mailServer.quit()
exit()
def main():
global emailSender, password, emailReceiver, localHost, message
os.system("clear")
print text_colors.CYAN + "SendSimpleText.py" + text_colors.END
# Asks for the credentials prior to send the email
emailSender, password, emailReceiver, localHost = enter_credentials()
# Asks for the email's content
message, message['Subject'] = build_message()
message['From'] = emailSender
message['To'] = emailReceiver
# Sends the email
send_email()
if __name__ == "__main__":
main()