gpt4 book ai didi

python - 设置发送电子邮件的 python 脚本

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:16 24 4
gpt4 key购买 nike

我正在尝试设置一个 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)

关于python - 设置发送电子邮件的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com