Python 批量发送邮件

Python实现批量邮件发送

批量发送邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import ssl
import time


smtp_server = "example.test.com"
port = 587# For starttls (consider using 587)
sender_email = "example@test.com"
password = "xxx"
# WARNING: SSL verification is being DISABLED below.
# This is INSECURE and should only be used for local testing.
context = ssl._create_unverified_context()
print("WARNING: SSL certificate verification is DISABLED (INSECURE).")
try:
   server = smtplib.SMTP(smtp_server, port)
   server.ehlo() # 可以省略
   # Start TLS using an unverified (insecure) context
   server.starttls(context=context) # INSECURE: certificate not verified
   server.ehlo() # 可以省略
   server.login(sender_email, password)
   print("登录成功")
   
   # Create the email message
   message = MIMEMultipart("alternative")
   message["Subject"] = "测试主题"
   message["From"] = sender_email
   # message["To"] = "example@test.com"
   text = "test"
   html = """
   <html>
   <body>
      <p>test<br>
         test!
      </p>
   </body>
   </html>
   """
   part1 = MIMEText(text, "plain")
   part2 = MIMEText(html, "html")
   message.attach(part1)
   message.attach(part2)
   
   # Send the email to multiple recipients with a small delay
   recipients = ["example@test.com", "example@test.com"]
   for recipient in recipients:
      message["To"] = recipient
      try:
         server.sendmail(sender_email, recipient, message.as_string())
         print(f"邮件已发送至: {recipient}")
         time.sleep(2) # 适当的时间间隔
      except Exception as e:
         print(f"发送邮件到 {recipient} 时出错: {e}")
except Exception as e:
   print(f"错误: {e}")
finally:
   # 仅在连接仍然存在时调用 quit()
   try:
      if 'server' in locals() and getattr(server, 'sock', None):
         server.quit()
   except Exception:
      pass

发送附件邮件

from email.mime.base import MIMEBase
from email import encoders
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = "receiver@example.com"
message["Subject"] = "带附件的邮件"
body = "这是一个带附件的邮件"
message.attach(MIMEText(body, "plain"))
filename = "document.pdf"
with open(filename, "rb") as attachment:
   part = MIMEBase("application", "octet-stream")
   part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
   "Content-Disposition",
   f"attachment; filename= {filename}",
)
message.attach(part)

使用Python实现批量邮件可以借助`smtplib`和`email`模块,下面结合引用内容说明一种实现方式。 可以通过Python3结合Excel实现自动批量发送邮件功能。在之前文章《Python3实现自动定时发送邮件功能》中提到利用QQ邮箱自动给指定邮箱地址定时定点发送邮件,但未涉及操作Excel功能。这里有一个带有Excel操作类的示例,通过多线程实现同时向500个不同邮箱地址点对点单一发送相同的带有附件的邮件,只需在终端下执行`python auto_send_email.py`命令即可完成操作,发完500封邮件大概耗时几分钟 [^1]。 以下是一个简单的Python代码示例用于批量发送邮件: ```python import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication # 配置发件人信息 sender_email = "your_email@example.com" sender_password = "your_email_password" # 配置收件人列表 receiver_emails = ["recipient1@example.com", "recipient2@example.com"] # 配置邮件主题和内容 subject = "Test Email" body = "This is a test email sent from Python." # 创建邮件对象 message = MIMEMultipart() message["From"] = sender_email message["Subject"] = subject # 添加邮件正文 message.attach(MIMEText(body, "plain")) # 添加附件(可选) # with open("attachment.pdf", "rb") as file: # part = MIMEApplication(file.read(), Name="attachment.pdf") # part['Content-Disposition'] = f'attachment; filename="attachment.pdf"' # message.attach(part) # 连接到SMTP服务器并发送邮件 try: server = smtplib.SMTP("smtp.example.com", 587) server.starttls() server.login(sender_email, sender_password) for receiver_email in receiver_emails: message["To"] = receiver_email text = message.as_string() server.sendmail(sender_email, receiver_email, text) print(f"Email sent to {receiver_email}") server.quit() print("All emails sent successfully.") except Exception as e: print(f"Error: {e}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值