/
SendHTMLText.py
171 lines (136 loc) · 4.5 KB
/
SendHTMLText.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
"""
SendHTMLText.py
This example shows how to send a text in HTML and plain format 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-10-2015 Minor details added
"""
# Libraries required
import os
import smtplib
from email.mime.multipart import MIMEMultipart
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(plain):
print ""
print text_colors.GREEN + "Build the email's content." + text_colors.END
print ""
emailSubject = raw_input("Email subject: ")
print "Email content: " + text_colors.BLUE + plain + text_colors.END
print ""
print text_colors.YELLOW + "NOTE: The message content has been added automatically. The text can be modified directly in the script." + text_colors.END
return 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 + "SendHTMLText.py" + text_colors.END
#
# Create the content in HTML and plain text
#
plain = "Hi!\nThis is an email sent by Edison.\nHere is an interesting link that you can visit:\nhttps://communities.intel.com/community/tech/edison/"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
This is an email sent by Edison.<br>
Here is an interesting <a href="https://communities.intel.com/community/tech/edison/">link</a> that you can visit.
</p>
</body>
</html>
"""
# Asks for the credentials prior to send the email
emailSender, password, emailReceiver, localHost = enter_credentials()
# Asks for the email's content
message = MIMEMultipart()
message['Subject'] = build_message(plain)
message['From'] = emailSender
message['To'] = emailReceiver
# Attach both types of text to the message
plainText = MIMEText(plain, "plain")
htmlText = MIMEText(html, "html")
message.attach(plainText)
message.attach(htmlText)
# Sends the email
send_email()
if __name__ == "__main__":
main()