Search code examples
python-2.7sendgrid

HTTPError: HTTP Error 401: Unauthorized for sendgrid integration with python


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

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

    to_email = mail.Email( "[email protected]")
    from_email = mail.Email( "[email protected]" )
    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

Solution

  • Setup Environment Variables.

    To Set up environment Variable follow the below 3 steps

    echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
    echo "sendgrid.env" >> .gitignore
    source ./sendgrid.env