下面是Python自动发邮件、读取数据库数据生成列表、填写在邮件正文表格中的示例代码:
```python
# 导入相关库
import smtplib
import pymysql.cursors
# Connect to the database
connection=pymysql.connect(host='localhost', user='user', password='password', db='database_name', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
# 从数据库中读取数据
with connection.cursor() as cursor:
# 从表中读取数据
sql="SELECT * FROM `simple_table`"
cursor.execute(sql)
result=cursor.fetchall()
# 生成邮件内容
table_html="<table><tr><th>ID</th><th>Name</th><th>Age</th></tr>"
for row in result:
table_html +="<tr><td>{}</td><td>{}</td><td>{}</td></tr>".format(row['id'], row['name'], row['age'])
table_html +="</table>"
message="""\
<html>
<body>
<p>Dear Sir/Madam,</p>
<p>Please find attached the list of data:</p>
{}
<p>Best regards,</p>
<p>Your Name</p>
</body>
</html>
""".format(table_html)
# 发送邮件
server=smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("your_email@gmail.com", "your_password")
server.sendmail("your_email@gmail.com", "recipient_email@example.com", message)
server.quit()
```
这个代码可以实现从数据库中读取数据,生成一个 HTML 表格,并将其填写在邮件正文中,发送到指定的收件人。需要注意的是,这只是示例代码,需要根据实际情况进行修改。如果遇到了问题,可以在评论中留言,我会尽力帮助你解决问题。