WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP5 Email Object
Categories : PHP, PHP Classes, Email Click here to Update Your Picture
Joseph Crawford
Date : Dec 07th 2005
Grade : 2 of 5 (graded 3 times)
Viewed : 6527
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Joseph Crawford
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

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 :)




Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
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
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
cPanel Email Accounts Creator
Categories : PHP, PHP Classes, Email, Form Processing, Web Services
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
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
POP3 Class
Categories : PHP Classes, PHP, Email
very simple ftp class
Categories : PHP, PHP Classes, FTP
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
PHP based Contact email form with multiple recipients, text file based, supports departments.
Categories : PHP, Email, Beginner Guides, Filesystem