I have a sendgrid account set up and I can use it to send emails through python. When I run it in my spyder IDE it works great, but when run through the windows CMD i get <urlopen error unknown url type: https>
.
I tried hard coding the host as sg = SendGridAPIClient(pw,host='https://api.sendgrid.com')
. This works in the IDE, but not in CMD, and I get the same error.
I also tried removing the ':' with sg = SendGridAPIClient(pw,host=urllib.parse.quote('https://api.sendgrid.com'))
. This changesthe error to unknown url type: 'https%3A//api.sendgrid.com/v3/mail/send'
This idea was based on the SO post urllib.error.URLError: <urlopen error unknown url type: 'https>.
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *
api_key='your_api_key'
message = Mail(
from_email='me@example.com',
to_emails='you@example.com',
subject='SUBJECT',
html_content='some body stuff')
try:
sg = SendGridAPIClient(api_key)
response = sg.send(message) #this line throws the error
print('worked')
except Exception as e:
print(str(e))
when I run it in the command line I use "python.exe 'my_scirpt.py'" in the directory of my script.