Gmail API 在批量发送期间发送多封邮件

问题描述

我希望使用 Gmail API 批量发送电子邮件我有一个使用 for 循环的实现,它在每次迭代中更改接收者的电子邮件,并创建一条新消息并执行服务。

问题是我观察到多封电子邮件发送到同一个邮件 ID。由于我刚开始使用 Gmail API,因此我不知道此问题的根本原因。

代码


# receiver_path contains file with emails separated by comma
f = open(receiver_path,"r")
receivers = (f.read()).split(',')

message = MIMEText(body_txt)
message['from'] = senders_email
message['subject'] = subject

while i < (len(receivers)):
    a = min(45,len(receivers)-i)
    for j in range(a):
        message['to'] = str(receivers[i + j])

        raw = base64.urlsafe_b64encode(message.as_bytes())
        raw = raw.decode()
        final_message = {'raw': raw}

        try:
            message_temp = (service.users().messages().send(userId=senders_email,body=final_message)
                            .execute())
        except BaseException as e:
            print('Could not send mail: ',e)
            print('Mail id #' + str(i + j + 1))

    sleep(1.5)

    print('Mail delivered to set ' + str(int(i / 45) + 1))
    i += 45

我尝试过什么?:

  1. 在执行完每封邮件而不是每批邮件后休眠。
  2. 调试代码,循环逻辑按预期工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)