According to
filter_var(), the email address ending with "@randomname" is valid.
Indeed, "randomname" is a valid host name, at least syntactically.
The regex pattern in this script is less restrictive than the pattern used by filter_var() to validate email addresses, so it should pull
something out of the input string from the places you expect to find email addresses.
Code: Select all
<?php
header('Content-Type: text/plain');
$pattern = '/<((?:"[^"]+"@|[^<]+@)[^>]+)>/';
$subject = 'Sep 1 09:57:55 gpmail postfix/smtp[12622]: 093A116B10B: to=<randomtext82@hotmail.com>, relay=mx2.hotmail.com[65.54.188.110]:25, delay=0.85, delays=0.08/0.03/0.28/0.46, dsn=2.0.0, status=sent (250 <dc57235831ca0d82c17d2fe292982ad2@randomname> Queued mail for delivery)';
$matches = null;
var_dump(
preg_match_all($pattern, $subject, $matches),
$matches,
filter_var($matches[1][0], FILTER_VALIDATE_EMAIL),
filter_var($matches[1][1], FILTER_VALIDATE_EMAIL)
);
Pattern explanation:
Code: Select all
'/<((?:"[^"]+"@|[^<]+@)[^>]+)>/' $pattern
' ' string bounds
/ / regex bounds
< > literal characters (email bounds)
( ) capturing subpattern (the email address)
(?: ) non-capturing subpattern (the local part)
| "or" branch within nearest subpattern
" "@ literal chars in case of quoted local part
@ literal char in case of usual, unquoted local part
[ ] character range bounds
[^"] any character not a quote mark
[^"]+ one or more any char not a quote mark
[^<]+ one or more any char not a less-than sign
[^>]+ one or more any char not a greater-than sign
After validating the matched strings with filter_var(), you can apply additional restrictions, such as requiring a dot nested in the host part or a verified top-level domain. (Wikipedia:
List of TLDs) Ask if you need tips.
Beyond that, it is possible to verify the domain with DNS validation and maybe verify the account with SMTP validation, but I have not yet explored that in-depth. There are some sites that do this kind of validation for you, but I wouldn't trust them to keep the email addresses private, so you should probably come up with a solution yourself or find an open-source solution that you can examine before implementing.
Update: I found
something that might be useful. (I haven't tried it.)