【问题标题】:Setting up a python script to email设置 python 脚本以发送电子邮件
【发布时间】:2018-01-15 00:09:37
【问题描述】:

我正在尝试设置一个 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() 语法不正确的错误。 但是,它适用于我拥有的所有其他测试脚本。 在出现此错误之前,脚本只会运行并且永远不会关闭,并且永远不会发送电子邮件。我不确定我错过了什么。如果您看到问题,请告诉我。谢谢!

【问题讨论】:

    标签: python email smtp smtplib


    【解决方案1】:

    你有左括号:

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

    改为:

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

    【讨论】:

    • 我解决了这个问题,但是关于 driver.close() 我仍然遇到同样的问题
    • 您仍然收到无效的语法错误,或者您的代码是否抛出异常?错误信息是什么?
    • 说没有错误。代码“运行”,或者更确切地说,它出现在终端中,除了它永远不会停止并且永远不会发送电子邮件。就好像脚本打开但没有做任何事情或关闭
    • 抱歉,我确实最终修复了 driver.close(),但无法使电子邮件功能正常工作
    猜你喜欢
    • 2011-09-16
    • 2020-11-03
    • 1970-01-01
    • 2020-10-17
    • 2011-07-28
    • 1970-01-01
    • 2012-09-12
    • 2015-01-16
    • 1970-01-01
    相关资源
    最近更新 更多