-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_mail_sender.py
More file actions
87 lines (70 loc) · 1.81 KB
/
Copy pathhtml_mail_sender.py
File metadata and controls
87 lines (70 loc) · 1.81 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
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
#!/usr/bin/python3
import base64
import smtplib, ssl
from getpass import getpass
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import email.mime, email.mime.nonmultipart, email.charset
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
# password = getpass("Type your password and press enter: ")
# Create a secure SSL context
context = ssl.create_default_context()
sender_email="sham72942@gmail.com"
receiver_email="sham72942@gmail.com"
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>The th element</h1>
<p>The th element defines a header cell in a table:</p>
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
</body>
</html>"""
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email
# Turn these into plain/html MIMEText objects
cs=email.charset.Charset('UTF-8')
cs.body_encoding = email.charset.BASE64
part1=email.mime.nonmultipart.MIMENonMultipart('text', 'plain')
part1.set_payload(text, charset=cs)
part2=email.mime.nonmultipart.MIMENonMultipart('text', 'html')
part2.set_payload(html, charset=cs)
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part1)
message.attach(part2)
print(message)
# with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
# server.login(sender_email, password)
# server.sendmail(sender_email, receiver_email, message.as_string())
# server.quit()
# print("mail_sent")