Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python脚本发送电子邮件_Python_Email_Smtp_Python Unittest_Smtplib - Fatal编程技术网

从python脚本发送电子邮件

从python脚本发送电子邮件,python,email,smtp,python-unittest,smtplib,Python,Email,Smtp,Python Unittest,Smtplib,我正在使用unittest和SeleniumWebDriver编写一个python测试,以测试我们的服务器是否停机,并发送电子邮件通知我是否停机。我目前正致力于实现电子邮件功能。这是我当前的代码。运行时,程序似乎在运行,但从未结束,也从未发送电子邮件。(即,在命令行中,程序似乎正在运行,因为它不会给出任何错误,在输入另一个命令之前,您必须退出“正在运行”的程序)。 我目前的代码是: #tests for if server is up and notifies if it is not

我正在使用unittest和SeleniumWebDriver编写一个python测试,以测试我们的服务器是否停机,并发送电子邮件通知我是否停机。我目前正致力于实现电子邮件功能。这是我当前的代码。运行时,程序似乎在运行,但从未结束,也从未发送电子邮件。(即,在命令行中,程序似乎正在运行,因为它不会给出任何错误,在输入另一个命令之前,您必须退出“正在运行”的程序)。 我目前的代码是:

 #tests for if server is up and notifies if it is not
    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import os
    import time
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    from selenium.webdriver.common.action_chains import ActionChains
    from urllib.request import urlopen
    from html.parser import HTMLParser
    import smtplib
    
    
    class PythonOrgSearch(unittest.TestCase):
    
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.login("email@gmail.com", "password")
            msg = "Testing if server is down" 
            server.sendmail("email@gmail.com", "email@gmail.com", msg)
    
    if __name__ == "__main__":
        unittest.main()
我不确定为什么这不起作用,并希望有任何见解。谢谢大家!

编辑 按照建议更改代码时,我出现以下错误:

Traceback (most recent call last):
  File "testServerIsUp.py", line 14, in <module>
    class PythonOrgSearch(unittest.TestCase):
  File "testServerIsUp.py", line 18, in PythonOrgSearch
    server.starttls() #and this method to begin encryption of messages
  File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 751, in starttls
    "STARTTLS extension not supported by server.")
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
回溯(最近一次呼叫最后一次):
文件“testServerIsUp.py”,第14行,在
类PythonOrgSearch(unittest.TestCase):
PythonOrgSearch中第18行的文件“testServerIsUp.py”
server.starttls()#并使用此方法开始加密消息
文件“C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\smtplib.py”,第751行,在starttls中
“服务器不支持STARTTLS扩展。”)
smtplib.SMTPNotSupportedError:服务器不支持STARTTLS扩展。

您需要启动与邮件服务器的对话并启用加密:

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls() #and this method to begin encryption of messages
server.login("email@gmail.com", "password")
msg = "Testing if server is down" 
server.sendmail("email@gmail.com", "email@gmail.com", msg)
由于
smtplib.SMTP()
调用未成功,您可以在端口465上尝试
SMTP\u SSL

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("email@gmail.com", "password")
msg = "Testing if server is down" 
server.sendmail("email@gmail.com", "email@gmail.com", msg)

它仍然不起作用,有同样的问题。在我这样运行它之后,我想可能需要一个server.close()来修复它,但这也不起作用。有什么建议吗?这给了我一个回溯错误。我在帖子中添加了完整错误作为编辑以保留格式。尝试在不使用
server.ehlo()
方法的情况下运行第二个选项。然后我得到了这个错误:回溯(最近一次调用):文件“testServerIsUp.py”,第14行,在类PythonOrgSearch(unittest.TestCase)中:文件“testServerIsUp.py”,第17行,在PythonOrgSearch server.starttls()中,使用此方法开始加密starttls“服务器不支持starttls扩展名”中第751行的消息文件“C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\smtplib.py”)smtplib.SMTPNotSupportedError:服务器不支持STARTTLS扩展。我认为它不允许我使用ehlos或STARTTLS扩展