<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.7

Copyright (C) 2006-2007  Sylvain FATOME
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

/********************************/
/* Fonctions d'envoi des emails */
/********************************/

// Création des destinataires centre de gestion
function cre_dest_cdg($tab,$dir,$tec)
{
	$tab_dest=$tab;

	$row = lit_enr_cdg();
	if (is_array($row))
	{
		extract ($row);
		if ($dir==TRUE)
			$tab_dest[$cge_emaild]="Centre de gestion (Service direction)";
		if ($tec==TRUE)
		{
			if (array_key_exists($cge_emailt, $tab_dest)==false)
				$tab_dest[$cge_emailt]="Centre de gestion (Service technique)";
		}
	}
	return $tab_dest;	
}

// Création des destinataires agence
function cre_dest_age($code,$tab,$dir,$tec)
{
	$tab_dest=$tab;

	$row = lit_enr_age($code);
	if (is_array($row))
	{
		extract ($row);
		if ($dir==TRUE)
			$tab_dest[$age_emaild]="Agence ".$age_nom." (Service direction)";
		if ($tec==TRUE)
		{
			if (array_key_exists($age_emailt, $tab_dest)==false)
				$tab_dest[$age_emailt]="Agence ".$age_nom." (Service technique)";
		}
	}
	return $tab_dest;	
}

// Création des destinataires client
function cre_dest_cli($code,$tab,$dir,$tec)
{
	$tab_dest=$tab;

	$row = lit_enr_cli($code);
	if (is_array($row))
	{
		extract ($row);
		if ($cli_email <> "") // Contact Principal
			$tab_dest[$cli_email]=$cli_civilite." ".$cli_prenom." ".$cli_nom;
							
		$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND `ccli_email`<>''";
		if (($dir==TRUE) OR ($tec==TRUE))
		{
			$rexist.=" AND (";
			if ($dir==TRUE)
				$rexist.=" `ccli_maild` = '1'";
			if ($tec==TRUE)
			{
				if ($dir==TRUE)
					$rexist.=" OR `ccli_mailt` = '1'";
				else
					$rexist.=" `ccli_mailt` = '1'";
			}
			$rexist.=")";
		}
		$rexist.=" AND `ccli_email` <> '".$cli_email."'";
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		$rs2 = mysql_query($rexist) or die(mysql_error());
		if (mysql_num_rows($rs2) > 0)
		{
			while ($resultat = mysql_fetch_array($rs2)) 
			{
				extract($resultat);
				if (array_key_exists($ccli_email, $tab_dest)==false)
					$tab_dest[$ccli_email]=$ccli_civilite." ".$ccli_prenom." ".$ccli_nom;
			}
		}
		mysql_free_result($rs2);
		mysql_close($conn2);
	}
	return $tab_dest;	
}

// Création des destinataires donneurs d'ordres
function cre_dest_do($code,$tab,$dir,$tec)
{
	$tab_dest=$tab;

	$row = lit_enr_do($code);
	if (is_array($row))
	{
		extract ($row);
		if ($dir==TRUE)
			$tab_dest[$do_emaild]=$do_nom." (Service direction)";
		if ($tec==TRUE)
		{
			if (array_key_exists($do_emailt, $tab_dest)==false)
				$tab_dest[$do_emailt]=$do_nom." (Service technique)";
		}
	}
	return $tab_dest;	
}

// Création des destinataires sous-traitants
function cre_dest_st($code,$tab,$dir,$tec)
{
	$tab_dest=$tab;

	$row = lit_enr_st($code);
	if (is_array($row))
	{
		extract ($row);
		if ($dir==TRUE)
			$tab_dest[$st_emaild]=$st_nom." (Service direction)";
		if ($tec==TRUE)
		{
			if (array_key_exists($st_emailt, $tab_dest)==false)
				$tab_dest[$st_emailt]=$st_nom." (Service technique)";
		}
	}
	return $tab_dest;	
}

