TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   checking email address domains (http://www.talkphp.com/absolute-beginners/2836-checking-email-address-domains.html)

webosb 05-22-2008 02:10 PM

checking email address domains
 
currently i'm checking with this method:

$email = "godjesus@hotmail.com";
$domain = explode( "@", $email );
if(($domain[1] == "yahoo.com") || ($domain[1] == "hotmail.com")){
echo "its either yahoo or hotmail";
}

is there a better way of doing this or is this the most optimized way?

Wildhoney 05-22-2008 04:43 PM

I don't know what you're after exactly. However, I would put the list of acceptable domains in an array, that way you don't have to keep adding to your if statement. I would take the following approach, using objects so that you can extend if need be, and keep it nice and tidy.

php Code:
class Validate_Email
{
    private $m_szEmail;
    private $m_szDomain;
   
    public function __construct($szEmail)
    {
        $this->m_szEmail = $szEmail;
    }
   
    public function hasValidDomain()
    {
        $aAllowedDomains = array('yahoo', 'hotmail', 'msn', 'gmail');
        preg_match('~.+?@(?P<domain>.+?)\..+~', $this->m_szEmail, $aMatches);
       
        $this->m_szDomain = $aMatches['domain'];
   
        if(!in_array($this->m_szDomain, $aAllowedDomains))
        {
            return false;
        }
       
        return true;
    }
   
    public function getDomain()
    {
        return ucfirst($this->m_szDomain);
    }
}

Then you would simply use it like so. I've put this in a loop so we can check an array of them.

php Code:
$aEmails = array('messiah@hotmail.com', 'buddha@hushmail.com', 'horus@yahoo.co.uk');

foreach ($aEmails as $szEmail)
{
    $pEmail = new Validate_Email($szEmail);
   
    if(!$pEmail->hasValidDomain())
    {
        printf('%s is an unacceptable domain.<br />', $pEmail->getDomain());
        continue;
    }
       
    printf('%s is an acceptable domain.<br />', $pEmail->getDomain());
}

sketchMedia 05-27-2008 07:33 PM

if your on a linux platform you could use the checkdnsrr() function:

PHP Code:

$email 'user@domain.com';

$domain=explode('@'$email);

if(!
checkdnsrr($domain[1], 'MX')) //MX = mail exchange
{
    return 
false;



xenon 05-27-2008 10:23 PM

If you want to check domain availability (such as ping the host), try the fsockopen function.


All times are GMT. The time now is 03:22 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0