Category Archives: Email

Email Validation using PHP

In one of my recent project I was required to validate email address in PHP. There are many ways to do this but I like following two approaches very much for which I created two different functions:

I created a function isEmail which will validate the email address and return true or false as per validation result.

First Approach: Using Filter functions (http://in.php.net/manual/en/function.filter-var.php)

This is quite simple and straightforward way using filter_var method and FILTER_VALIDATE_EMAIL constant which works on PHP 5.2+

Here is the function definition:
<?php
function isEmail_FirstApproach($strEmail)
{
if(filter_var($strEmail, FILTER_VALIDATE_EMAIL))
{
return true;
}
return false;
}
?>

Second Approach: Using Regular Expression (http://in3.php.net/manual/en/function.preg-match.php)
Regular expression is extremely powerful and quite confusing tool. So use it cautiously 🙂
Note: This function validates emails very well but quite not fully functional will all kind of emails. For e.g. Gmail gives option to add ‘+’ sign in emails which will fail with this functions and so on. Anyways you can easily enhance it 🙂

Here is the function definition:

<?php
function isEmail_SecondApproach($strEmail)
{
$strPattern = “/^([a-z])([a-z0-9._])+([a-z0-9])\@([a-z0-9])*([a-z])+(-[a-z])*([a-z0-9])*(\.([a-z0-9])*([a-z])+(-[a-z])*([a-z0-9])*)+$/i”;

if(preg_match($strPattern, $strEmail))
{
return true;
}
return false;
}
?>

Now simply call these functions to use it’s functionality as follows:
<?php
$email = “abc@example.com”;

if(isEmail_FirstApproach($email))
echo “Valid Email”;
else
echo “Invalid Email”;

if(isEmail_SecondApproach($email))
echo “Valid Email”;
else
echo “Invalid Email”;
?>

Note: Validation results can be different for different email addresses as filter validation is quite liberal than my Regex function.

Let me know if you find any issue with this or know any more efficient way to perform the same.

Advertisements

Lets Forward E-Mails But …

I just got this mail from one of the yahoo groups I have subscribed. Normally I do follow all of the steps and I think its very necessary for all of us to make this as a habit to reduce junk mails and virus attacks at everyones end. Also it makes a mail more readable.

Do you wonder why you get viruses or junk mail? Do you hate it?

Every time you forward an e-mail there is information left over from the people who got the message before you, namely their e-mail addresses & names. As the messages get forwarded along, the list of addresses builds, and builds, and builds, and all it takes is for some poor sap to get a virus, and his or her computer can send that virus to every e-mail address that has come across his computer. Or, someone can take all of those addresses and sell them or send junk mail to them in the hopes that you will go to the site and he will make five cents for each hit. That’s right, all of that inconvenience over a nickel!

How do you stop it? Well, there are several easy steps:

(1) When you forward an e-mail, DELETE all of the other addresses that appear in the body of the message (at the top). That’s right, DELETE them. Highlight them and delete them, backspace them, cut them, whatever it is you know how to do. It only takes a second You MUST click the “Forward” button first and then you will have full editing capabilities against the body and headers of the message. If you don’t click on “Forward” first, you won’t be able to edit the message at all.

(2) Whenever you send an e-mail to more than one person, do NOT use the To: or Cc: fields for adding e-mail addresses. Always use the BCC: (blind carbon copy) field for listing the e-mail addresses. This is the way the people you send to will only see their own e-mail address. If you don’t see your BCC: option click on where it says To: and your address list will appear. Highlight the address and choose BCC: and that’s it, it’s that easy. When you send to BCC: your message will automatically say “Undisclosed Recipients” in the “TO:” field of the people who receive it.

(3) Remove any “FW :” in the subject line. You can re-name the subject if you wish or even fix spelling. Also remove all “>” from the mail body to make it reader friendly.

(4) ALWAYS hit your Forward button from the actual e-mail you are reading. Ever get those e-mails that you have to open 10 pages to read the one page with the information on it? By Forwarding from the actual page you wish someone to view, you stop them from having to open many e-mails just to see what you sent.

(5) Have you ever gotten an email that is a petition? It states a position and asks you to add your name and address and to forward it to 10 or 15 people or your entire address book. The email can be forwarded on and on and can collect thousands of names and email addresses. A FACT: The completed petition is actually worth a couple of bucks to a professional spammer because of the wealth of valid names and email addresses contained therein. If you want to support the petition, send it as your own personal letter to the intended recipient. Your position may carry more weight as a personal letter than a laundry list of names and email address on a petition. (Actually, if you think about it, who’s supposed to send the petition in to whatever cause it supports? And don ‘t believe the ones that say that the email is being traced, it just ain’t so!)

(6) One of the main ones I hate is the ones that say that something like, “Send this email to 10 people and you’ll see something great run across your screen.” Or, sometimes they’ll just tease you by saying something really cute will happen IT AIN’T GONNA HAPPEN!!!!!

(7) Before you forward an Amber Alert, or a Virus Alert, or some of the other ones floating around nowadays, check them out before you forward d them. Most of them are junk mail that’s been circling the net for YEARS! Just about everything you receive in an email that is in question can be checked out at Snopes.

Its really easy to find out if it’s real or not. If it’s not, please don’t pass it on.

So please, in the future, let’s stop the junk mail and the viruses.

Finally, here’s an idea!!! Let’s send this to everyone we know. This is something that SHOULD be forwarded.


GMail goes Mobile

Google now provides a downloadable gmail application for java-enabled phones to browse your email in a familiar web based user interface of Gmail. It is faster to access and also pre-fetch the messages. So now you can read, compose, search your inbox, view and add attachments including images and PDF files through your mobiles only.

To download the application, go to gmail.com/app and follow the instructions.