// Envoi des emails "classiques"
function envoi_mail($sujet,$type,$mode,$orig,$dest,$code,$pass,$total,$copieadmin)
{
	global
		$chemin_logos,
		$admin_name,
		$admin_mail,
		$smtp_nom,
		$smtp_port,
		$smtp_login,
		$smtp_mdp,
		$code_agence,
		$code_client,
		$code_util,
		$niv_util;

	$tab_dest=array();		

	$mail = new PHPMailer();
	$mail->Host = $smtp_nom;
	$mail->Port = $smtp_port;
	$mail->Helo = "localhost.localdomain";
	if (trim($smtp_login)<>"")
	{
		$mail->Username = $smtp_login;
		$mail->Password = $smtp_mdp;
		$mail->SMTPAuth = true;
	}
	else
	{
		$mail->Username = "";
		$mail->Password = "";
		$mail->SMTPAuth = false;
	}
	$mail->IsHTML(TRUE);
	$mail->Mailer = "smtp";

	// Entête de l'email
	$mail->Priority = 1;

	//Expédié par
	$mail->From = $admin_mail;
	$mail->FromName = $admin_name;
	$mail->Sender = $admin_mail;

	//Réponse automatique à...
	$mail->AddReplyTo($admin_mail, $admin_name);

	// Destinataires

	if ($dest == "wm") // Le destinataire est le webmaster
	{
		$mail->AddAddress($admin_mail,$admin_name);
	}
	else // Ce n'est pas uniquement le Webmaster
	{
		// Création des destinataires
		switch ($type) // Analyse du type d'opération
		{
			// Agence
			case "age" :
					if ($dest == "age")
						$tab_dest=cre_dest_age($code,$tab_dest,TRUE,TRUE);					

					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					
					break;
						
			// Centre de Gestion
			case "cdg" :
					$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Client
			case "cli" :
					if ($dest == "cli")
					{
						// Si on crée un compte client, ou si on modifie le MDP, ou si déménagement copie à direction direction uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MDP") OR (str_majuscules($mode) == "DEM"))
							$tab_dest = cre_dest_cli($code,$tab_dest,TRUE,FALSE);
						else
							$tab_dest = cre_dest_cli($code,$tab_dest,FALSE,FALSE);
					}
					if ($dest == "do")
					{
						$row = lit_enr_cli($code);
						if (is_array($row))
						{
							extract ($row);
							$tab_dest=cre_dest_do($cli_codedo,$tab_dest,TRUE,TRUE);
						}
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Contrat
			case "con" :
					if ($dest == "cli")
					{
						$row = lit_enr_cocl($code);
						extract ($row);
	
						// Si on crée un contrat, on le modifie, ou on en accuse réception, copies à direction et technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD") OR (str_majuscules($mode) == "ENV") OR (str_majuscules($mode) =="AR"))
							$tab_dest = cre_dest_cli($cocl_codecli,$tab_dest,TRUE,TRUE);
						else
							$tab_dest = cre_dest_cli($cocl_codecli,$tab_dest,FALSE,FALSE);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_cocl($code);
						extract ($row);
						$tab_dest=cre_dest_age($cocl_codeage,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Si demande
			case "dde" :
					if (str_majuscules($mode) == "CRE") // On est en mode demande
					{
						$row = lit_enr_dde($code);
						extract ($row);
						if ($dde_codedded <> "VA")
						{
							$row = lit_enr_dded($dde_codedded);
							extract ($row);
							$tab_dest[$dded_email]=$dded_desc;
						}
						else // Le destinataire est "Votre agence"
						{
							if ($niv_util == 1) // On vérifie que c'est un client qui demande
							{
								$row = lit_enr_cli($code_util);
								if (is_array($row))
								{
									extract ($row);
									if (($cli_prop <> NULL) AND ($cli_prop <> "")) // Le client est géré par une agence
									{
										$row = lit_enr_age($cli_prop);
										extract ($row);
										$tab_dest[$age_email]="Agence ".$age_nom;
									}
									else // Le client n'est pas géré par une agence, on envoie à toutes les agences de sa zone
									{
										$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
										mysql_select_db(DBNAME);
										$rech = "SELECT * FROM `" .TBAGE. "`";
										$rech.="  WHERE `age_ouvert` = 1 AND `age_codepays`='".str_majuscules($cli_codepays)."'";
										$rs = mysql_query($rech) or die(mysql_error());
										if (mysql_num_rows($rs) >0)
										{
											while ($row = mysql_fetch_assoc($rs)) 
											{
												extract($row);
												$tab_depzc=tab_zcc_age_open($age_code,FALSE);
												if (count(tab_depzc)>0)
												{
													for ($i=0; $i<count($tab_depzc); $i++)
													{
														if (trim($tab_depzc[$i])<>"")
														{
															if (array_key_exists($age_email, $tab_dest)==false)
																$tab_dest[$age_email]="Agence ".$age_nom;
														}
													}
												}
											}
										}
										mysql_free_result($rs);
										mysql_close($conn);
									}
								}
							}
						}
					}
					if (str_majuscules($mode) == "AR") // On est en mode accusé de réception
					{
						$row = lit_enr_dde($code);
						extract ($row);
						$tab_dest[$dde_email]=$dde_civilite." ".$dde_prenom." ".$dde_nom;
					}
					break;
						
			// Donneur d'ordres
			case "do" :
					if ($dest == "do")
						$tab_dest=cre_dest_do($code,$tab_dest,TRUE,TRUE);					

					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					
					break;
						
			// Fichiers
			case "fic" :
					if ($dest == "age")
						$tab_dest=cre_dest_age($code_agence,$tab_dest,TRUE,TRUE);					
					if ($dest == "cli")
					{
						$row = lit_enr_age($code_client);
						extract ($row);

						$tab_dest = cre_dest_cli($code_client,$tab_dest,TRUE,TRUE);
					}
				break;

			// Intervention
			case "int" :
					if ($dest == "cli")
					{
						$row = lit_enr_int($code);
						extract ($row);
	
						// Si on crée une intervention, ou on la termine, copie à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "FAIT"))
							$tab_dest = cre_dest_cli($int_codecli,$tab_dest,FALSE,TRUE);
						else
							$tab_dest = cre_dest_cli($int_codecli,$tab_dest,FALSE,FALSE);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_int($code);
						extract ($row);
						$tab_dest=cre_dest_age($int_codeage,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "do")
					{
						$row = lit_enr_int($code);
						extract ($row);
						$tab_dest=cre_dest_do($int_codedo,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "st")
					{
						$row = lit_enr_int($code);
						extract ($row);
						$tab_dest=cre_dest_st($int_codest,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Opérateur
			case "ope" :
					if ($dest == "ope")
					{
						$row = lit_enr_ope($code);
						extract ($row);
						$tab_dest[$ope_email]=$ope_civilite." ".$ope_prenom." ".$ope_nom;
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Ordre sous-traitance
			case "ot" :
					if ($dest == "age")
					{
						$row = lit_enr_ot($code);
						extract ($row);
						$row = lit_enr_int($ot_codeint);
						extract ($row);
						$tab_dest=cre_dest_age($int_codeage,$tab_dest,TRUE,TRUE);
					}
					if ($dest == "st")
					{
						$row = lit_enr_ot($code);
						extract ($row);
						$tab_dest=cre_dest_st($ot_codest,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Technicien
			case "tec" :
					if ($dest == "age")
					{
						$row = lit_enr_tec($code);
						extract ($row);
						$tab_dest=cre_dest_age($tec_codeage,$tab_dest,TRUE,TRUE);
					}
					if ($dest == "tec")
					{
						$row = lit_enr_tec($code);
						extract ($row);
						$tab_dest[$tec_email]=$tec_civilite." ".$tec_prenom." ".$tec_nom;
					}
					if ($dest == "do")
					{
						$row = lit_enr_tec($code);
						extract ($row);
						$tab_dest=cre_dest_do($tec_codedo,$tab_dest,TRUE,TRUE);
					}
					if ($dest == "st")
					{
						$row = lit_enr_tec($code);
						extract ($row);
						$tab_dest=cre_dest_st($tec_codest,$tab_dest,TRUE,TRUE);
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Relevé de parc
			case "rel" :
					if ($dest == "cli")
					{
						$row = lit_enr_rel($code);
						extract ($row);
	
						// Si on crée un contrat, ou si on le modifie, copies à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
							$tab_dest = cre_dest_cli($rel_codecli,$tab_dest,FALSE,TRUE);
						else
							$tab_dest = cre_dest_cli($rel_codecli,$tab_dest,FALSE,FALSE);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rel($code);
						extract ($row);
						$tab_dest=cre_dest_age($rel_codeage,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Relevé Licence
			case "rlic" :
					if ($dest == "cli")
					{
						$row = lit_enr_rlic($code);
						extract ($row);
	
						// Si on crée un contrat, ou si on le modifie, copies à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
							$tab_dest = cre_dest_cli($rlic_codecli,$tab_dest,FALSE,TRUE);
						else
							$tab_dest = cre_dest_cli($rlic_codecli,$tab_dest,FALSE,FALSE);
	
						// On envoie une copie au contact principal du site 
						if ($rlic_idsite>0)
						{
							$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
	
							$rexist = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$rlic_idsite'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								if ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $tab_dest)==false)
										$tab_dest[$ccli_email]=$ccli_civilite." ".$ccli_prenom." ".$ccli_nom;
								}
							}
							mysql_free_result($rs2);
							mysql_close($conn2);
						}							
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rlic($code);
						extract ($row);
						$tab_dest=cre_dest_age($rlic_codeage,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Relevé Produit
			case "rpdt" :
					if ($dest == "cli")
					{
						$row = lit_enr_rpdt($code);
						extract ($row);
	
						// Si on crée un contrat, ou si on le modifie, copies à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
							$tab_dest = cre_dest_cli($rpdt_codecli,$tab_dest,FALSE,TRUE);
						else
							$tab_dest = cre_dest_cli($rpdt_codecli,$tab_dest,FALSE,FALSE);
	
						// On envoie une copie au contact principal du site 
						if ($rpdt_idsite>0)
						{
							$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
	
							$rexist = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$rpdt_idsite'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								if ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $tab_dest)==false)
										$tab_dest[$ccli_email]=$ccli_civilite." ".$ccli_prenom." ".$ccli_nom;
								}
							}
							mysql_free_result($rs2);
							mysql_close($conn2);
						}							
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rpdt($code);
						extract ($row);
						$tab_dest=cre_dest_age($rpdt_codeage,$tab_dest,TRUE,TRUE);					
					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Déclaration de problème
			case "rpbm" :
					if ($dest == "cli")
					{
						$row = lit_enr_rpbm($code);
						extract ($row);
						
						// Si on crée un contrat, ou si on le modifie, copies à technique et contact du site (si existe) uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
							$tab_dest = cre_dest_cli($rpbm_codecli,$tab_dest,FALSE,TRUE);
						else
							$tab_dest = cre_dest_cli($rlic_codecli,$tab_dest,FALSE,FALSE);

						// On envoie une copie au contact principal du site 
						if ($rpbm_idsite>0)
						{
							$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
	
							$rexist = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$rpbm_idsite'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								if ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $tab_dest)==false)
										$tab_dest[$ccli_email]=$ccli_civilite." ".$ccli_prenom." ".$ccli_nom;
								}
							}
							mysql_free_result($rs2);
							mysql_close($conn2);
						}							
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rpbm($code);
						extract ($row);
						
						switch ($rpbm_typrel)
						{
							case 1 :
								$row = lit_enr_rlic($rpbm_coderlp);
								extract ($row);
								$codeage=$rlic_codeage;
								break;
							case 2 :
								$row = lit_enr_rpdt($rpbm_coderlp);
								extract ($row);
								$codeage=$rpdt_codeage;
								break;
						}
						
						$tab_dest=cre_dest_age($codeage,$tab_dest,TRUE,TRUE);					

					}
					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					break;
						
			// Sous-traitant
			case "st" :
					if ($dest == "st")
						$tab_dest=cre_dest_st($code,$tab_dest,TRUE,TRUE);					

					if ($dest == "cdg")
						$tab_dest=cre_dest_cdg($tab_dest,TRUE,TRUE);
					
					break;
						
			default : break;
		}
	
	
		foreach ($tab_dest as $key=>$value)
		{
			$mail->AddAddress($key,$value);
		}
		
		//Copie du mail à l'administrateur ?
		if (($copieadmin == TRUE) AND ($admin_mail <> ""))
			$mail->AddBCC($admin_mail,$admin_name);
	}

	// Corps de l'email
	$txtcorps = "";
	$txtcorps = cre_corps_txtm($type,$mode,$orig,$dest,$code,$pass,$total);

	if ((($tab_dest && count($tab_dest) > 0)  && (trim($txtcorps)<>"") && (trim($sujet)<>"")) || ($dest == "wm"))
	{
		// Logo ?
		if (stristr($txtcorps,"%logo_cge%") OR stristr($txtcorps,"%logo_age%") OR stristr($txtcorps,"%logo_do%")  OR stristr($txtcorps,"%logo_st%"))
		{
			// Il y a un logo centre de gestion
			if (stristr($txtcorps,"%logo_cge%"))
			{
				$mail->AddEmbeddedImage($chemin_logos."/logcge.png" , "logo_cge", "logcge.png", "base64", "image/png" );

				// Mise à l'échelle logo
				$infos_img = getimagesize($chemin_logos."/logcge.png"); 
				$largeur = $infos_img[0];
				$hauteur = $infos_img[1];
				$coeffl=1;
				$coeffh=1;
				if ($largeur>195)
					$coeffl=($largeur/195);
				if ($hauteur>36)
					$coeffh=($hauteur/36);
				if ($coeffl>$coeffh)
					$coeff=$coeffl;
				else
					$coeff=$coeffh;
				$hauteur=round($hauteur/$coeff);
				$largeur=round($largeur/$coeff);

				$txtcorps = ereg_replace("%logo_cge%",'<img src="cid:logo_cge" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_siege">',$txtcorps);
			}

			// Il y a un logo agence
			if (stristr($txtcorps,"%logo_age%"))
			{
				$codeage="";
				// Balayage des types
				switch ($type)
				{
					// Agence
					case "age" :
							$codeage=give_codeage($code);;
							break;
								
					// Client
					case "cli" :
							$row = lit_enr_cli($code);
							if (is_array($row))
							{
								extract($row);
								if ($cli_prop<>"")
									$codeage=give_codeage($cli_prop);
							}
							break;
								
					// Contrat
					case "con" :
							$row = lit_enr_cocl($code);
							if (is_array($row))
							{
								extract($row);
								if ($cocl_codeage<>"")
									$codeage=give_codeage($cocl_codeage);
							}
							break;
			
					// Intervention
					case "int" :
							$row = lit_enr_int($code);
							if (is_array($row))
							{
								extract($row);
								if ($int_codeage<>"")
									$codeage=give_codeage($int_codeage);
							}
							break;
								
					// Ordre Sous-traitance
					case "ot" :
							$row = lit_enr_ot($code);
							if (is_array($row))
							{
								extract($row);
								$row = lit_enr_int($ot_codeint);
								if (is_array($row))
								{
									extract($row);
									if ($int_codeage<>"")
										$codeage=give_codeage($int_codeage);
								}
							}
							break;
								
					// Relevé (Dossier)
					case "rel" :
							$row = lit_enr_rel($code);
							if (is_array($row))
							{
								extract($row);
								if ($rel_codeage<>"")
									$codeage=give_codeage($rel_codeage);
							}
							break;
								
					// Relevé (Licence)
					case "rlic" :
							$row = lit_enr_rlic($code);
							if (is_array($row))
							{
								extract($row);
								if ($rlic_codeage<>"")
									$codeage=give_codeage($rlic_codeage);
							}
							break;
								
					// Relevé (Matériel)
					case "rpdt" :
							$row = lit_enr_rpdt($code);
							if (is_array($row))
							{
								extract($row);
								if ($rpdt_codeage<>"")
									$codeage=give_codeage($rpdt_codeage);
							}
							break;
								
					// Déclaration de problème
					case "rpbm" :
							$row = lit_enr_rpbm($code);
							if (is_array($row))
							{
								extract($row);
								switch ($rpbm_typrel)
								{
									case 1 :
										$row = lit_enr_rlic($rpbm_coderlp);
										if (is_array($row))
										{
											extract($row);
											if ($rlic_codeage<>"")
												$codeage=give_codeage($rlic_codeage);
										}
										break;
									case 2 :
										$row = lit_enr_rpdt($rpbm_coderlp);
										if (is_array($row))
										{
											extract($row);
											if ($rpdt_codeage<>"")
												$codeage=give_codeage($rpdt_codeage);
										}
										break;
									default :
										break;
								}
							}
							break;
								
					// Technicien
					case "tec" :
							$row = lit_enr_tec($code);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codeage<>"")
									$codeage=give_codeage($tec_codeage);
							}
							break;
					
					default : break;
				}
				
				if ($codeage<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codeage).".png" , "logo_age", strtolower($codeage)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codeage).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_age%",'<img src="cid:logo_age" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_agence">',$txtcorps);
				}
			}

			// Il y a un logo donneur d'ordres
			if (stristr($txtcorps,"%logo_do%"))
			{
				$codedo="";
				// Balayage des types
				switch ($type)
				{
					// Client
					case "cli" :
							$row = lit_enr_cli($code);
							if (is_array($row))
							{
								extract($row);
								if ($cli_codedo<>"")
									$codedo=give_codedo($cli_codedo);
							}
							break;
								
					// Intervention
					case "int" :
							$row = lit_enr_int($code);
							if (is_array($row))
							{
								extract($row);
								if ($int_codedo<>"")
									$codedo=give_codedo($int_codedo);
							}
							break;
								
					// Technicien
					case "tec" :
							$row = lit_enr_tec($code);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codedo<>"")
									$codedo=give_codedo($tec_codedo);
							}
							break;
					
					default : break;
				}

				if ($codedo<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codedo).".png" , "logo_do", strtolower($codedo)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codedo).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_do%",'<img src="cid:logo_do" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_do">',$txtcorps);
				}
			}

			// Il y a un logo sous-traitant
			if (stristr($txtcorps,"%logo_st%"))
			{
				$codest="";
				// Balayage des types
				switch ($type)
				{
					// Intervention
					case "int" :
							$row = lit_enr_int($code);
							if (is_array($row))
							{
								extract($row);
								if ($int_codest<>"")
									$codest=give_codest($int_codest);
							}
							break;
								
					// Ordre de sous-traitance
					case "ot" :
							$row = lit_enr_ot($code);
							if (is_array($row))
							{
								extract($row);
								if ($ot_codest<>"")
									$codest=give_codest($ot_codest);
							}
							break;
								
					// Technicien
					case "tec" :
							$row = lit_enr_tec($code);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codest<>"")
									$codest=give_codest($tec_codest);
							}
							break;
					
					default : break;
				}
				
				if ($codest<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codest).".png" , "logo_st", strtolower($codest)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codest).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_st%",'<img src="cid:logo_st" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_st">',$txtcorps);
				}
			}
		}		

		$mail->Subject = $sujet;
		$mail->Body = $txtcorps;

		$sent = $mail->Send();
	}
	else
		$sent=false;

	$mail->SmtpClose();
	$mail->ClearAllRecipients();
	unset($mail);
}

