Email address parsing

From PostgreSQL wiki
Jump to navigationJump to search

Library Snippets

Function to parse email addresses

Works with PostgreSQL

Any version

Written in

PL/Perl

Depends on

Email::Address

This function extracts the real name from an email address. With slight variations, other components such as local part or host name can be extracted. Relying on a well-tested Perl module makes this function extremely robust, compared to say handcrafted pattern-matching approaches.

CREATE OR REPLACE FUNCTION email_name(email text) RETURNS text
LANGUAGE plperlu
AS $$
use Email::Address;

my @addresses = Email::Address->parse($_[0]);
return undef unless scalar(@addresses) > 0;
return $addresses[0]->name;
$$;