0

I'm trying to send an email using SendGrid within Azure Machine Learning. This is initially just a basic test email to ensure it is working correctly.

The steps I have taken:

  1. Pip installed SendGrid;
  2. Zipped the SendGrid download and uploaded as a dataset to AML platform;
  3. Attempted to run the example SendGrid Python code (which can be seen below):

I have copied steps in similar posts concerning uploading modules to AML here and here as well as ensuring the correct settings for the SendGrid API key were established on setup here.

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)

No error message is returned in the terminal. To me this indicates there weren't any issues with the code, yet no emails have been received/sent.

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$  
philnash
  • 70,667
  • 10
  • 60
  • 88
WPB
  • 13
  • 4

1 Answers1

0

Twilio SendGrid developer evangelist here.

When you run the code nothing is printed, which is concerning as neither the success or error prints are called.

If the code you have shared is the entirety of the file then I think the code is simply not running. You have defined a function but you have not called the function itself.

Try calling azureml_main() on the last line of the file:

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

azureml_main()

One other thing that concerns me is this line:

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

It looks as though you intend to actually place your SG API Key in the call to os.environ.get. Instead, that string should be the identifier for an environment variable that you have set. For example, you should set the SendGrid API key as an environment variable called SENDGRID_API_KEY and then in your code fetch the API key by calling os.environ.get("SENDGRID_API_KEY"). Check out this post on working with environment variables in Python for more.

philnash
  • 70,667
  • 10
  • 60
  • 88