\Mail_RFC822

RFC 822 Email address list validation Utility

What is it?

This class will take an address string, and parse it into it's consituent parts, be that either addresses, groups, or combinations. Nested groups are not supported. The structure it returns is pretty straight forward, and is similar to that provided by the imap_rfc822_parse_adrlist(). Use print_r() to view the structure.

How do I use it?

$address_string = 'My Group: "Richard" richard@localhost (A comment), ted@example.com (Ted Bloggs), Barney;'; $structure = Mail_RFC822::parseAddressList($address_string, 'example.com', true) print_r($structure);

Summary

Methods
Properties
Constants
__construct()
extract_email_addresses()
parseAddressList()
validateMailbox()
approximateCount()
isValidInetAddress()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
_splitAddresses()
_isGroup()
_splitCheck()
_hasUnclosedQuotes()
_hasUnclosedBrackets()
_hasUnclosedBracketsSub()
_validateAddress()
_validatePhrase()
_validateAtom()
_validateQuotedString()
_validateRouteAddr()
_validateRoute()
_validateDomain()
_validateSubdomain()
_validateDliteral()
_validateAddrSpec()
_validateLocalPart()
$address
$default_domain
$nestGroups
$validate
$addresses
$structure
$error
$index
$num_groups
$mailRFC822
$limit
N/A

Properties

$address

$address : string

The address being parsed by the RFC822 object.

Type

string

$default_domain

$default_domain : string

The default domain to use for unqualified addresses.

Type

string

$nestGroups

$nestGroups : boolean

Should we return a nested array showing groups, or flatten everything?

Type

boolean

$validate

$validate : boolean

Whether or not to validate atoms for non-ascii characters.

Type

boolean

$addresses

$addresses : array

The array of raw addresses built up as we parse.

Type

array

$structure

$structure : array

The final array of parsed address information that we build up.

Type

array

$error

$error : string

The current error message, if any.

Type

string

$index

$index : integer

An internal counter/pointer.

Type

integer

$num_groups

$num_groups : integer

The number of groups that have been found in the address list.

Type

integer

$mailRFC822

$mailRFC822 : boolean

A variable so that we can tell whether or not we're inside a Mail_RFC822 object.

Type

boolean

$limit

$limit : integer

A limit after which processing stops

Type

integer

Methods

__construct()

__construct(string  $address = null, string  $default_domain = null, boolean  $nest_groups = null, boolean  $validate = null,   $limit = null) : object

Sets up the object. The address must either be set here or when calling parseAddressList(). One or the other.

Parameters

string $address

The address(es) to validate.

string $default_domain

Default domain/host etc. If not supplied, will be set to localhost.

boolean $nest_groups

Whether to return the structure with groups nested for easier viewing.

boolean $validate

Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.

$limit

Returns

object —

Mail_RFC822 A new Mail_RFC822 object.

extract_email_addresses()

extract_email_addresses(  $str,   $default_domain = null) 

Parameters

$str
$default_domain

parseAddressList()

parseAddressList(string  $address = null, string  $default_domain = null, boolean  $nest_groups = null, boolean  $validate = null,   $limit = null) : array

Starts the whole process. The address must either be set here or when creating the object. One or the other.

Parameters

string $address

The address(es) to validate.

string $default_domain

Default domain/host etc.

boolean $nest_groups

Whether to return the structure with groups nested for easier viewing.

boolean $validate

Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.

$limit

Returns

array —

A structured array of addresses.

validateMailbox()

validateMailbox(  $mailbox) : boolean

Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; name and route-addr

Parameters

$mailbox

Returns

boolean —

Success or failure.

approximateCount()

approximateCount(string  $data) : integer

Returns an approximate count of how many addresses are in the given string. This is APPROXIMATE as it only splits based on a comma which has no preceding backslash. Could be useful as large amounts of addresses will end up producing *large* structures when used with parseAddressList().

Parameters

string $data

Addresses to count

Returns

integer —

Approximate count

isValidInetAddress()

isValidInetAddress(string  $data, boolean  $strict = false) : mixed

This is a email validating function separate to the rest of the class. It simply validates whether an email is of the common internet form: <user>@<domain>. This can be sufficient for most people. Optional stricter mode can be utilised which restricts mailbox characters allowed to alphanumeric, full stop, hyphen and underscore.

Parameters

