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.
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:
<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 valign=top style='padding:1.5pt 1.5pt 1.5pt 1.5pt'>
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:
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.