E-mail List Splitting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

E-mail List Splitting

Post 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?
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Re: E-mail List Splitting

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: E-mail List Splitting

Post by VladSun »

Sounds more like a regexp problem to me:

Code: Select all

preg_match_all('/<(.+?)>/', $to, $matches);
There are 10 types of people in this world, those who understand binary and those who don't
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Re: E-mail List Splitting

Post 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.
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Re: E-mail List Splitting

Post 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.
Post Reply