Forum Moderators: coopster
I have three things concerns for it's successful working
1> email validation -- using regex
2> email verification -- if a email is valid that does not mean is exist... so some techniques to check it. But on this forum, someone said checking this is blocked by few sites as hacking attempt and blacklist
3> prevention from email injection.
I searched the forum and most of the posts deal only with the first point. Can we discuss the methods and codes relating the next two points
"/^[a-zA-Z0-9._+-]+@{1}+[a-zA-Z0-9-_]{2,}+\.+[a-zA-Z]{2,4}+$/"
// strposs() looks for the terms included in the top array
$header_injection_attempts = array(
"bcc:",
"cc:",
"to:",
"content-type:",
"mime-version:",
"multipart/mixed",
"content-transfer-encoding:",
".EXE",
".exe"
);
// lowercase the email
$email_body_lower = strtolower($email_body);
// innocent until proven guilty
$injection_attempted = false;
foreach($header_injection_attempts as $attempt){
// check the email for each possible attempt
if(strpos($email_body_lower, $attempt)!==false){
// we found something bad being attempted
$injection_attempted = true;
// get out of the loop
break;
}
}
if($injection_attempted){
// log the error, and visitor IP
// don't send the email
} else {
// send the email
mail(....);
}
This addresses your #2 concern
I use it often
<<<<
<?php // FIRST FUNCTION
// Function to check whether a given hostName is a valid email
// domain address.
function myCheckDNSRR($hostName, $recType = '')
{
if(!empty($hostName)) {
if( $recType == '' ) $recType = "MX";
exec("nslookup -type=$recType $hostName", $result);
// check each line to find the one that starts with the host
// name. If it exists then the function succeeded.
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}
}
// otherwise there was no mail handler for the domain
return false;
}
return false;
}
?>
<?
function validate_email($email)
{
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("@",$email);
// Validate the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}
return $valid;
}
/* Usage:
if(validate_email($_POST['email'])) {
email is valid...
} else {
email not valid..
}*/
?>
>>>>>>
/^(([A-Za-z0-9]+_+)¦([A-Za-z0-9]+\-+)¦([A-Za-z0-9]+\.+)¦([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)¦(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i #2, you are correct that some hosts block this and render this check essentially useless. The only way to confirm 100% is to send a confirmation e-mail.
#3, @henry_o's first bit of code (dealing with cc: and bcc:) are all I have ever needed to use.