Hi guys

The below function works like a charm I just need help because it sends the mail twice instead of once. Can anybody please shed some light as to why its being sent twice.

function friends_invitechecker(){

global $user;

$uid_to = $_POST['inviteto'];

$message = $_POST['invitemessage'];

$author = $user->mail;

$uid_to = strtolower($uid_to); // we convert all text to lowercase

//this is the expression that does all the validation

$regex = "/(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/";

if(!preg_match_all($regex, $uid_to, $newto)){

print '

To:* One or more email addresses you entered seems to be invalid...

';

}

if ($message == null){
$message .= t("I set up a ". variable_get('site_name','Drupal') ." profile where I have access to my company's network and I want to add you as a ");
$message .= t("friend so you can see it. First, you need to join ". variable_get('site_name','Drupal') ." ! Once you join, you can also create your own");
$message .= t("profile. \n\n Thanks,\n\n ". $user->name ." \n\n Here's the link: ". variable_get('site_name','Drupal') ."?q=user/register");
}

if ($author == null | $uid_to == null){

print '

Error: Please Fill In All Required Fields...

';

}
else{

// Bundle up the variables into a structured array for altering.

$message = array(

'id' => 'freinds-friends_invite',

'to' => $uid_to,

'from' => isset($author) ? $author : $default_from,

'language' => $language,

'params' => $params,

'subject' => t('Friend Invite Request'),

'body' => t($message),

);

// Build the default headers

$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
if ($author) {
$headers['From'] = $headers['Reply-To'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $author;
}

$message['headers'] = $headers;

// Concatenate and wrap the e-mail body.

$message['body'] = is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']);

// Optionally send e-mail.

$message['result'] = drupal_mail_send($message);

// Log errors
if($message['result']){
print '

Your invite request has been successfully been sent...

';
}

else{

watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR);

drupal_set_message(t('Unable to send e-mail. Please contact the site admin, if the problem persists.'), 'error');

}
}

}

Thanks in advance...