Page 1 of 1

E-mail List Splitting

Posted: Tue Jan 22, 2008 9:36 am
by Bon Bon
Hi all,

not been here in a while.

Got another complicated question to ask, hopefully, even if you do not know the answer somebody might be able to point me in the right direction.

I am working on e-mail address lists at the moment and I could not find a way of splitting e-mail addresses in accordance to rfc2822.

Take this example:
From: "Joe Q. Public" <john.q.public@example.com>
To: Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>
Cc: <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>
Date: Tue, 1 Jul 2003 10:52:37 +0200
Message-ID: <5678.21-Nov-1997@example.com>

In the example the e-mail addresses are as follows:

Code: Select all

 
$email_addresses['"Joe Q. Public"'] = 'john.q.public@example.com';
$email_addresses['Mary Smith'] = 'mary@x.test';
$email_addresses[] = 'jdoe@example.org';
$email_addresses['Who?'] = 'one@y.test';
$email_addresses[] = 'boss@nil.test';
$email_addresses['"Giant; \"Big\" Box"'] = 'sysservices@example.net';
 
I was hoping somebody knew some regex that could do this?

Re: E-mail List Splitting

Posted: Tue Jan 22, 2008 9:41 am
by Bon Bon
I forgot to mention the list will look something like this:

$to = 'DIY Kitchen Units <x@example.com>; "DIY Kitchens; Kitchen Units" <y@example.com>, "z - a, b, c " <dune@example.com>';

Hence the commas and semi-colons in the display names.

Re: E-mail List Splitting

Posted: Tue Jan 22, 2008 10:19 am
by VladSun
Sounds more like a regexp problem to me:

Code: Select all

preg_match_all('/<(.+?)>/', $to, $matches);

Re: E-mail List Splitting

Posted: Wed Jan 23, 2008 7:12 am
by Bon Bon
That has gave me a great idea, thanks. Should have really thought about it but was getting tired. If it works will post the code here.

Re: E-mail List Splitting

Posted: Wed Jan 23, 2008 7:44 am
by Bon Bon
I know I have not spent that long on this but had no hope, I need some way of pulling out e-mails from a string even if for now it does not validate against the rfc 2822 specification.

If anyone can, there are regex's at http://www.regular-expressions.info/email.html which may be of use.