【发布时间】:2019-08-07 14:27:48
【问题描述】:
我正在尝试将基本的邮件发送方 sendgrid 功能连接到我的程序管道。问题是当我从终端执行必要的命令时,它可以正常工作:
echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
python3 sendgrid_mail.py
但是当我尝试从 PyCharm 运行它时,它给了我 HTTP 错误 401:未经授权 错误。
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
def mail_sender():
message = Mail(
from_email='from_mail',
to_emails='to_mail',
subject='hello pycharm',
html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
sg = SendGridAPIClient(os.environ.get('my_api_key'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e)
mail_sender()
【问题讨论】:
-
你在PyCharm任务配置中设置环境变量了吗?
-
是的,确实解决了这个问题。谢谢
标签: python terminal pycharm sendgrid