如果内容包含的数字大于阈值,则移动 Outlook 邮件

发布于 2024-11-18 17:15:59 字数 891 浏览 2 评论 0原文

我每天都会在收件箱中收到数千条 Nagios 警报,但其中许多实际上都是微不足道的(尽管 Nagios 报告它们很重要)。我想检查这些警报的文本是否包含超过特定阈值的数字;如果数字低于该阈值,则将邮件移至垃圾文件夹。我真的应该与我的系统管理员合作,首先减少 Nagios 发送的无用警报的数量,但请在我尝试创造性解决方法时迁就我。

我使用的是 Outlook 2007,并且发现了一些有关在 VB 中编写 Outlook 宏的教程,包括 这个关于以编程方式创建规则以将邮件移动到不同的文件夹。该示例使用 TextRuleCondition 来检查主题是否包含数组中的任何关键字。

但我不想检查关键字,我想检查消息文本中的数字是否大于或小于阈值。例如,如果消息的文本包含以下内容,则可以将其移至垃圾文件夹:

Nagios bad condition: foo = 3

但如果消息包含此内容,我希望保留它:

Nagios bad condition: foo = 157

这个示例 似乎更像我想要的在邮件内容中搜索任意文本。但它要求消息是开放的,所以我不太确定如何将其转化为规则。任何帮助将不胜感激。

I get thousands of Nagios alerts in my inbox daily, but many of them are actually trivial (even though Nagios reports them as critical). I want to check whether the text of these alerts contains numbers above a certain threshold; if the numbers are lower than that threshold, move the message to a junk folder. I should really work with my sysadmin to decrease the number of useless alerts Nagios sends in the first place, but humor me in my attempt at a creative workaround.

I'm using Outlook 2007 and have found several tutorials on writing Outlook macros in VB, including this one about programmatically creating a rule to move messages to different folders. That example uses a TextRuleCondition to check whether the subject contains any of the keywords in an array.

But I don't want to check for keywords, I want to check if a number in the message text is greater or less than a threshold value. For example, if the text of a message contains the following, it could be moved to a junk folder:

Nagios bad condition: foo = 3

But if a message contained this, I would want to keep it:

Nagios bad condition: foo = 157

This example seems a little more like what I want in terms of searching the content of the message for arbitrary text. But it requires the message to be open, so I'm not quite sure how to translate it into a rule. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看透却不说透 2024-11-25 17:15:59

您链接到的第二个示例将使您走上正确的道路,编写区分好坏的代码垃圾电子邮件。

然后,您需要将该代码放入收件箱项目的 _ItemAdd 事件中,以便每次在收件箱中弹出新内容时都会运行该代码。以下是 Outlook VBA 模块中应包含的内容的示例:

Public WithEvents myOlItems As Outlook.Items

Public Sub Application_Startup()
    ' Upon starting Outlook, set reference to the items in the Inbox.
    Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
    ' Because myOlItems is declared "WithEvents",
    ' the ItemAdd event will fire anytime something new pops up in the Inbox.

    If TypeName(Item) = "MailItem" Then
        ' It's an e-mail.

        ' Here goes the code to test whether it should go to the junk folder.

    Else
        ' It's something else than an e-mail.
        ' Do nothing.
    End If
End Sub

The second example you link to will put you on the right track to write code that discriminates between good and junk e-mails.

Then you will want to put that code in the _ItemAdd event for the Inbox items, such that it runs every time something new pops up in your Inbox. Here's an example of what should go in your Outlook VBA module:

Public WithEvents myOlItems As Outlook.Items

Public Sub Application_Startup()
    ' Upon starting Outlook, set reference to the items in the Inbox.
    Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
    ' Because myOlItems is declared "WithEvents",
    ' the ItemAdd event will fire anytime something new pops up in the Inbox.

    If TypeName(Item) = "MailItem" Then
        ' It's an e-mail.

        ' Here goes the code to test whether it should go to the junk folder.

    Else
        ' It's something else than an e-mail.
        ' Do nothing.
    End If
