Python群发特定附件
时间: 2024-04-12 12:25:09 浏览: 98
python批量从上市公司年报中获取指定内容.rar
5星 · 资源好评率100%
在Python中,可以使用smtplib和email库来实现群发特定附件的功能。下面是一个简单的示例代码:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
def send_email_with_attachment(sender_email, sender_password, receiver_emails, subject, message, attachment_path):
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = ', '.join(receiver_emails)
msg['Subject'] = subject
# 添加正文
msg.attach(MIMEText(message, 'plain'))
# 添加附件
with open(attachment_path, 'rb') as file:
attachment = MIMEApplication(file.read())
attachment.add_header('Content-Disposition', 'attachment', filename=attachment_path)
msg.attach(attachment)
# 发送邮件
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(msg)
# 示例用法
sender_email = 'your_email@gmail.com'
sender_password = 'your_password'
receiver_emails = ['recipient1@example.com', 'recipient2@example.com']
subject = '邮件主题'
message = '这是一封带附件的邮件'
attachment_path = 'path/to/attachment.pdf'
send_email_with_attachment(sender_email, sender_password, receiver_emails, subject, message, attachment_path)
```
请注意,上述示例使用了Gmail的SMTP服务器,如果你使用其他邮箱,请根据相应的SMTP服务器和端口进行修改。
阅读全文