-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancy_html_mail.py
54 lines (44 loc) · 1.42 KB
/
fancy_html_mail.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
import smtplib,ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import getpass
port = 465 #for SSL
smtp_server = "smtp.gmail.com"
sender_email = "[email protected]" #your email
reciever_email = "[email protected]" #recievers email
password = getpass.getpass(prompt='Password: ', stream=None)
#getpass() used to hide typing password in prompt
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["To"] = sender_email
message["From"] = reciever_email
#Creating a plain text and html verison of context
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\
<html>
<body>
<p>Hi,<br>
How are you?<br>
<a href="http://www.google.com">Google</a>
has many great tutorials.
</p>
</body>
</html>
"""
#Turing this plain and html messages into MIMEText objects
part1 = MIMEText(text,"plain")
part2 = MIMEText(html,"html")
# Adding the HTML/plain-text parts -> MIMEMultipart message
# The email client will first try to render the last part
message.attach(part1)
message.attach(part2)
#creating a secure SSL context
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server,port,context=context) as server:
server.login("[email protected]",password)
server.sendmail(sender_email,reciever_email,message.as_string())
# Server is automatically closed after getting out from with block