Like this code?
Show the author your appreciation.
I needed a simple way to deal with email addresses in PHP5, i decided i would create a class to deal with them. This class will allow me to do things like if($email->isValid()) and $email->isFree();
ofcourse checking to see if it is a free provider you either need to store a list of free providers or fetch a remote list. Here is the db table i have
CREATE TABLE `free_email_providers` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`domain` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
all that is stored in the db is the domain like yahoo.com, gmail.com etc..
you can also do
$email->GetLink('Email Me', 'Custom Subject', 'Custom CSS Class');
here is the class for dealing with emails
<?php
class Email {
const VALID_REGEX = '/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([_\\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i' ;
private $username ;
private $domain ;
private $suffix ;
private $isFree ;
private $isValid ;
static private $freeProviders = array();
public function __construct ( $email = '' ) {
$this -> LoadFreeProviders ();
if( strstr ( $email , '@' )) {
$email = explode ( '@' , $email );
$this -> username = $email [ 0 ];
$domain = explode ( '.' , $email [ 1 ]);
$pieces = count ( $domain );
$this -> suffix = $domain [ $pieces - 1 ];
if( $pieces > 2 ) {
unset( $domain [ $pieces - 1 ]);
$domain = array_values ( $domain );
$this -> domain = implode ( '.' , $domain );
} else {
$this -> domain = $domain [ 0 ];
}
$this -> isValid = $this -> IsValid ();
$this -> isFree = $this -> IsFree ();
} else {
throw new EmailEx ( 'Invalid e-mail address provided.' );
}
}
private function LoadFreeProviders () {
$db = Database :: GetDriver ();
$sql = "SELECT domain FROM free_email_providers" ;
$rs = $db -> Execute ( $sql );
$rs -> FirstRow ();
while(! $rs -> EOF ()) {
array_push ( self :: $freeProviders , $rs -> fields [ 'domain' ]);
$rs -> NextRow ();
}
}
public function GetUsername () {
return $this -> username ;
}
public function GetDomain () {
return $this -> domain . '.' . $this -> suffix ;
}
public function GetShortDomain () {
return $this -> domain ;
}
public function GetSuffix () {
return $this -> suffix ;
}
public function IsValid () {
if( preg_match ( self :: VALID_REGEX , $this -> __toString ())) return TRUE ;
else return FALSE ;
}
public function IsFree () {
foreach( self :: $freeProviders as $provider ) {
if( $this -> GetDomain () == $provider ) return TRUE ;
}
return FALSE ;
}
public function GetLink ( $title = null , $subject = null , $class = 'link' ) {
$email = $this -> username . '@' . $this -> domain . '.' . $this -> suffix ;
if(! is_null ( $subject )) $email .= '?SUBJECT=' . $subject ;
return '<a href="mailto:' . $email . '" class="' . $class . '">' . $title . '</a>' ;
}
public function __toString () {
return $this -> username . '@' . $this -> domain . '.' . $this -> suffix ;
}
}
?>
Enjoy :)
POP3 Class Categories : PHP Classes , PHP , Email cPanel Email Accounts Creator Categories : PHP , PHP Classes , Email , Form Processing , Web Services Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip Categories : Email , Network , PHP , PHP Classes A class for sending email; it has support for To:, Cc:, Bcc: and Reply-To:
headers. It requires that you have sendmail installed. Categories : Email , PHP Classes , PHP PHP MIME Decoder. This class decodes Mime Encoded email message.
Attachments are stored in a director. Works with Multipart/alternative,
multipart/mixed etc.
see http://p3mail.com for example. Categories : PHP , PHP Classes , Email Validator - A PHP class that can can be used for validating Email IDs and Dates Categories : PHP , PHP Classes , Data Validation , Email , Date Time Three Cool Classes and One Trick Categories : PHP , PHP Classes , Graphics , Email Class that allows the PHP developer to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box.
Categories : Network , Email , PHP , PHP Classes EAvalidator - This class can be used to validate an e-mail address by checking its domain. Categories : PHP , PHP Classes , Email , Regexps Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support Categories : PHP , FTP , Filesystem , PHP Classes , Compression DbObject - A PHP wrapper for working with various databases Categories : Databases , PHP , PHP Classes Authorize.net AIM Interface Class v1.0.0 Categories : PHP , PHP Classes , Ecommerce , Payment Gateways Browser Detecor Class Categories : PHP Classes , PHP , HTML filesplit : Split big text files in multiple small ones Categories : PHP , Log Files , Filesystem , PHP Classes Simple Email address validation Categories : Email , PHP , Strings