function give_codeage($code)
{
	$codeage="";
	$row=lit_enr_age($code);
	if (is_array($row))
	{
		extract($row);
		$codeage=$age_code;
	}
	return $codeage;
}

function give_codedo($code)
{
	$codedo="";
	$row=lit_enr_do($code);
	if (is_array($row))
	{
		extract($row);
		$codedo=$do_code;
	}
	return $codedo;
}

function give_codest($code)
{
	$codest="";
	$row=lit_enr_st($code);
	if (is_array($row))
	{
		extract($row);
		$codest=$st_code;
	}
	return $codest;
}

// Envoi des emails aux administrateurs seulement
function envoi_mailwm($sujet,$type,$mode,$orig,$dest,$code,$pass,$total)
{
	global
		$chemin_logos,
		$admin_name,
		$admin_mail,
		$smtp_nom,
		$smtp_port,
		$smtp_login,
		$smtp_mdp;
		
	$mail = new PHPMailer();
	$mail->Host = $smtp_nom;
	$mail->Port = $smtp_port;
	$mail->Helo = "localhost.localdomain";
	if (trim($smtp_login)<>"")
	{
		$mail->Username = $smtp_login;
		$mail->Password = $smtp_mdp;
		$mail->SMTPAuth = true;
	}
	else
	{
		$mail->Username = "";
		$mail->Password = "";
		$mail->SMTPAuth = false;
	}
	$mail->IsHTML(TRUE);
	$mail->Mailer = "smtp";

	// Entête de l'email
	$mail->Priority = 1;

	//Expédié par
	$mail->From = $admin_mail;
	$mail->FromName = $admin_name;
	$mail->Sender = $admin_mail;

	//Réponse automatique à...
	$mail->AddReplyTo($admin_mail, $admin_name);

	// Le destinataire est le webmaster
	$mail->AddAddress($admin_mail,$admin_name);
	
	// Corps de l'email
	$txtcorps = "";
	$txtcorps = cre_corps_txtm($type,$mode,$orig,$dest,$code,$pass,$total);

	if ((trim($txtcorps)<>"") && (trim($sujet)<>""))
	{
		// Logos ?
		if (stristr($txtcorps,"%logo_cge%") OR stristr($txtcorps,"%logo_age%") OR stristr($txtcorps,"%logo_do%")  OR stristr($txtcorps,"%logo_st%"))
		{
			// Il y a un logo centre de gestion
			if (stristr($txtcorps,"%logo_cge%"))
			{
				$mail->AddEmbeddedImage($chemin_logos."/logcge.png" , "logo_cge", "logcge.png", "base64", "image/png" );

				// Mise à l'échelle logo
				$infos_img = getimagesize($chemin_logos."/logcge.png"); 
				$largeur = $infos_img[0];
				$hauteur = $infos_img[1];
				$coeffl=1;
				$coeffh=1;
				if ($largeur>195)
					$coeffl=($largeur/195);
				if ($hauteur>36)
					$coeffh=($hauteur/36);
				if ($coeffl>$coeffh)
					$coeff=$coeffl;
				else
					$coeff=$coeffh;
				$hauteur=round($hauteur/$coeff);
				$largeur=round($largeur/$coeff);

				$txtcorps = ereg_replace("%logo_cge%",'<img src="cid:logo_cge" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_siege">',$txtcorps);
			}

			// Il y a un logo agence
			if (stristr($txtcorps,"%logo_age%"))
			{
				$codeage="";
				// Balayage des types
				switch ($type)
				{
					// Agence
					case "age" :
							$codeage=give_codeage($code);;
							break;
								
					// Client
					case "cli" :
							$row = lit_enr_cli($code);
							if (is_array($row))
							{
								extract($row);
								if ($cli_prop<>"")
									$codeage=give_codeage($cli_prop);
							}
							break;
								
					// Contrat
					case "con" :
							$row = lit_enr_cocl($code);
							if (is_array($row))
							{
								extract($row);
								if ($cocl_codeage<>"")
									$codeage=give_codeage($cocl_codeage);
							}
							break;
			
					// Intervention
					case "int" :
							$row = lit_enr_int($code);
							if (is_array($row))
							{
								extract($row);
								if ($int_codeage<>"")
									$codeage=give_codeage($int_codeage);
							}
							break;
								
					// Ordre de sous-traitance
					case "ot" :
							$row = lit_enr_ot($code);
							if (is_array($row))
							{
								extract($row);
								$row = lit_enr_int($ot_codeint);
								if (is_array($row))
								{
									extract($row);
									if ($int_codeage<>"")
										$codeage=give_codeage($int_codeage);
								}
							}
							break;
								
					// Relevé (Dossier)
					case "rel" :
							$row = lit_enr_rel($code);
							if (is_array($row))
							{
								extract($row);
								if ($rel_codeage<>"")
									$codeage=give_codeage($rel_codeage);
							}
							break;
								
					// Relevé (Licence)
					case "rlic" :
							$row = lit_enr_rlic($code);
							if (is_array($row))
							{
								extract($row);
								if ($rlic_codeage<>"")
									$codeage=give_codeage($rlic_codeage);
							}
							break;
								
					// Relevé (Matériel)
					case "rpdt" :
							$row = lit_enr_rpdt($code);
							if (is_array($row))
							{
								extract($row);
								if ($rpdt_codeage<>"")
									$codeage=give_codeage($rpdt_codeage);
							}
							break;
								
					// Déclaration de problème
					case "rpbm" :
							$row = lit_enr_rpbm($code);
							if (is_array($row))
							{
								extract($row);
								switch ($rpbm_typerel)
								{
									case 1 :
										$row = lit_enr_rlic($rpbm_coderlp);
										if (is_array($row))
										{
											extract($row);
											if ($rlic_codeage<>"")
												$codeage=give_codeage($rlic_codeage);
										}
										break;
									case 2 :
										$row = lit_enr_rpdt($rpbm_coderlp);
										if (is_array($row))
										{
											extract($row);
											if ($rpdt_codeage<>"")
												$codeage=give_codeage($rpdt_codeage);
										}
										break;
									default :
										break;
								}
							}
							break;
								
					// Technicien
					case "tec" :
							$row = lit_enr_tec($code);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codeage<>"")
									$codeage=give_codeage($tec_codeage);
							}
							break;
					
					default : break;
				}
				
				if ($codeage<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codeage).".png" , "logo_age", strtolower($codeage)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codeage).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_age%",'<img src="cid:logo_age" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_agence">',$txtcorps);
				}
			}

			// Il y a un logo donneur d'ordres
			if (stristr($txtcorps,"%logo_do%"))
			{
				$codedo="";
				// Balayage des types
				switch ($type)
				{
					// Client
					case "cli" :
							$row = lit_enr_cli($code);
							if (is_array($row))
							{
								extract($row);
								if ($cli_codedo<>"")
									$codedo=give_codeage($cli_codedo);
							}
							break;
								
					// Intervention
					case "int" :
							$row = lit_enr_int($code);
							if (is_array($row))
							{
								extract($row);
								if ($int_codedo<>"")
									$codedo=give_codedo($int_codedo);
							}
							break;
								
					// Technicien
					case "tec" :
							$row = lit_enr_tec($code);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codedo<>"")
									$codedo=give_codedo($tec_codedo);
							}
							break;
					
					default : break;
				}
				
				if ($codedo<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codedo).".png" , "logo_do", strtolower($codedo)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codedo).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_do%",'<img src="cid:logo_do" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_do">',$txtcorps);
				}
			}

			// Il y a un logo sous-traitant
			if (stristr($txtcorps,"%logo_st%"))
			{
				$codest="";
				// Balayage des types
				switch ($type)
				{
					// Intervention
					case "int" :
							$row = lit_enr_int($code);
							if (is_array($row))
							{
								extract($row);
								if ($int_codest<>"")
									$codest=give_codest($int_codest);
							}
							break;
								
					// Ordte sous-traitance
					case "ot" :
							$row = lit_enr_ot($code);
							if (is_array($row))
							{
								extract($row);
								if ($ot_codest<>"")
									$codest=give_codest($int_codest);
							}
							break;
								
					// Technicien
					case "tec" :
							$row = lit_enr_tec($code);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codest<>"")
									$codest=give_codest($tec_codest);
							}
							break;
					
					default : break;
				}
				
				if ($codest<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codest).".png" , "logo_st", strtolower($codest)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codest).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_st%",'<img src="cid:logo_st" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_st">',$txtcorps);
				}
			}
		}		


		$mail->Subject = $sujet;
		$mail->Body = $txtcorps;

		$sent = $mail->Send();
	}
	else
		$sent=false;

	$mail->SmtpClose();
	$mail->ClearAllRecipients();
	unset($mail);
}

