Python SMTP电子邮件脚本随机停止工作
因此,我有一个脚本来监视我的计算机并创建日志文件,并想制作另一个脚本,每次创建新文件时,都会向我发送带有日志文件内容的电子邮件。所述脚本的问题在于,电子邮件服务器随机突然停止工作,并且不再发送电子邮件。我无法复制触发问题的行为,而且我绝对没有达到发送的电子邮件的限制。代码在下面。
我已经将其变成了一个EXE文件,并且它在Windows 10上的AutoStart上运行。当我打开PC并发送电子邮件一段时间并随机决定停止时,它总是运行。有什么想法可能是什么?
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()
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. 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Google宣布,截至2022年5月30日,它将禁用对免费Gmail帐户上不安全的应用程序的控制。 ( https://support.google.com/accogle.com/acccounts/accounts/answer/answer/answer/6010255?hllll6010255? = en )
您需要使用Google API插入: https ://www.thepythoncode.com/article/use-gmail-api-in-python
Google has announced that as of May 30th, 2022, it will disable control over Less Secure Apps on free Gmail accounts. (https://support.google.com/accounts/answer/6010255?hl=en)
You need to use Google API insteed : https://www.thepythoncode.com/article/use-gmail-api-in-python