我能够使用smtplib从脚本发送电子邮件,但是当我尝试从批处理文件或任务调度程序运行它时,除了电子邮件未发送电子邮件外,所有内容均有效结尾。我遇到了一个" no ssl"错误。
我正在从Conda环境中运行此操作,但是我仔细检查了我在该环境中而不是基本的调用python。
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 []rn'
reply: b'250-Outlook Hello [IP]rn'
reply: b'250-SIZE 157286400rn'
reply: b'250-PIPELININGrn'
reply: b'250-DSNrn'
reply: b'250-ENHANCEDSTATUSCODESrn'
reply: b'250-STARTTLSrn'
reply: b'250-8BITMIMErn'
reply: b'250-BINARYMIMErn'
reply: b'250-CHUNKINGrn'
reply: b'250 SMTPUTF8rn'
reply: retcode (250); Msg: b'outlook' Hello [IP]nSIZE 157286400nPIPELININGnDSNnENHANCEDSTATUSCODESnSTARTTLSn8BITMIMEnBINARYMIMEnCHUNKINGnSMTPUTF8'
send: 'STARTTLSrn'
reply: b'220 2.0.0 SMTP server readyrn'
reply: retcode (220); Msg: b'2.0.0 SMTP server ready'
Traceback (most recent call last):
File "pathmy_script.py", line 136, in <module>
send_log_email(result, message)
File "pathmy_email_script.py", line 31, in send_log_email
conn.starttls()
File "python_pathcustom_environmentlibsmtplib.py", line 756, in starttls
raise RuntimeError("No SSL support included in this Python")
RuntimeError: No SSL support included in this Python
与我在线发现的大多数相反,我需要激活批处理文件中的环境,以使脚本从任务调度程序中正确运行。
call C:ProgramDataAnaconda3Scriptsactivate.bat
"C:my_env_pathpython.exe" "C:my_script_pathmy_script.py"
call C:ProgramDataAnaconda3Scriptsdeactivate.bat
不确定我是否真的需要停用环境,但是有。
在某些批处理脚本而不是其他问题上遇到了麻烦。您需要在同一命令提示中激活环境:
call&quot'c: programData anaconda3 scripts activate.bat;python&quot'path yousercript.py'