Python smtplib批量发送邮件
时间: 2023-11-19 12:50:20 浏览: 182
好的,关于使用Python的smtplib模块来批量发送邮件,你可以按照以下步骤进行操作:
1. 导入必要的模块:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
```
2. 设置发件人和收件人信息:
```python
sender_email = "your_email@example.com"
password = "your_password"
receivers = ["receiver1@example.com", "receiver2@example.com"]
```
3. 创建包含邮件内容的MIMEMultipart对象:
```python
message = MIMEMultipart("alternative")
message["Subject"] = "邮件主题"
message["From"] = sender_email
```
4. 编写邮件内容,并将内容添加到MIMEMultipart对象中:
```python
text = "这是纯文本内容"
html = "<p>这是HTML内容</p>"
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
```
5. 连接SMTP服务器并登录:
```python
smtp_server = "smtp.example.com"
smtp_port = 587
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, password)
```
6. 发送邮件给每个收件人:
```python
for receiver in receivers:
message["To"] = receiver
server.sendmail(sender_email, receiver, message.as_string())
```
7. 断开与SMTP服务器的连接:
```python
server.quit()
```
这样就可以使用smtplib模块来批量发送邮件了。
阅读全文
相关推荐






