python - 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
我试过什么?:
- 在执行每封邮件而不是每批邮件后休眠。
- 调试代码,循环逻辑按预期工作。
解决方案
推荐阅读
- ios - 如何从 UITableViewCell 中加载 UITableView?
- ruby-on-rails - 将 ActiveStorage 资产从一个 S3 存储桶迁移到另一个
- ios - UIActivityViewController 将图像保存到照片库,但使用 false 调用其完成处理程序
- r - 累计值 CI
- android - 如何在 jetpack compose 中传递来自 Firestore 的查询的参数
- python - VSCode for Python 中未解决的导入
- powerbi-embedded - PowerBi EmbedToken 与任何工作区上的报告标识一起使用
- java - 如何根据传递给方法的参数从 Aspect 执行 @Before
- python - 如何使用熊猫数据框进行此列时间行操作?
- powerbi - DAX 使用基于另一个布尔列的值进行除法