欢迎光临UUpython
最大、最新、最全的Python代码收集站

多线程批量发送邮件,csv读取发送账号,支持图片附件

这段代码使用了多线程来并发发送邮件。需要注意的是,使用多线程发送邮件时,可能会受到邮件服务商的限制,因为有些服务商可能对邮件发送频率有一定的限制。

另外,请确保你的代码中的邮件账户和密码等敏感信息不会被不当地泄露。在实际使用时,建议采用更加安全的方式来管理敏感信息,比如使用配置文件或者环境变量。

最后,尽量遵循邮件服务商的使用规则,以避免被认定为滥用或垃圾邮件。

import smtplib
import csv
import threading
import argparse
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

def send_email(sender_email, sender_password, receiver_email):
    # 与SMTP服务器建立连接
    smtp_server = 'smtp.qq.com'
    server_port = 587
    server = smtplib.SMTP(smtp_server, server_port)
    server.starttls()

    try:
        # 登录邮箱账号
        server.login(sender_email, sender_password)
        # 构造邮件内容
        msg_root = MIMEMultipart('related')
        msg_root['Subject'] = '测试一下'
        msg_root['From'] = sender_email
        msg_root['To'] = receiver_email

        subject = '测试一下111111'
        message = '''
        <html>
          <body>
            <p><img src="cid:image1" alt="升学教育图片" style="width:300px;height:200px;"></p>
            <p>班上新来一位学生,名字叫“马騳骉”。</p>
            <p>开学点名了,班主任不知怎么念,就说 : 马叉叉到了没?</p>
            <p>语文老师有点文学素养,点名道:万马奔腾到了没?</p>
            
          </body>
        </html>
        '''
        # 添加文本内容
        html_part = MIMEText(message, 'html')
        msg_root.attach(html_part)

        # 添加图片附件
        with open('F:/data/png/pexels-max-andrey-1366630.jpg', 'rb') as image_file:
            image_part = MIMEImage(image_file.read())
            image_part.add_header('Content-Disposition', 'attachment; filename=1366630.jpg')
            image_part.add_header('Content-ID', '<image1>')
            msg_root.attach(image_part)

        # 添加附件#
        with open('F:/data/Jmeter弱网测试.pdf', 'rb') as file:
            attachment_part = MIMEApplication(file.read(), Name='弱网测试.pdf')
            attachment_part.add_header('Content-Disposition', 'attachment; filename=弱网测试.pdf')
            msg_root.attach(attachment_part)

        # 发送邮件
        server.sendmail(sender_email, receiver_email, msg_root.as_string())
        print(f"邮件发送成功: {receiver_email}")
    except smtplib.SMTPException as e:
        print(f"邮件发送失败: {receiver_email},错误信息: {str(e)}")

    # 关闭连接
    server.quit()

def send_emails(sender_email, sender_password, thread_count):
    # 读取CSV文件并获取收件人邮箱列表
    receiver_emails = []
    with open('E:/pythonProject/test/file/email.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            receiver_emails.append(row[0])

    # 创建线程列表
    threads = []

    # 创建发送邮件的函数
    def send_email_thread(receiver_email):
        send_email(sender_email, sender_password, receiver_email)

    # 创建并启动发送邮件的线程
    for receiver_email in receiver_emails:
        t = threading.Thread(target=send_email_thread, args=(receiver_email,))
        threads.append(t)
        t.start()

        # 控制最大线程数
        if len(threads) >= thread_count:
            for thread in threads:
                thread.join()
            threads = []

    # 等待剩余线程完成
    for thread in threads:
        thread.join()

def main():
    # 解析命令行参数
    parser = argparse.ArgumentParser(description='发送邮件')
    parser.add_argument('-s', '--sender-email', required=True, help='发件人邮箱')
    parser.add_argument('-p', '--sender-password', required=True, help='发件人密码')
    parser.add_argument('-t', '--thread-count', default=5, type=int, help='发送线程数')
    args = parser.parse_args()

    # 调用发送邮件函数
    send_emails(args.sender_email, args.sender_password, args.thread_count)

if __name__ == '__main__':
    main()
赞(0) 打赏
未经允许不得转载:UUpython » 多线程批量发送邮件,csv读取发送账号,支持图片附件
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!