// Envoi des emailings

// $typdest : cli, tec, age, ope
// $codedest : code du destinataire
// $typeexp : type de l'expéditeur (administrateur, cdg, agence)
// $codeexp : code de l'expéditeur (administrateur, cdg, agence)
// $txt : code du texte type utilisé
// $sujet : titre du message
function envoi_emailing($typedest,$typemail,$codedest,$typeexp,$codeexp,$txt,$sujet)
{
	global
		$chemin_logos,
		$admin_name,
		$admin_mail,
		$smtp_nom,
		$smtp_port,
		$smtp_login,
		$smtp_mdp;
		
	$mail = new PHPMailer();
	$mail->Host = $smtp_nom;
	$mail->Port = $smtp_port;
	$mail->Helo = "localhost.localdomain";
	if (trim($smtp_login)<>"")
	{
		$mail->Username = $smtp_login;
		$mail->Password = $smtp_mdp;
		$mail->SMTPAuth = true;
	}
	else
	{
		$mail->Username = "";
		$mail->Password = "";
		$mail->SMTPAuth = false;
	}
	$mail->IsHTML(TRUE);
	$mail->Mailer = "smtp";

	// Entête de l'email
	$mail->Priority = 1;

	// Destinataires
	switch ($typedest) // Analyse du type de destinataire
	{
		// Agence
		case "age" :
					$row = lit_enr_age($codedest);
					extract ($row);
					for ($i = 0; $i < 4; $i++)
					{
						if ($typemail[$i] == 1)
						{
							switch ($i)
							{
								case 0 :
									$tab_dest[$age_email]="Agence ".$age_nom;
									break;
								case 1 :
									if (array_key_exists($age_emaild, $tab_dest)==false)
										$tab_dest[$age_emaild]="Agence ".$age_nom." (Service direction)";
									break;
								case 2 :
									if (array_key_exists($age_emailc, $tab_dest)==false)
										$tab_dest[$age_emailc]="Agence ".$age_nom." (Service comptabilité)";
									break;
								case 3 :
									if (array_key_exists($age_emailt, $tab_dest)==false)
										$tab_dest[$age_emailt]="Agence ".$age_nom." (Service technique)";
									break;
								default :
									break;
							}
						}
					}
					break;
					
		// Client
		case "cli" :
					$row = lit_enr_cli($codedest);
					extract ($row);

					if ($typemail[0] == 1)
						$tab_dest[$cli_email]=$cli_civilite." ".$cli_prenom." ".$cli_nom;

					// Récupération des contacts
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);

					$rexist = "SELECT DISTINCT ccli_email FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code'";
					$rs2 = mysql_query($rexist) or die(mysql_error());
					if (mysql_num_rows($rs2) <> 0)
					{
						while ($resultat = mysql_fetch_array($rs2)) 
						{
							extract($resultat);

							for ($i = 1; $i < 4; $i++)
							{
								if ($typemail[$i] == 1)
								{
									switch ($i)
									{
										case 1 :
											if ($ccli_emaild == 1)
											{
												if (array_key_exists($ccli_email, $tab_dest)==false)
													$tab_dest[$ccli_email]=$ccli_civilite." ".$ccli_prenom." ".$ccli_nom;
											}
											break;
										case 2 :
											if ($ccli_emailc == 1)
											{
												if (array_key_exists($ccli_emailc, $tab_dest)==false)
													$tab_dest[$ccli_emailc]=$ccli_civilite." ".$ccli_prenom." ".$ccli_nom;
											}
											break;
										case 3 :
											if ($ccli_emailt == 1)
											{
												if (array_key_exists($ccli_emailt, $tab_dest)==false)
													$tab_dest[$ccli_emailt]=$ccli_civilite." ".$ccli_prenom." ".$ccli_nom;
											}
											break;
										default :
											break;
									}
								}
							}
						}
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;
					
		// Donneur d'ordres
		case "do" :
					$row = lit_enr_do($codedest);
					extract ($row);
					for ($i = 0; $i < 4; $i++)
					{
						if ($typemail[$i] == 1)
						{
							switch ($i)
							{
								case 0 :
									$tab_dest[$do_email]=$age_nom;
									break;
								case 1 :
									if (array_key_exists($do_emaild, $tab_dest)==false)
										$tab_dest[$do_emaild]=$do_nom." (Service direction)";
									break;
								case 2 :
									if (array_key_exists($do_emailc, $tab_dest)==false)
										$tab_dest[$do_emailc]=$do_nom." (Service comptabilité)";
									break;
								case 3 :
									if (array_key_exists($do_emailt, $tab_dest)==false)
										$tab_dest[$do_emailt]=$do_nom." (Service technique)";
									break;
								default :
									break;
							}
						}
					}
					break;
					
		// Opérateur
		case "ope" :
					$row = lit_enr_ope($codedest);
					extract ($row);
					$tab_dest[$ope_email]=$ope_civilite." ".$ope_prenom." ".$ope_nom;
					break;
					
		// Sous-traitant
		case "st" :
					$row = lit_enr_st($codedest);
					extract ($row);
					for ($i = 0; $i < 4; $i++)
					{
						if ($typemail[$i] == 1)
						{
							switch ($i)
							{
								case 0 :
									$tab_dest[$st_email]=$st_nom;
									break;
								case 1 :
									if (array_key_exists($st_emaild, $tab_dest)==false)
										$tab_dest[$st_emaild]=$st_nom." (Service direction)";
									break;
								case 2 :
									if (array_key_exists($st_emailc, $tab_dest)==false)
										$tab_dest[$st_emailc]=$st_nom." (Service comptabilité)";
									break;
								case 3 :
									if (array_key_exists($st_emailt, $tab_dest)==false)
										$tab_dest[$st_emailt]=$st_nom." (Service technique)";
									break;
								default :
									break;
							}
						}
					}
					break;
					
		// Technicien
		case "tec" :
		case "tecd" :
		case "tecs" :
					$row = lit_enr_tec($codedest);
					extract ($row);
					$tab_dest[$tec_email]=$tec_civilite." ".$tec_prenom." ".$tec_nom;
					break;
					
		default : break;
	}

	foreach ($tab_dest as $key=>$value)
	{
		$mail->AddAddress($key,$value);
	}
	
	// Entête de l'email

	$nom="";
	$email="";
	
	switch ($typeexp) // Analyse du type de l'expéditeur
	{
		// Administrateur
		case "adm" :
					$nom = $admin_name;
					$email = $admin_mail;
					break;
					

		// Agence
		case "age" :
					$row = lit_enr_age($codeexp);
					extract ($row);
					$nom=$age_nom;
					$email=$age_email;
					break;
					
		// Centre de gestion
		case "cdg" :
					$row = lit_enr_cdg();
					extract($row);
					$nom=$cge_nom;
					$email=$cge_email;
					break;

		// Donneur d'ordres
		case "do" :
					$row = lit_enr_do($codeexp);
					extract($row);
					$nom=$do_nom;
					$email=$do_email;
					break;

		// Sous-traitant
		case "st" :
					$row = lit_enr_st($codeexp);
					extract($row);
					$nom=$st_nom;
					$email=$st_email;
					break;

		default : break;
	}
					
	//Expédié par
	$mail->From = $email;
	$mail->FromName = $nom;
	$mail->Sender = $email;

	//Réponse automatique à...
	$mail->AddReplyTo($email, $nom);

	// Corps de l'email
	$txtcorps = "";	
	$txtcorps = cre_corps_txtem($typedest,$codedest,$typeexp,$codeexp,$txt);

	if(($tab_dest && count($tab_dest) > 0) && (trim($txtcorps)<>"") && (trim($sujet)<>""))
	{
		// Logo ?
		if (stristr($txtcorps,"%logo_cge%") OR stristr($txtcorps,"%logo_age%") OR stristr($txtcorps,"%logo_do%") OR stristr($txtcorps,"%logo_st%"))
		{
			// Il y a un logo centre de gestion
			if (stristr($txtcorps,"%logo_cge%"))
			{
				$mail->AddEmbeddedImage($chemin_logos."/logcge.png" , "logo_cge", "logcge.png", "base64", "image/png" );

				// Mise à l'échelle logo
				$infos_img = getimagesize($chemin_logos."/logcge.png"); 
				$largeur = $infos_img[0];
				$hauteur = $infos_img[1];
				$coeffl=1;
				$coeffh=1;
				if ($largeur>195)
					$coeffl=($largeur/195);
				if ($hauteur>36)
					$coeffh=($hauteur/36);
				if ($coeffl>$coeffh)
					$coeff=$coeffl;
				else
					$coeff=$coeffh;
				$hauteur=round($hauteur/$coeff);
				$largeur=round($largeur/$coeff);

				$txtcorps = ereg_replace("%logo_cge%",'<img src="cid:logo_cge" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_siege">',$txtcorps);
			}

			// Il y a un logo agence
			if (stristr($txtcorps,"%logo_age%"))
			{
				$codeage="";
				
				// Destinataires
				switch ($typedest) // Analyse du type de destinataire
				{
					// Client
					case "cli" :
							$row = lit_enr_cli($codedest);
							if (is_array($row))
							{
								extract($row);
								if ($cli_prop<>"")
									$codeage=give_codeage($cli_prop);
							}
							break;
								
					// Technicien
					case "tec" :
							$row = lit_enr_tec($codedest);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codeage<>"")
									$codeage=give_codeage($tec_codeage);
							}
							break;
								
					default : break;
				}

				if ($codeage<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codeage).".png" , "logo_age", strtolower($codeage)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codeage).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_age%",'<img src="cid:logo_age" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_agence">',$txtcorps);
				}
			}

			// Il y a un logo donneur d'ordres
			if (stristr($txtcorps,"%logo_do%"))
			{
				$codedo="";
				
				// Destinataires
				switch ($typedest) // Analyse du type de destinataire
				{
					// Technicien donneur d'ordres
					case "tecd" :
							$row = lit_enr_tec($codedest);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codedo<>"")
									$codedo=give_codedo($tec_codedo);
							}
							break;
								
					default : break;
				}

				if ($codedo<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codedo).".png" , "logo_do", strtolower($codedo)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codedo).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_do%",'<img src="cid:logo_do" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_do">',$txtcorps);
				}
			}

			// Il y a un logo sous-traitant
			if (stristr($txtcorps,"%logo_st%"))
			{
				$codest="";
				
				// Destinataires
				switch ($typedest) // Analyse du type de destinataire
				{
					// Technicien donneur d'ordres
					case "tecs" :
							$row = lit_enr_tec($codedest);
							if (is_array($row))
							{
								extract($row);
								if ($tec_codest<>"")
									$codest=give_codest($tec_codest);
							}
							break;
								
					default : break;
				}

				if ($codest<>"")
				{
					$mail->AddEmbeddedImage($chemin_logos."/log".strtolower($codest).".png" , "logo_st", strtolower($codest)."png", "base64", "image/png" );

					// Mise à l'échelle logo
					$infos_img = getimagesize($chemin_logos."/log".strtolower($codest).".png"); 
					$largeur = $infos_img[0];
					$hauteur = $infos_img[1];
					$coeffl=1;
					$coeffh=1;
					if ($largeur>195)
						$coeffl=($largeur/195);
					if ($hauteur>36)
						$coeffh=($hauteur/36);
					if ($coeffl>$coeffh)
						$coeff=$coeffl;
					else
						$coeff=$coeffh;
					$hauteur=round($hauteur/$coeff);
					$largeur=round($largeur/$coeff);
	
					$txtcorps = ereg_replace("%logo_st%",'<img src="cid:logo_st" width="'.$largeur.'" height="'.$hauteur.'" alt="logo_st">',$txtcorps);
				}
			}
		}		

		$mail->Subject = $sujet;
		$mail->Body = $txtcorps;

		$sent = $mail->Send();
	}
	else
		$sent=false;
	
	return $sent;
}

?>