使用python 在邮件中怎么插入表格内容
时间: 2024-03-08 17:48:29 浏览: 14
您可以使用Python中的email和smtplib模块来发送邮件,并且使用HTML格式来插入表格内容。下面是一个示例代码,其中包括如何创建并插入表格内容:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email import encoders
# 创建邮件内容
msg = MIMEMultipart()
msg['From'] = 'sender@example.com'
msg['To'] = 'receiver@example.com'
msg['Subject'] = '邮件主题'
# 添加表格内容
html = """
<html>
<body>
<table border="1">
<tr>
<th>列1</th>
<th>列2</th>
<th>列3</th>
</tr>
<tr>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<td>内容4</td>
<td>内容5</td>
<td>内容6</td>
</tr>
</table>
</body>
</html>
"""
part = MIMEText(html, 'html')
msg.attach(part)
# 发送邮件
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('sender@example.com', 'password')
text = msg.as_string()
server.sendmail('sender@example.com', 'receiver@example.com', text)
server.quit()
```
在上面的示例代码中,我们使用了HTML格式来创建一个表格,然后使用MIMEText将其添加到邮件内容中。您可以根据需要修改表格的内容和样式。