What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

My IMAP sensor does not process HTML emails correctly using regex. What can I do?

Votes:

0

We monitor several backup services that send emails about their status. For the sensor to show an error as soon as a backup failed, we use regular expressions in the search text field. If the email body does not contain “Failed Backups: 0” (but a number bigger than 0), for example, we want the sensor to show an alarm.

For plain text emails, this works as expected. However, some solutions send HTML emails. The IMAP sensor obviously recognizes these emails but erroneously changes to the Down status even if the regex in the search text field does match the "successful backup" condition.

The sensor log shows that the sensor seems to analyze only a part of the HTML emails. Is there any solution to this issue?

backup-monitoring email email-sensor imap imap-sensor prtg regex regular-expression

Created on Jul 25, 2014 1:47:45 PM by  Gerald Schoch [Paessler Support]

Last change on Jan 4, 2023 11:55:57 AM by  Brandy Greger [Paessler Support]



1 Reply

Accepted Answer

Votes:

0

This article applies as of PRTG 22

IMAP sensors with regex for HTML emails

If you encounter issues regarding the correct matching of regular expressions, first check the correct definition of your regex with a test tool (for example, regex101.com). Then check which type of emails the IMAP sensor has to analyze. If the emails contain an HTML body, make sure that your regex also matches additional source code.

Analyze the HTML content of the email

In some cases, the email server (for example, Microsoft Exchange) creates additional HTML markups within the source code of an email, for example, line breaks or paragraph breaks. So you need to check what exactly the HTML code contains after the email has been delivered. You can view the HTML code of an email if you save it as .html and open it with an editor (in Outlook: File | Save as).

Depending on the additional HTML code in the email, adjust the regex of your IMAP sensor. Make sure that the regex also matches line breaks, paragraphs, or tables. The example below will give you an idea of what you have to consider.

Example

Take the example in the question part of this article. Let us assume that our IMAP sensor should show an alarm if the target email does not contain the expression Failed Backups: 0 because in this case, there has been at least one failed backup.

Then you might have the idea to define the following regex in the search text field of the sensor settings:
Failed Backups: [^\d]*0[^\d]

For plain text emails, this regex will work perfectly. If there is a number other than 0, the sensor will change to the Down status. However, the defined regex assumes that there is no other character between “Failed Backups:” and “0”. But if your service sends HTML-encoded emails, there might be some HTML tags in between. This could look like the following: <tr style='mso-yfti-irow:4'> <td width=350 valign=top style='width:262.5pt;padding:1.5pt 1.5pt 1.5pt 1.5pt'> <p class=MsoNormal>Failed Backups:</p> </td> <td valign=top style='padding:1.5pt 1.5pt 1.5pt 1.5pt'> <p class=MsoNormal>0</p> </td> </tr>

So, after analyzing the actual content of the HTML-encoded email body, you might have the idea to use a dot "." in your regex that matches all characters:
Last Failed Backups.*0

But this would not work either because the dot does not match line breaks, which the HTML code most likely contains.

Solution: Use the dot matches all regex option: You can switch it on with (?s) and off with (?-s). Furthermore, add the left angle bracket < of the HTML tag that follows “0” to your regex in octal notation to close the search space: \074

The resulting regex that matches Failed Backups: 0 will then be:
Failed Backups:(?s).*0(?-s)\074

Analyze your HTML-encoded emails and adjust your defined regex in a similar way if your IMAP sensor does not correctly recognize them. Always keep in mind that additional HTML markups may possibly appear.

Created on Jul 25, 2014 1:54:17 PM by  Gerald Schoch [Paessler Support]

Last change on Jan 3, 2023 10:19:42 AM by  Brandy Greger [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.