SendGrid多收件人邮件发送失败:现有代码需调整吗?
SendGrid多收件人邮件发送问题修复
你的代码需要修改,问题出在to_emails参数的传递方式上:当前用逗号分隔的字符串传递多个收件人,当数量达到3个及以上时,SendGrid SDK可能无法正确解析收件人地址。
正确的做法是将收件人地址以列表形式传入to_emails参数,每个邮箱地址作为独立的字符串元素。
修改后的完整代码如下:
to = ['aaa@mail.com', 'bbb@mail.com', 'ccc@mail.com'] html_content1 = ''' Sample''' message = Mail( from_email='from@mail.com', to_emails=to, subject=fl, html_content=html_content1) with open('attachment', 'rb') as f: data = f.read() encoded = base64.b64encode(data).decode() attachment = Attachment() attachment.file_content = FileContent(encoded) attachment.file_type = FileType('application/pdf') attachment.file_name = FileName(file+'.pdf') attachment.disposition = Disposition('attachment') attachment.content_id = ContentId('Example Content ID') message.attachment = attachment sendgrid_client = SendGridAPIClient('key') response = sendgrid_client.send(message) print(response.status_code) print(response.body) print(response.headers)
额外注意事项:
- 确保所有收件人邮箱地址格式合法,无拼写错误
- 如果需要区分收件人类型(抄送、密送),可以使用
personalizations字段配置,而非仅依赖to_emails
内容的提问来源于stack exchange,提问作者san1
相关产品推荐
相关产品推荐

