无法通过SendGrid发送电子邮件,弹出网址错误

时间:2019-07-15 00:38:13

标签: python email url sendgrid

我在Jupyter Lab中进行测试时,我的代码可以正常工作,但是当我使用.bat对其进行自动化时,我的代码则无法工作

通过搜索错误消息找不到任何解决方案。奇怪的是我可以在Jupyter Notebook中运行它,但是当我将它放在.bat文件中并通过命令行运行时,它不起作用。

我非常确定可执行文件和python脚本的路径均正确。

import base64
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Attachment

mail = Mail(
    from_email = 'sender@email.com',
    to_emails = ['recipient@email.com', 'recipient_2@email.com'],
    subject = 'subject line',
    plain_text_content = 'message.'
)

with open('my_attachment.csv', 'rb') as file:
    data = file.read()
    file.close()

encoded = base64.b64encode(data).decode()

attachment = Attachment(
    file_content = encoded,
    file_name = 'attachment.csv',
)

sendgrid = SendGridAPIClient('my_api_key')
sendgrid.send(mail)

下面是完整的错误消息。似乎错误发生在最后一行。

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\CCP Automation V2\Python_scripts\1.3 Notify.py", line 55, in <module>
    sendgrid.send(mail)
  File "C:\ProgramData\Anaconda3\lib\site-packages\sendgrid\sendgrid.py", line 98, in send
    response = self.client.mail.send.post(request_body=message.get())
  File "C:\ProgramData\Anaconda3\lib\site-packages\python_http_client\client.py", line 252, in http_request
    return Response(self._make_request(opener, request, timeout=timeout))
  File "C:\ProgramData\Anaconda3\lib\site-packages\python_http_client\client.py", line 172, in _make_request
    return opener.open(request, timeout=timeout)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 548, in _open
    'unknown_open', req)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1387, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: https>

0 个答案:

没有答案