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_Smtplib - Fatal编程技术网

设置python脚本以发送电子邮件

设置python脚本以发送电子邮件,python,email,smtp,smtplib,Python,Email,Smtp,Smtplib,我正在尝试设置一个python脚本,用于测试服务器是否已启动,如果未启动,请发送电子邮件给我。我目前正在努力实现这一目标的电子邮件部分 我的代码如下所示: import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys import os import time from selenium.webdriver.firefox.firefox_binary import F

我正在尝试设置一个python脚本,用于测试服务器是否已启动,如果未启动,请发送电子邮件给我。我目前正在努力实现这一目标的电子邮件部分

我的代码如下所示:

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



binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path='C:\geckodriver-v0.18.0-win64\geckodriver.exe')


class PythonOrgSearch(unittest.TestCase):

#sets up driver to run tests
    def setUp(self):
        self.driver = driver

    def testServer(self):
        driver = self.driver
        server = smtplib.SMTP('smtp.gmail.com', 587)

        #Next, log in to the server
        server.login("email@gmail.com", "password")

        #Send the mail
        msg = "Testing if server is down" # The /n separates the message from the headers
        server.sendmail("email@google.com", "email@google.com", msg)
        driver.close()

if __name__ == "__main__":
    unittest.main()
当我运行这个程序时,我得到的错误是driver.close()的语法不正确。 然而,它在我拥有的所有其他测试脚本上都工作得很好。
在这个错误出现之前,脚本将只是运行并且永远不会关闭,电子邮件也永远不会被发送。我不确定我错过了什么。如果你看到这个问题,请告诉我。谢谢大家!

您有开括号:

server.sendmail(("email@google.com", "email@google.com", msg)
改为:

server.sendmail("email@google.com", "email@google.com", msg)

我已经解决了这个问题,但是我仍然遇到与驱动程序相同的问题。close()您仍然遇到无效的语法错误,或者您的代码引发了异常?错误消息是什么?没有任何错误。代码“运行”,或者更确切地说,它出现在终端中,除非它从未停止,电子邮件从未发送。这就好像脚本打开了,但没有做任何事情或关闭很抱歉澄清一下,我最终修复了驱动程序。close(),但无法使电子邮件功能正常工作