python代码实现邮件群发工资条
时间: 2024-02-09 22:06:17 浏览: 161
要实现Python代码实现邮件群发工资条,你可以使用Python的smtplib和email库来发送邮件。下面是一个简单的示例代码:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 邮件服务器的配置信息
smtp_server = "smtp.example.com"
smtp_port = 587
smtp_username = "your_username"
smtp_password = "your_password"
# 发件人和收件人信息
sender = "sender@example.com"
recipients = ["recipient1@example.com", "recipient2@example.com"]
# 邮件内容
subject = "工资条"
body = "附件中是本月的工资条,请查收。"
# 创建邮件对象
msg = MIMEMultipart()
msg["From"] = sender
msg["To"] = ", ".join(recipients)
msg["Subject"] = subject
# 添加邮件正文
msg.attach(MIMEText(body, "plain"))
# 添加附件
attachment_path = "path_to_attachment.pdf"
with open(attachment_path, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
part.add_header("Content-Disposition", f"attachment; filename= {attachment_path}")
msg.attach(part)
# 发送邮件
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
server.send_message(msg)
print("邮件发送成功!")
```
请注意,你需要将代码中的以下信息替换为你自己的信息:
- 邮件服务器的配置信息(smtp_server、smtp_port、smtp_username、smtp_password)
- 发件人和收件人信息(sender、recipients)
- 邮件内容(subject、body)
- 附件路径(attachment_path)
这是一个简单的示例,你可以根据自己的需求进行修改和扩展。例如,你可以使用循环来发送多个工资条附件,或者从文件中读取收件人列表。
阅读全文