string $data

Address to check

boolean $strict

Optional stricter mode

Returns

mixed —

False if it fails, an indexed array username/domain if it matches

_splitAddresses()

_splitAddresses(string  $address) : boolean

Splits an address into separate addresses.

Parameters

string $address

The addresses to split.

Returns

boolean —

Success or failure.

_isGroup()

_isGroup(string  $address) : boolean

Checks for a group at the start of the string.

Parameters

string $address

The address to check.

Returns

boolean —

Whether or not there is a group at the start of the string.

_splitCheck()

_splitCheck(array  $parts, string  $char) : mixed

A common function that will check an exploded string.

Parameters

array $parts

The exloded string.

string $char

The char that was exploded on.

Returns

mixed —

False if the string contains unclosed quotes/brackets, or the string on success.

_hasUnclosedQuotes()

_hasUnclosedQuotes(string  $string) : boolean

Checks if a string has unclosed quotes or not.

Parameters

string $string

The string to check.

Returns

boolean —

True if there are unclosed quotes inside the string, false otherwise.

_hasUnclosedBrackets()

_hasUnclosedBrackets(string  $string, string  $chars) : boolean

Checks if a string has an unclosed brackets or not. IMPORTANT: This function handles both angle brackets and square brackets;

Parameters

string $string

The string to check.

string $chars

The characters to check for.

Returns

boolean —

True if there are unclosed brackets inside the string, false otherwise.

_hasUnclosedBracketsSub()

_hasUnclosedBracketsSub(string  $string,   $num, string  $char) : integer

Sub function that is used only by hasUnclosedBrackets().

Parameters

string $string

The string to check.

$num
string $char

The character to count.

Returns

integer —

The number of occurences of $char in $string, adjusted for backslashes.

_validateAddress()

_validateAddress(string  $address) : mixed

Function to begin checking the address.

Parameters

string $address

The address to validate.

Returns

mixed —

False on failure, or a structured array of address information on success.

_validatePhrase()

_validatePhrase(string  $phrase) : boolean

Function to validate a phrase.

Parameters

string $phrase

The phrase to check.

Returns

boolean —

Success or failure.

_validateAtom()

_validateAtom(string  $atom) : boolean

Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>

If validation ($this->validate) has been turned off, then validateAtom() doesn't actually check anything. This is so that you can split a list of addresses up before encoding personal names (umlauts, etc.), for example.

Parameters

string $atom

The string to check.

Returns

boolean —

Success or failure.

_validateQuotedString()

_validateQuotedString(string  $qstring) : boolean

Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">

Parameters

string $qstring

The string to check

Returns

boolean —

Success or failure.

_validateRouteAddr()

_validateRouteAddr(string  $route_addr) : mixed

This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">"

Angle brackets have already been removed at the point of getting to this function.

Parameters

string $route_addr

The string to check.

Returns

mixed —

False on failure, or an array containing validated address/route information on success.

_validateRoute()

_validateRoute(string  $route) : mixed

Function to validate a route, which is: route = 1#("@" domain) ":"

Parameters

string $route

The string to check.

Returns

mixed —

False on failure, or the validated $route on success.

_validateDomain()

_validateDomain(string  $domain) : mixed

Function to validate a domain, though this is not quite what you expect of a strict internet domain.

domain = sub-domain *("." sub-domain)

Parameters

string $domain

The string to check.

Returns

mixed —

False on failure, or the validated domain on success.

_validateSubdomain()

_validateSubdomain(string  $subdomain) : boolean

Function to validate a subdomain: subdomain = domain-ref / domain-literal

Parameters

string $subdomain

The string to check.

Returns

boolean —

Success or failure.

_validateDliteral()

_validateDliteral(string  $dliteral) : boolean

Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"

Parameters

string $dliteral

The string to check.

Returns

boolean —

Success or failure.

_validateAddrSpec()

_validateAddrSpec(string  $addr_spec) : mixed

Function to validate an addr-spec.

addr-spec = local-part "@" domain

Parameters

string $addr_spec

The string to check.

Returns

mixed —

False on failure, or the validated addr-spec on success.

_validateLocalPart()

_validateLocalPart(string  $local_part) : mixed

Function to validate the local part of an address: local-part = word *("." word)

Parameters

string $local_part

Returns

mixed —

False on failure, or the validated local part on success.