End Sub
完美的未来在梦里 2024-11-25 17:15:59

JFC已经给你提供了一种方法。这是另一个使用规则来检查消息到达的情况。这样做。

打开 VBA 编辑器并将此代码粘贴到 ThisOutlookSession

UNTESTED

Option Explicit

Sub Sample(MyMail As MailItem)
    Dim strID As String, olNS As Outlook.NameSpace
    Dim objInboxFolder As Outlook.MAPIFolder
    Dim objDestinationFolder As Outlook.MAPIFolder
    Dim olMail As Outlook.MailItem
    Dim strFileName As String, strSubj As String
    Dim Myarray() As String
    Dim ThrsdVal As Long

    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set olMail = olNS.GetItemFromID(strID)

    '~~> Email Subject
    strSubj = olMail.Subject

    '~~> Threshold value
    ThrsdVal = 100

    'Nagios bad condition: foo = 3
    Myarray = Split(strSubj, "=")

    Set objInboxFolder = olNS.GetDefaultFolder(olFolderInbox)
    '~~> Destination folder
    Set objDestinationFolder = objInboxFolder.Folders("Temp")

    '~~> Check if less than threshold value
    If Val(Trim(Myarray(1))) < ThrsdVal Then
        olMail.Move objDestinationFolder
    End If

    Set olMail = Nothing
    Set olNS = Nothing
End Sub

中 立即

1) 创建新规则(选择“邮件到达时检查”)

2) 在(条件)中选择“发件人”人员或通讯组列表”

3) 选择您从中获取电子邮件的相关电子邮件地址

4) 在“操作”中,选择“运行脚本”,然后选择上述脚本。

5) 最后单击完成即可完成:)

最好的部分是您也可以对收件箱文件夹中的现有电子邮件运行此规则:)

注意 :就像我上面提到的,我还没有测试代码,所以如果您遇到任何错误,请告诉我,我会纠正它。另外,我假设该消息的主题格式为“Nagios bad condition: foo = X”。我没有包含任何错误处理。我相信你能解决这个问题 :)

HTH

Sid

JFC has already given you one way. Here is another using RULES to check messages as they arrive. Do this.

Open VBA Editor and paste this code in ThisOutlookSession

UNTESTED

Option Explicit

Sub Sample(MyMail As MailItem)
    Dim strID As String, olNS As Outlook.NameSpace
    Dim objInboxFolder As Outlook.MAPIFolder
    Dim objDestinationFolder As Outlook.MAPIFolder
    Dim olMail As Outlook.MailItem
    Dim strFileName As String, strSubj As String
    Dim Myarray() As String
    Dim ThrsdVal As Long

    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set olMail = olNS.GetItemFromID(strID)

    '~~> Email Subject
    strSubj = olMail.Subject

    '~~> Threshold value
    ThrsdVal = 100

    'Nagios bad condition: foo = 3
    Myarray = Split(strSubj, "=")

    Set objInboxFolder = olNS.GetDefaultFolder(olFolderInbox)
    '~~> Destination folder
    Set objDestinationFolder = objInboxFolder.Folders("Temp")

    '~~> Check if less than threshold value
    If Val(Trim(Myarray(1))) < ThrsdVal Then
        olMail.Move objDestinationFolder
    End If

    Set olMail = Nothing
    Set olNS = Nothing
End Sub

Now

1) Create a new Rule (Select "Check Messages When they Arrive")

2) In (Condition) select "From people or Distribution List"

3) Select the relevant email address from which you are getting the emails

4) In Actions, select "run a script" and then choose the above script.

5) Finally click on Finish and you are done :)

The best part about this is that you can run this rule for existing emails in your inbox folder as well :)

NOTE: Like I mentioned above, I have not tested the code so do let me know if you get any errors and I will rectify it. Also I am assuming that the message will have a subject with the format as "Nagios bad condition: foo = X". I have not included any error handling. I am sure you can take care of that :)

HTH

Sid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文