如何使用 python 搜索主题行中包含字符串的 outlook 电子邮件?

How to search outlook email having string in subject line using python?

我想计算最近 7 天的 outlook 电子邮件,其中包含主题错误类型和服务器名称。我是 Python coding.Can 的新手,请问有人帮忙吗?

例如:- 我的收件箱中有以下主题行的邮件:

  1. 检查 CP-TEST-DB2 上是否缺少备份
  2. 检查 G-PROD-AUDB 上的死锁
  3. LF-PTT-DW1SQL 错误日志中有错误
  4. 检查 CP-TEST-DB1 上的驱动器 space

因此,我想为每台服务器(例如- CP-TEST-DB2、G-PROD-AUDB)获取主题行为 'Check for missing backups' 的邮件,并希望在服务器方面对其进行计数。 比如我有多少 "Check for missing backups on" 封邮件给 "CP-TEST-DB2" 服务器。 我有多少 "Check for missing backups on" 邮件给 "G-PROD-AUDB" 等等每个服务器。

我有多少 "Check deadlock on" 封邮件给 "CP-TEST-DB2" 服务器。 我为 "G-PROD-AUDB" 发送了多少 "Check deadlock on" 封邮件,依此类推每台服务器... 等等错误类型。 我每 33 台服务器有 8 种类型的 sql 错误警报邮件?

import win32com.client
import imp, sys, os, re
import datetime as dt
import time

date_time = dt.datetime.now()
print (date_time)

#list of errors
error = ['There are errors in the SQL Error Log on', 'Check for missing backups on', 'Check drive space on', 'Check memory usage on', 'Check deadlock on ']

#list of server
server = ['TEST-AUDB','TEST-AUDB','EUDB','TEST-EUDB','PROD-AUDB','PROD-EUDB']

#setup range for outlook to search emails (so we don't go through the entire inbox)
lastHourDateTime = dt.datetime.now() - dt.timedelta(days = 7)
#print (lastHourDateTime)

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders.Item(2).Folders['Inbox']

messages = inbox.Items
messages.sort("[ReceivedTime]", True)
lastHourMessages = messages.Restrict("[ReceivedTime] >= '" +lastHourDateTime.strftime('%m/%d/%Y %H:%M %p')+"'")
print ("Current time: "+date_time.strftime('%m/%d/%Y %H:%M %p'))

for msg in lastHourMessages:
        subject = msg.Subject
        time = msg.ReceivedTime
        print (s1)```




您可以使用以下正则表达式:

([\-A-Z0-9]+)$

这将匹配每个大写字母、数字和破折号中的 1 个或多个,直到句子结束。这涵盖了您在问题中提供的所有案例,可以看出 here.

接下来,您可以使用 re 模块,遍历字符串列表,使用我上面提到的模式搜索匹配项,并将信息存储在嵌套字典中。

import re

# Example strings
strings = ["Check for missing backups on CP-TEST-DB2",
            "Check deadlock on CP-TEST-DB2",
            "Check deadlock on CP-TEST-DB2",
            "Check deadlock on G-PROD-AUDB",
            "There are errors in the SQL Error Log on LF-PTT-DW1",
            "Check drive space on CP-TEST-DB1"]

# Declare empty dictionary
occurrences = {}

# Iterate over all the examples
for string in strings:
    results = re.search('([\-A-Z0-9]+)$', string)
    # Get the server from the regex match
    server = results.group(0)
    # Remove the server from the string and use strip to get rid of the trailing whitespace
    instance = string.replace(server, '').strip()
    # If the server is still not in the dict, insert it manually
    if occurrences.get(server, None) is None:
        occurrences[server] = {instance: 1}
    # If the server is already in the dict, but not the instance, create the key and initial value for the instance
    elif occurrences[server].get(instance, None) is None:
        occurrences[server][instance] = 1
    # Otherwise, just increment the value for the server-instance pair
    else:
        occurrences[server].update({instance : occurrences[server].get(instance, 0) + 1})
print(occurrences)

希望对您有所帮助!