微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。