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;
}
{
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);
{
$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 = '';
$part4 = '';
$linkText = str_replace('.', '.', $linkText);
$linkText = str_split($linkText, 5);
$part1 = '';
$part4 = '';
$encoded = '';
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.
$to = "recipient@example.com";
$subject = "Hello 2013";
$body = "GoodBye 2012, Welcome 2013";
if (mail($to, $subject, $body)) {
echo("
} else {
echo("
$subject = "Hello 2013";
$body = "GoodBye 2012, Welcome 2013";
if (mail($to, $subject, $body)) {
echo("
Message successfully sent!
");} else {
echo("
Message delivery failed...
");