Sendgrid 示例示例不起作用以及如何向多个收件人发送电子邮件?
Sendgrid sample example is not working and how to email to multiple recipients?
我已经根据代码要求创建了一个 API 密钥并将其添加到环境中。
下面是我正在使用的代码,并已按照提供的步骤进行操作 here.
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email='from_email@example.com',
to_emails='to@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)
它抛出这个错误:
Traceback (most recent call last):
File "sendgrid_email.py", line 18, in <module>
print(e.message)
AttributeError: "ForbiddenError" object has no attribute "message"
并且在 打印异常时 它显示 pylint 警告-
Instance of "Exception" has no "message" member
关于我做错了什么或遗漏了什么有什么想法吗?
此外,to_emails
只有一个电子邮件地址,我们如何附加多个收件人?
授予 API 密钥完全访问权限,请按照以下步骤操作:
- 设置
- API 键
- 编辑API键
- 完全访问权限
- 更新
将您的域列入白名单,请按照以下步骤操作:
- 设置
- 发件人身份验证
- 域认证
- Select DNS 主机
- 输入您的域名
- 复制所有记录并将它们放入您的高级 DNS 管理控制台
注意:添加记录时,请确保主机中没有域名。剪掉它。
如果您不想验证域,您也可以尝试使用单一发件人验证。
注意:记录开始运行可能需要一些时间。
如果你使用的是pyinter,e.message
会说
Instance of 'Exception' has no 'message' member
这是因为 message
属性是由 sendgrid
动态生成的,pylinter 无法访问,因为它在运行前不存在。
因此,为防止出现这种情况,在文件顶部或 print(e.message)
行上方,您需要添加以下任一内容,它们的含义相同 -
# pylint: disable=no-member
E1101 是 no-member
的代码,更好 here
# pylint: disable=E1101
现在下面的代码应该对你有用了。只需确保在环境中设置了 SENDGRID_API_KEY
。如果没有,您也可以直接将其替换为 os.environ.get("SENDGRID_API_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)
to_emails
可以接收多个收件人的元组。例如
to_emails=("recipient1@example.com", "recipient2@example.com"),
我已经根据代码要求创建了一个 API 密钥并将其添加到环境中。
下面是我正在使用的代码,并已按照提供的步骤进行操作 here.
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email='from_email@example.com',
to_emails='to@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)
它抛出这个错误:
Traceback (most recent call last):
File "sendgrid_email.py", line 18, in <module>
print(e.message)
AttributeError: "ForbiddenError" object has no attribute "message"
并且在 打印异常时 它显示 pylint 警告-
Instance of "Exception" has no "message" member
关于我做错了什么或遗漏了什么有什么想法吗?
此外,to_emails
只有一个电子邮件地址,我们如何附加多个收件人?
授予 API 密钥完全访问权限,请按照以下步骤操作:
- 设置
- API 键
- 编辑API键
- 完全访问权限
- 更新
将您的域列入白名单,请按照以下步骤操作:
- 设置
- 发件人身份验证
- 域认证
- Select DNS 主机
- 输入您的域名
- 复制所有记录并将它们放入您的高级 DNS 管理控制台
注意:添加记录时,请确保主机中没有域名。剪掉它。
如果您不想验证域,您也可以尝试使用单一发件人验证。
注意:记录开始运行可能需要一些时间。
如果你使用的是pyinter,e.message
会说
Instance of 'Exception' has no 'message' member
这是因为 message
属性是由 sendgrid
动态生成的,pylinter 无法访问,因为它在运行前不存在。
因此,为防止出现这种情况,在文件顶部或 print(e.message)
行上方,您需要添加以下任一内容,它们的含义相同 -
# pylint: disable=no-member
E1101 是 no-member
的代码,更好 here
# pylint: disable=E1101
现在下面的代码应该对你有用了。只需确保在环境中设置了 SENDGRID_API_KEY
。如果没有,您也可以直接将其替换为 os.environ.get("SENDGRID_API_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)
to_emails
可以接收多个收件人的元组。例如
to_emails=("recipient1@example.com", "recipient2@example.com"),