Python Sendgrid示例不起作用,如何向多个收件人发送电子邮件?
我已经按照代码的要求创建了一个API密钥,并将其添加到环境中。Python Sendgrid示例不起作用,如何向多个收件人发送电子邮件?,python,sendgrid,Python,Sendgrid,我已经按照代码的要求创建了一个API密钥,并将其添加到环境中。 下面是我正在使用的代码,并遵循了提供的步骤 有没有关于我做错了什么或错过了什么的想法 另外,to_emails只有一个电子邮件地址,如何附加多个收件人?为API密钥提供完全访问权限,请执行以下步骤: 背景 API密钥 编辑API密钥 完全访问 更新 将您的域列入白名单,请执行以下步骤: 背景 发送方身份验证 域身份验证 选择DNS主机 输入您的域名 复制所有记录并将它们放在高级DNS管理控制台中 注意:添加记录时,请确保主机中没有
下面是我正在使用的代码,并遵循了提供的步骤 有没有关于我做错了什么或错过了什么的想法
另外,
to_emails
只有一个电子邮件地址,如何附加多个收件人?为API密钥提供完全访问权限,请执行以下步骤:
将您的域列入白名单,请执行以下步骤:
如果您使用的是pylinter,
e.message
会说
Instance of 'Exception' has no 'message' member
这是因为message
属性是由sendgrid
动态生成的,pylinter无法访问该属性,因为它在运行前不存在
因此,为了防止这种情况发生,在文件顶部或上面的print(e.message)
行中,您需要添加以下任意一项,它们的含义相同-
# pylint: disable=no-member
E1101编码为无成员
,更精细
现在,下面的代码应该适用于您。只需确保在环境中设置了
SENDGRID\u API\u键即可。如果没有,您也可以直接将其替换为os.environ.get(“SENDGRID\u API\u KEY”)
,但这不是一个好的做法
# pylint: disable=E1101
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email="from_email@your-whitelisted-domain.com",
to_emails=("recipient1@example.com", "recipient2@example.com"),
subject="Sending with Twilio SendGrid is Fun",
html_content="<strong>and easy to do anywhere, even with Python</strong>")
try:
sg = SendGridAPIClient(os.environ.get("SENDGRID_API_KEY"))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
哇,这正是我想要的完美答案。谢谢
Instance of 'Exception' has no 'message' member
# pylint: disable=no-member
# pylint: disable=E1101
# pylint: disable=E1101
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email="from_email@your-whitelisted-domain.com",
to_emails=("recipient1@example.com", "recipient2@example.com"),
subject="Sending with Twilio SendGrid is Fun",
html_content="<strong>and easy to do anywhere, even with Python</strong>")
try:
sg = SendGridAPIClient(os.environ.get("SENDGRID_API_KEY"))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
to_emails=("recipient1@example.com", "recipient2@example.com"),