将SendGrid与Azure ML一起使用时出现问题



我正试图在Azure机器学习中使用SendGrid发送电子邮件。这最初只是一封基本的测试电子邮件,以确保其正常工作。

我采取的步骤:

  1. Pip安装SendGrid
  2. 压缩SendGrid下载并作为数据集上传到AML平台
  3. 尝试运行示例SendGrid Python代码(如下所示(:

我已经复制了类似帖子中的步骤,这些步骤涉及在此处和此处将模块上传到AML,以及确保在此处设置SendGrid API密钥的正确设置。

def azureml_main():
import sendgrid
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
from_email='xxx@xyz.com',
to_emails='xxx@xyz.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('SG API Code'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e)

终端中没有返回错误消息。对我来说,这表明代码没有任何问题,但还没有收到/发送任何电子邮件。

python ScheduleRun.py 
azureuser@will1:~/cloudfiles/code/Users/will/Schedule$ 
azureuser@will1:~/cloudfiles/code/Users/will/Schedule$ python ScheduleRun.py 
azureuser@will1:~/cloudfiles/code/Users/will/Schedule$  

Twilio SendGrid开发人员在此发布。

运行代码时,不会打印任何内容,这与调用成功或错误print无关。

如果您共享的代码是整个文件,那么我认为该代码根本没有运行。您已经定义了一个函数,但尚未调用该函数本身。

尝试在文件的最后一行调用azureml_main()

print(response.headers)
except Exception as e:
print(e)
azureml_main()

另一件让我担心的事是这条线:

os.environ.get('SG API Code')

看起来您似乎打算将SG API密钥放在对os.environ.get的调用中。相反,该字符串应该是您设置的环境变量的标识符。例如,您应该将SendGrid API键设置为名为SENDGRID_API_KEY的环境变量,然后在代码中通过调用os.environ.get("SENDGRID_API_KEY")获取API键。查看这篇关于在Python中使用环境变量的文章,了解更多信息。

最新更新