用Python实现自动化群发邮件
分类:默认分类
浏览:25
2024-10-09
pip install secure-smtplib
pip install email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(subject, body, to_addrs):
from_addr = '你的邮箱地址'
password = '你的邮箱密码'
smtp_server = 'smtp.你的邮箱服务提供商.com'
smtp_port = 587
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = ", ".join(to_addrs)
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(from_addr, password)
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()
print('邮件发送成功')
except Exception as e:
print(f'邮件发送失败: {e}')
if __name__ == '__main__':
subject = '测试邮件'
body = '这是一封通过Python发送的测试邮件。'
to_addrs = ['收件人[email protected]', '收件人[email protected]']
send_email(subject, body, to_addrs)
to_addrs = ['收件人[email protected]', '收件人[email protected]', '收件人[email protected]']
然后调用send_email函数,传入邮件主题、内容和收件人列表即可。