/
sendmail.py
185 lines (154 loc) · 7.05 KB
/
sendmail.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
172
173
174
175
176
177
178
179
180
181
182
183
184
import html
import mimetypes
from email.headerregistry import Address
from email.message import EmailMessage
from email.utils import make_msgid
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from pathlib import Path
import smtplib
import getpass
import argparse
import paste
import re
from PIL import Image
class SMTPWrapper():
def __init__(self):
self.smtp = self.initSMTP("gbc.life.tw@gmail.com")
# seems the most pleasant size within an email
# not too big, not too small
self.max_long_size = 560
def initSMTP(self, sender):
smtp = smtplib.SMTP("smtp.gmail.com:587")
smtp.ehlo()
smtp.starttls()
password = getpass.getpass("Please input your password:")
smtp.login(sender, password)
return smtp
def sendMsg(self, msg):
self.smtp.send_message(msg)
def dump_greeting_image(self, name, greeting_image_path):
paste.paste(name, greeting_image_path)
def get_image_size(self, gathering_image_path):
assert isinstance(gathering_image_path, Path)
width, height = Image.open(str(gathering_image_path)).size
long_size = max(width, height)
ratio = 1.
if long_size > self.max_long_size:
ratio = self.max_long_size / long_size
width = int(width * ratio)
height = int(height * ratio)
return width, height
def create_message(self, name, greeting_image_path, dest_email):
gmail_user = "gbc.life.tw@gmail.com"
title = name
path = Path(greeting_image_path)
assert(path.exists())
me = Address("life gbc", *gmail_user.rsplit('@', 1))
msg = EmailMessage()
msg['Subject'] = '懷恩堂生命助道會歡迎你!'
msg['From'] = me
msg['To'] = [dest_email]
msg.set_content('[image: {title}]'.format(title=title)) # text/plain
cid = make_msgid()[1:-1] # strip <>
msg.add_alternative( # text/html
'<img src="cid:{cid}" alt="{alt}"/>'
.format(cid=cid, alt=html.escape(title, quote=True)),
subtype='html')
maintype, subtype = mimetypes.guess_type(str(path))[0].split('/', 1)
msg.get_payload()[1].add_related( # image/png
path.read_bytes(), maintype, subtype, cid="<{cid}>".format(cid=cid))
# save to disk a local copy of the message
Path('outgoing.msg').write_bytes(bytes(msg))
return msg
def create_message_mime(self, name, greeting_image_path, dest_email, gathering_path, gathering_name):
gmail_user = "gbc.life.tw@gmail.com"
title = name
greeting_path = Path(greeting_image_path)
assert(greeting_path.exists())
gathering = Path(gathering_path)
msg = MIMEMultipart('related')
msg['Subject'] = '懷恩堂生命助道會歡迎你!'
msg['From'] = gmail_user
msg['To'] = dest_email
msg.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)
with open("plain.txt", "r", encoding="utf-8") as f:
plain_content = name + f.read()
plain_content = re.sub("name_placeholder", name, plain_content)
plain_content = re.sub("gathering1_placeholder", gathering_name, plain_content)
msgText = MIMEText(plain_content)
msgAlternative.attach(msgText)
# https://gist.github.com/jossef/2a4a46a899820d5d57b4
with open("content.txt", "r", encoding="utf-8") as f:
content = "<div dir=\"ltr\">" + name + f.read().strip() + "</div>"
# dynamically decide the gathering image's width and height
width, height = self.get_image_size(gathering)
content = re.sub("!gathering_width", str(width), content)
content = re.sub("!gathering_height", str(height), content)
msgText = MIMEText(content, 'html', "utf-8")
msgAlternative.attach(msgText)
title_png = title + ".png"
with greeting_path.open("rb") as f:
msgImage = MIMEImage(f.read(), name=title_png)
# https://stackoverflow.com/questions/37019708/how-can-i-customize-file-name-in-python-mimeimage
# https://docs.python.org/3.6/library/email.message.html#email.message.EmailMessage.add_header
msgImage.add_header('Content-Disposition', "attachment", filename=title_png)
msgImage.add_header('Content-ID', '<greeting>')
msg.attach(msgImage)
with gathering.open("rb") as f:
msgImage = MIMEImage(f.read(), name=gathering_name)
msgImage.add_header('Content-Disposition', "attachment", filename=gathering_name)
msgImage.add_header('Content-ID', '<gathering1>')
msg.attach(msgImage)
# https://stackoverflow.com/questions/37019708/how-can-i-customize-file-name-in-python-mimeimage
Path('outgoing.msg').write_bytes(bytes(msg))
return msg
def main(args):
smtp_wrapper = SMTPWrapper()
greeting_card_not_success = []
with open(args.name_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
splitted = line.split(",")
assert(len(splitted) == 2)
name, dest_email = splitted
name = name + "同學" if args.student else name
print("=" * 70)
print("Dumping a greeting card")
print("=" * 70)
smtp_wrapper.dump_greeting_image(name, args.output)
if args.check_greeting_card:
print("=" * 70)
print("Please check if this greeting card is ok, and then press c to continue sending.")
pil_image = Image.open(args.output)
pil_image.show()
response = input().strip()
if response != "c":
greeting_card_not_success.append(line)
continue
print("=" * 70)
print("=" * 70)
print("Creating a message")
print("=" * 70)
msg = smtp_wrapper.create_message_mime(name, args.output, dest_email, args.gathering_image_path, args.gathering_name)
print("=" * 70)
print("Sending the message")
print("=" * 70)
smtp_wrapper.sendMsg(msg)
print("=" * 70)
print("Not success list: ")
print(greeting_card_not_success)
print("=" * 70)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("name_file", type=str, help="names and their corresponding emails")
parser.add_argument("gathering_image_path", type=str, help="The gathering card")
parser.add_argument("gathering_name", type=str, help="The gathering card's filename")
parser.add_argument("output", type=str, help="Image output path")
parser.add_argument("--student", action="store_true", default=False)
parser.add_argument("--check_greeting_card", action="store_true", default=False)
args = parser.parse_args()
main(args)