当我从批处理文件中使用 smtplib 运行脚本时,为什么我会得到“此 python 中不包含 SSL”,而当我手动运行它时却没有?

问题描述 投票:0回答:2

我可以使用 smtplib 从脚本发送电子邮件,但是当我尝试从批处理文件或任务计划程序运行它时,一切正常,除了电子邮件最后没有发送。我收到“无 SSL”错误。

我在 conda 环境中运行它,但我已经仔细检查过我是从那个环境中调用 python 的,而不是从 base 中调用的。

python 版本 3.7.3。

我从另一个脚本调用此函数并将电子邮件主题和消息传递给该函数。

def send_log_email(subject, message):
     smtp_server = 'smtp.office365.com'
     smpt_port = 25
     sender = '[emailed address]'
     pw = '[password]'

     msg = MIMEMultipart()
     msg['From'] = sender
     msg['To'] = sender
     msg['Subject'] = subject
     msg.attach(MIMEText(message))

     conn = SMTP(smtp_server,smtp_port)
     conn.set_debuglevel(1)
     conn.starttls()
     conn.login(sender, pw)
     conn.sendmail(sender, sender, msg.as_string())

这是我从批处理文件运行时遇到的错误。

send: 'ehlo []\r\n'
reply: b'250-Outlook Hello [IP]\r\n'
reply: b'250-SIZE 157286400\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-DSN\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-BINARYMIME\r\n'
reply: b'250-CHUNKING\r\n'
reply: b'250 SMTPUTF8\r\n'
reply: retcode (250); Msg: b'outlook' Hello [IP]\nSIZE 157286400\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\nSTARTTLS\n8BITMIME\nBINARYMIME\nCHUNKING\nSMTPUTF8'
send: 'STARTTLS\r\n'
reply: b'220 2.0.0 SMTP server ready\r\n'
reply: retcode (220); Msg: b'2.0.0 SMTP server ready'
Traceback (most recent call last):
  File "path\my_script.py", line 136, in <module>
    send_log_email(result, message)
  File "path\my_email_script.py", line 31, in send_log_email
    conn.starttls()
  File "python_path\custom_environment\lib\smtplib.py", line 756, in starttls
    raise RuntimeError("No SSL support included in this Python")
RuntimeError: No SSL support included in this Python
python-3.x batch-file scheduled-tasks smtplib
2个回答
2
投票

与我在网上找到的大部分内容相反,我需要在批处理文件中激活环境才能让脚本从任务计划程序中正常运行。

call C:\ProgramData\Anaconda3\Scripts\activate.bat
"C:\my_env_path\python.exe" "C:\my_script_path\my_script.py" 
call C:\ProgramData\Anaconda3\Scripts\deactivate.bat

不确定我是否真的需要停用环境,但它确实存在。


0
投票

你能解决这个问题吗?手动运行时遇到与 .starttls() 相同的错误,但不是通过批处理/空闲运行。

© www.soinside.com 2019 - 2024. All rights reserved.