Tuesday, 1 January 2013

How to Validate, Encode and Send Email using PHP Script?

How to Validate, Encode and Send Email using PHP Script?

Sending email from PHP script is very easy. You can send email using PHP script in many ways. Just create an HTML Form and run the PHP script to send email to various recipient. PHP script uses mail() function to send the email. But before sending the email using PHP script, you must validate the email address to which you are going to send the email. You can impose an extra check to encode the email address to which you are going to send the email using PHP script. Validating and Encoding the email address with PHP script is very easy. So, I will show here step by step how can you validate, encode and send email using PHP script.

Step 1: How to Validate Email Address using PHP Script?

Following is the PHP code to validate email address:

function IsValidEmailAddress($email, $test_mx = false)
{
 if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
  if($test_mx)
  {
   list($username, $domain) = split("@", $email);
   return getmxrr($domain, $mxrecords);
  }
  else
   return true;
 else
  return false;
}

Above function to validate the email address uses regular expressions to check whether email address is valid or not. If email address is valid, it returns true otherwise false.

Step 2: How to Encode Email Address using PHP Script?

Following is the PHP code to encode email address:

function EncodeEmailAddress($email='info@domain.com', $linkText='Contact Us', $attrs ='class="emailencoder"' )
{
 $email = str_replace(
'@', '@', $email);
 $email = str_replace('.', '.', $email);
 $email = str_split($email, 5);
 $linkText = str_replace('@', '@', $linkText);
 $linkText = str_replace('.', '.', $linkText);
 $linkText = str_split($linkText, 5);

 $part1 = '<a href="ma';
 $part2 = 'ilto&#58;';
 $part3 = '" '. $attrs .' >';
 $part4 = '</a>';
 $encoded = '<script type="text/javascript">';
 $encoded .= "document.write('$part1');";
 $encoded .= "document.write('$part2');";
 foreach($email as $e)
 {
   $encoded .= "document.write('$e');";
 }
 $encoded .= "document.write('$part3');";
 foreach($linkText as $l)
 {
   $encoded .= "document.write('$l');";
 }
 $encoded .= "document.write('$part4');";
 $encoded .= '</script>';
 return $encoded;
}

Step 3: How to Send Email using PHP Script?

Following is the PHP code to send email to recipient. PHP uses mail() function to send the email. PHP mail() function takes "TO", "SUBJECT" and "BODY" as arguments.

<?php
 $to = "
recipient@example.com";
 $subject = "Hello 2013";
 $body = "GoodBye 2012, Welcome 2013";
 if (mail($to, $subject, $body)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");

2 comments:

  1. E-mail is a lot more complex than what you've got here and your regular expression for validation will exclude a significant number of valid e-mail addresses. A better approach is to use a toolkit such as:

    https://github.com/cubiclesoft/ultimate-email/

    I prefer that one but there are also a couple other decent toolkits available such as PHPMailer and SwiftMailer.

    ReplyDelete
  2. This is a good tool to validate email addresses in .net:
    http://www.kellermansoftware.com/p-37-net-email-validation.aspx

    ReplyDelete