mysqli: Retry on deadlock-found error

If MySQL error #1213 (deadlock found when trying to get lock) is returned
from query, it is safe to retry the query.

This patch will retry queries up to two times for a total of three. If the
query cannot be executed without error #1213 after the third time, the error
is logged as usual, and the system continues with the failed query.
Use case-insensitive matching on email address

This patch addresses an issue where a client may have upper-case letters in
their email address. When visiting the client portal to check the ticket
status, previously, the exact same case would be required in the 'Email
Address' box.

This patch removes the case sensitivity for email logins.
Timeout after max timeout while fetching emails

Processing fetching in ASC order of the last fetch time
Remove LIMIT of 10 emails per fetch
Flush the buffer and terminate the request cleanly on autocron
i18n: Remove unused, possibly incorrect formatting

These functions don't appear to be used an could corrupt UTF-8 encoded text
htmLawed: Fix corruption to UTF8 encoded text

On some combinations of operating systems, PHP and libpcre versions, `\s`
will match the iso-8859-x non-breaking-space, 0xa0. This regular expression
will munge the UTF8 encoded version, 0xc2a0 to 0xc220, which is not a valid
UTF8 character.

When inserted into a UTF8 field in mysql, the text will be truncated at and
after the first invalid character.
Send staff alerts as Notice as opposed to AutoRely

Some emails providers (e.g gmail/google) won't deliver emails marked
bulk/autoreply to group email address.
oops: Fix report time frame interpretation
Search for system emails in To and Delivered-To

Previously, the Delivered-To header was only used if the To header was not
found
Shift error checking into Mail_Parse::parseAddressList
Register session close on shutdown at the start of session handling

This is necessary to make sure session data is saved on redirect.
smtp: Reuse existing connections where possible

This should speed up the system where several emails and alerts will be sent
through the same SMTP account in the same request.
pipe: Honor attachment ext and size restrictions