<?php
# fonction du viralMail
include($BIB_VIRAL_MAIL);



############################
# Mailing constants
############################
# nombre de microsecondes à attendre
$SLEEP_MICROSECONDS = 0;

function waitMail($email, $fd, $nbMails)
{
	global $SLEEP_MICROSECONDS;
	
	usleep($SLEEP_MICROSECONDS);
	
	if ($nbMails < 2 or $nbMails % 1000 == 0)
	{
		echo "<br>".$nbMails." ".$email;
		flush();
	}
	
	fwrite($fd, $email."\n");
}

############################
# fonction qui ajoute le tag de désinscription
# unregisterTag(Id du membre,message)
############################
function unregisterTag($MId,$msg)
{
	# recupération des infos du membre
	# on utilise les fonctions de cryptage de l'affiliation
	# on l'insere dans le message
	return ereg_replace("__UNREGISTER__",cryptMember(getmember($MId, false)),$msg);
}

############################
# fonction d'envoie d'un mail via le system ViralGames
############################
function generic_mail($email, $subject, $message, $headers,$message_text="",$attachement_content="",$attachement_name="")
{
	global $VIRAL_MAIL_SPOOL;
	
	//debug("mail : ".$message);
	//viral_mail($VIRAL_MAIL_SPOOL,$email, $subject, $message, $headers,$message_text,$attachement_content,$attachement_name);
	//$subject = mb_encode_mimeheader($subject,'ISO-8859-1');
	//$headers = mb_encode_mimeheader($headers,'ISO-8859-15');
	//mail($email, $subject, $message, $headers);
	viral_mail($VIRAL_MAIL_SPOOL,$email, $subject, $message, $headers,$message_text,$attachement_content,$attachement_name);
	
}

function replaceTag($tag, $value, $msg)
{
	return str_replace($tag, $value, $msg);
}

function loadTemplate($aFile)
{
	/*
	$tabIn = file($aFile);
	$msg = "";
	
	for($k=0;$k<sizeof($tabIn);$k++)
    	$msg .= $tabIn[$k]."\n";
    
    return $msg;
    */
    
    return file_get_contents($aFile);
}

#########################
# Fonction de remplacement des tags dans un mail
#########################
function traiterTags($MailingString,$EmailInfos,$MailingId,$IsBat=0)
{
	$Tags = main_db_query("SELECT * FROM Tags WHERE MailingId='".$MailingId."'");
	while($Tag = db_fetch_assoc($Tags))
	{
		/*$len_InfoName = strlen($Tag["InfoName"]);
		$InfoName = substr($Tag["InfoName"], 0, $len_InfoName - 1);*/
		
		//debug("len_InfoName:".$len_InfoName." - InfoName:".$InfoName." - Original : ".$Tag["InfoName"]);
		
		$MailingString = replaceTag($Tag["Name"], $EmailInfos[$Tag["InfoName"]], $MailingString);
	}
	//renseigne l'id du contact pour la page mirroir
	if($IsBat)
		$MailingString = replaceTag("__CONTACT__", encrypt($EmailInfos['Id'])."&isBat=1", $MailingString);
	else
		$MailingString = replaceTag("__CONTACT__", encrypt($EmailInfos['Id']), $MailingString);
	
	return $MailingString;
}
?>