-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.py
More file actions
75 lines (65 loc) · 2.62 KB
/
Copy pathsend.py
File metadata and controls
75 lines (65 loc) · 2.62 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
"""
Example: sending example 05's digest email via SMTP.
Run `python3 run_all.py` first (or at least
`python3 examples/05-plain-text/run.py`) so dist/05-plain-text/digest.html
and digest.txt exist.
Uses Python's built-in smtplib and email modules -- nothing extra to
install for this example. For ESP-specific sending, consider:
- SendGrid: pip install sendgrid
- Amazon SES: pip install boto3
- Postmark: pip install postmarker
- Mailgun: pip install requests (use its REST API)
"""
import os
import secrets
import sys
# import smtplib
# from email.mime.multipart import MIMEMultipart
# from email.mime.text import MIMEText
ROOT = os.path.dirname(os.path.abspath(__file__))
html_path = os.path.join(ROOT, "dist", "05-plain-text", "digest.html")
text_path = os.path.join(ROOT, "dist", "05-plain-text", "digest.txt")
if not os.path.isfile(html_path) or not os.path.isfile(text_path):
print("Missing dist/05-plain-text/digest.{html,txt} -- run `python3 run_all.py` first.", file=sys.stderr)
sys.exit(1)
with open(html_path, encoding="utf-8") as f:
html = f.read()
with open(text_path, encoding="utf-8") as f:
text = f.read()
# msg = MIMEMultipart("alternative")
# msg["Subject"] = "This week at Northwind Coffee"
# msg["From"] = "Northwind Coffee <digest@northwindcoffee.example>"
# msg["To"] = "subscriber@example.com"
# msg.attach(MIMEText(text, "plain")) # least capable part first
# msg.attach(MIMEText(html, "html")) # most capable part second
#
# with smtplib.SMTP("smtp.example.com", 587) as server:
# server.starttls()
# server.login("your-username", "your-password")
# server.send_message(msg)
# print("sent")
# What MIMEMultipart builds under the hood, spelled out by hand: a
# multipart/alternative message with the plain-text part first (least
# capable first) and the HTML part second, joined by a MIME boundary.
boundary = "nwc-" + secrets.token_hex(16)
message = (
"From: Northwind Coffee <digest@northwindcoffee.example>\r\n"
"To: subscriber@example.com\r\n"
"Subject: This week at Northwind Coffee\r\n"
"MIME-Version: 1.0\r\n"
f'Content-Type: multipart/alternative; boundary="{boundary}"\r\n'
"\r\n"
f"--{boundary}\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
f"{text}\r\n"
f"--{boundary}\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"\r\n"
f"{html}\r\n"
f"--{boundary}--\r\n"
)
print("SMTP config not set -- edit send.py with your credentials.")
print(f"HTML part: {len(html.encode('utf-8'))} bytes")
print(f"Text part: {len(text.encode('utf-8'))} bytes")
print(f"Multipart total: {len(message.encode('utf-8'))} bytes")