erreur avec fonction qui se répète dans une boucle foreach

Eléphant du PHP | 331 Messages

22 oct. 2007, 17:33

Bonjour,

J'ai une fonction qui me sert à envoyer un mail avec une pièce jointe.
Mon mail doit être envoyé à de nombreux contacts et pour cela j'utilise une boucle foreach().
A l'exécution, cela fonctionne trés bien pour le premier passage dans la boucle, mais des le second contact, j'ai une erreur qui me dit qu'une fonction ne peut pas être redéclarée.
Du coup, je suis bien embété pour faire ce que je cherche à réaliser.
Je vous montre mon code. Si vous pouviez m'aider ... Merci d'avance !
	//On récupère les contacts mails du client 
	$query_ListeContactsEmail = "SELECT EmailContact FROM Contacts WHERE IdClient = '$IdCli' AND EmailContact <> ''";
	$ListeContactsEmail = mysql_query($query_ListeContactsEmail, $connection) or die(mysql_error());
	$totalRows_ListeContactsEmail = mysql_num_rows($ListeContactsEmail);
	
	while ($row_ListeContactsEmail = mysql_fetch_assoc($ListeContactsEmail))
	{ 
	  $courriels[] = $row_ListeContactsEmail['EmailContact'];
	}
	$NbEmails = count($courriels);

	//s'il y a au moins un contact email, alors on fait la suite
	if ($NbEmails >= 1)
	{
		//on envoie les emails
		
		foreach($courriels as $email_destinataire) 
		{
			$to = $email_destinataire;
			$subject = "Sujet";
			$message = $TexteP;
			$pj = "../../push/images/moyennes/".$IdCli.".jpg";
	
			function envoyermailpj($to, $subject, $message, $pj) 
			{ 
			global $email;
			global $Nom;
			global $Prenom;	
	
			 $limite = "_parties_".md5(uniqid (rand())); 
	
			 $mail_mime  = "Date: ".date("l j F Y, G:i")."\n"; 
			 $mail_mime .= "MIME-Version: 1.0\n"; 
			 $mail_mime .= "Content-Type: multipart/mixed;\n"; 
			 $mail_mime .= " boundary=\"----=$limite\"\n\n"; 
	
			 $texte  = "Ceci est un message au format MIME.\n"; 
			 $texte .= "------=$limite\n"; 
			 $texte .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
			 $texte .= "Content-Transfer-Encoding: 7bit\n\n"; 
			 $texte .= $message; 
			 $texte .= "\n\n"; 
	
			 $attachement = ""; 
			 // fichier 1 
			 $fichier = $pj;  
			 $fp = fopen($fichier, "rb");  
			 $pg = fread($fp,filesize($fichier));  
			 fclose($fp);  
			 $attachement .= "------=$limite\n"; 
			 $attachement .= "Content-Type: application/octet-stream; name=\"photo.jpg\"\n"; 
			 $attachement .= "Content-Transfer-Encoding: base64\n"; 
			 $attachement .= "Content-Disposition: attachment; filename=\"photo.jpg\"\n\n"; 
			 $attachement .= chunk_split(base64_encode($pg)); 
			 $attachement .= $pg; 
			 $attachement .= "\n\n\n------=$limite\n"; 
	
			 $from = "From:".$Prenom." ".$Nom;
			 $reply = "Reply-To: ".$email;
			 
			 $headers = $from . "\r\n" . 
			 $reply . "\r\n" . 
			 'X-Mailer: PHP/' . phpversion(); 
			 mail($to, $subject, $texte.$attachement, $headers.$mail_mime); 
		   } 
	   
			//envoi du mail au client
			envoyermailpj($to, $subject, $message, $pj);
		}

Eléphant du PHP | 443 Messages

22 oct. 2007, 19:29

Salut,

Sors ta fonction envoyermailpj(...) de la boucle, mets la par exemple tout au début de ton fichier php.
Si tu as le temps regarde également la doc sur les fonctions en php ça t'aidera.


@+
Tracker.