HTTPError:HTTP 错误 401:未经授权将 sendgrid 与 python 集成

vin*_*nay 5 python-2.7 sendgrid

def sendEmail(to,apNumber,paperType,zipedFile):

    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get("API-KEY"))

    to_email = mail.Email( "to@email.com")
    from_email = mail.Email( "from@email.com" )
    subject = 'This is a test email'
    content = mail.Content('text/plain', 'Example message.')
    message = mail.Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body = message.get())
    return response
Run Code Online (Sandbox Code Playgroud)

小智 5

设置环境变量。

要设置环境变量,请按照以下 3 个步骤操作

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
Run Code Online (Sandbox Code Playgroud)