-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (59 loc) · 2.21 KB
/
app.py
File metadata and controls
70 lines (59 loc) · 2.21 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
import docx
from docx2pdf import convert
import openpyxl
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 生成对应的邀请函,并转存pdf格式
def get_invitation(name):
doc = docx.Document("template.docx")
for para in doc.paragraphs:
if '<name>' in para.text:
for run in para.runs:
if '<name>' in run.text:
run.text = run.text.replace('<name>', name)
doc.save(f'./邀请函/{name}.docx')
convert(f"./邀请函/{name}.docx")
smtp = smtplib.SMTP(host="smtp.qq.com", port=587)
smtp.login('235977@qq.com', "ruybefkipoo")
def send_email(name, email):
msg = MIMEMultipart()
msg["subject"] = f"您好,{name},您的邀请函!"
msg["from"] = "2352180977@qq.com"
msg["to"] = email
html_content = f"""
<html>
<body>
<p>您好:{name}<br>
<b>欢迎加入Python进阶者学习交流群,请在附件中查收您的门票~</b><br>
点击这里了解更多:<a href="https://www.pdcfighting.com">演唱会主页</a>
</p>
</body>
</html>
"""
html_part = MIMEText(html_content, "html")
msg.attach(html_part)
with open(f"./邀请函/{name}.pdf", "rb") as f:
doc_part = MIMEApplication(f.read())
doc_part.add_header("Content-Disposition", "attachment", filename=name)
# 把附件添加到邮件中
msg.attach(doc_part)
# 发送前面准备好的邮件
smtp.send_message(msg)
# 如果放到外边登录,这里就不用退出服务器连接,所以注释掉了
# smtp.quit()
def get_username_email():
workbook = openpyxl.load_workbook("names.xlsx")
worksheet = workbook.active
for index, row in enumerate(worksheet.rows):
if index > 0:
name = row[0].value
email = row[3].value
# print(name, email)
# print(f"{name}邀请函正在生成...")
# get_invitation(name)
send_email(name, email)
if __name__ == '__main__':
get_username_email()
# get_invitation('Python进阶者')