sendgrid API 发送邮件提示 AttributeError: 'str' object has no attribute 'get'


#1

代码(python3.7)

import sendgrid
import os
from sendgrid.helpers.mail import Email, Mail, Content


sg = sendgrid.SendGridAPIClient(apikey=os.getenv('API_KEY'))

from_mail = Email('noreply@sendmail.com')
to_mail = Email('nobody@example.com')
subject = '测试一下sendgrid_API邮件发送'
content = Content('text/plain', '这是一封测试邮件,不要回复哦!')

mail = Mail(from_mail, to_mail, subject, content)
response = sg.client.mail.send.post(rerquest_body=mail.get())

print(response.status_code)
print(response.body)
print(response.headers)

错误提示(这段代码就是照着书抄下来的啊):

AttributeError: 'str' object has no attribute 'get'

#2

subject 和 to_mail的位置调转


#3

谢谢你的回复,我改了位置之后,又出现错误了!
python_http_client.exceptions.UnauthorizedError: HTTP Error 401: Unauthorized


#4

按照书上说的,注册相应的账号,获取apikeys,然后填写到环境变量里


#5

已经有了api_key了


#6

我知道了,我原来是用另一个文件测试的,没在flask app 中测试,所以去不到 .env 的变量!
谢谢啊!