简体   繁体   English

Python SMTP 电子邮件脚本随机停止工作

[英]Python SMTP email script stops working randomly

So I have a script that monitors my machine and creates log files and wanted to make another script that sends me an email with the content of the log file every time a new file is created.所以我有一个脚本来监视我的机器并创建日志文件,并且想要制作另一个脚本,每次创建新文件时都会向我发送一封包含日志文件内容的电子邮件。 The problem with the said Script is that the email server randomly suddenly stops working and no longer sends emails.上述脚本的问题是电子邮件服务器随机突然停止工作并且不再发送电子邮件。 I cant reproduce a behaviour that trigger the problem and I am definitely not reaching the limit of emails sent.我无法重现触发问题的行为,而且我绝对没有达到发送电子邮件的限制。 The code is below.代码如下。

I have turned it into an exe file and it runs on autostart on Windows 10. It always runs when I turn the PC on and sends emails for a while and just randomly decided to stop.我已经把它变成了一个 exe 文件,它在 Windows 10 上自动启动时运行。它总是在我打开电脑并发送一段时间的电子邮件时运行,然后随机决定停止。 Any ideas whst the problem might be?问题可能出在哪里?

class ExampleHandler(FileSystemEventHandler):
    def sendmail(self, email, password, message):
        server = smtplib.SMTP(host="smtp.gmail.com", port=587)  
        server.ehlo()
        server.starttls()
        server.login(email, password)
        server.sendmail(email, email, message)
        server.close() 

    def on_created(self, event): # when file is created
        file_path = event.src_path
        with open(os.path.join(os.path.dirname(__file__), file_path), 'r') as input_file:
            content = input_file.read()
        self.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, content)

observer = Observer()
event_handler = ExampleHandler() # create event handler
# set observer to use created handler in directory
observer.schedule(event_handler, path="...")
observer.start()

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()

observer.join()

Google has announced that as of May 30th, 2022, it will disable control over Less Secure Apps on free Gmail accounts.谷歌宣布,自 2022 年 5 月 30 日起,它将禁用对免费 Gmail 帐户上不太安全的应用程序的控制。

You need to use Google API insteed : https://www.thepythoncode.com/article/use-gmail-api-in-python您需要使用 Google API insteed: https ://www.thepythoncode.com/article/use-gmail-api-in-python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM