Probleme avec la fonction mail

Eléphant du PHP | 258 Messages

12 juil. 2006, 17:19

Bonjour à tous voilà j'essaye d'envoyer un mail en php avec mise en page html.

Cet email récupère les informations d'une page style recapitulatif de commande ( nom, prenom,adresse etc et bien sur le panier).

Pour recupérer les adresses,nom etc et les envoyer pas de probleme mais pour le panier c'est tout autre chose car comme je vais vous montré dans mon code j'ai mis une boucle "do while" pour que quand il y a un article différent cela créer une autre ligne.

Mais le soucis c'est que l'on ne peut pas mettre du php dans la fonction mail

Voici le code qui me pose soucis :

 <?php do { ?>
  <tr>
    <td><div align="center">'.$nomArti.'</div></td>
    <td><div align="center">'.$prixUni.'</div></td>
    <td><div align="center">'.$Qu.'</div></td>
    <td><div align="center">'.$prixArti.'</div></td>
  </tr>
  <?php } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?>

Et aussi si il serait possible de mettre le code que je vais mettre dessous dans une fonction et dans un fichier à part et si oui comment on fait.

Voila mon code de la fonction mail :

<?php

$adresse = "mon mail";
	$sujet = "Confirmation de votre commande\n\n";



$texte=' <html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>&nbsp; '.$etatC.'  '.$nom.' '.$prenom.',</p>
<p>Merci d\'avoir pass&eacute; commande chez <strong>ATLANTIC BARS<strong>
<strong><br>Conservez ce mail car il tient lieu de facture. </strong></p>
<table width="429" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="225">Commande n&deg; '.$clientID.'</td>
    <td width="204">du '.$date.' </td>
  </tr>
</table>
<br>
<table width="559" border="0" cellspacing="0">
  <tr>
    <td width="166"><div align="center">D&eacute;signation</div></td>
    <td width="138"><div align="center">Prix Unitaire </div></td>
    <td width="112"><div align="center">Quantit&eacute;</div></td>
    <td width="135"><div align="center">Prix Article </div></td>
  </tr>
  <tr>
    <td>---------------------------</td>
    <td>-----------------------</td>
    <td>------------------</td>
    <td>----------------------</td>
  </tr>
  <?php do { ?>
  <tr>
    <td><div align="center">'.$nomArti.'</div></td>
    <td><div align="center">'.$prixUni.'</div></td>
    <td><div align="center">'.$Qu.'</div></td>
    <td><div align="center">'.$prixArti.'</div></td>
  </tr>
  <?php } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?>
</table>
---------------------------------------------------------------------------------------------------------------<br>
<table width="581" border="0" cellspacing="0">
  <tr>
    <td width="202">&nbsp;</td>
    <td width="375"><table width="284" border="0" align="right" cellpadding="0" cellspacing="0">
      <tr>
        <td width="118"><div align="center">Total T.T.C : </div></td>
        <td width="166"><div align="center">'.$totalc.'  </div></td>
      </tr>
    </table></td>
  </tr>
</table>
<p><strong>Adresse de livraison :</strong></p>
<p><strong>Nom :</strong> '.$nom.'<br>
  <strong>Prenom :  </strong>'.$prenom.'<br>
 <strong> Adresse : </strong>'.$adress.'<br>
  <strong>Code postal : </strong>'.$cp.' <br>
  <strong>Ville : </strong>'.$ville.'<br>
  <strong>T&eacute;l&eacute;phone : </strong>'.$tel.'<br>
  <strong>Mail :</strong> '.$mail.'</p>
<p><strong>Adresse de facturation :</strong><br> 
  <br>
  <strong>Nom :</strong> '.$nomf.'<br>
  <strong>Prenom : </strong>'.$prenomf.'<br>
  <strong>Adresse : </strong>'.$adressef.'<br>
  <strong>Code postal :</strong> '.$cpf.' <br>
  <strong>Ville : </strong>'.$villef.'<br>
  <strong>T&eacute;l&eacute;phone : </strong>'.$telf.'<br>
  <strong>Mail :</strong> '.$mailf.' <br>
<p>Merci de votre visite, en esp&eacute;rant vous revoir bient&ocirc;t.</p>
</body>
</html>
'; 
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: '.mailf.'\n";

mail ($adresse, $sujet, $texte,$headers);
?>
Merci à tous j'attend votre aide avec impatiance.
Modifié en dernier par 2501gigi le 19 juil. 2006, 17:07, modifié 1 fois.

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 10684 Messages

12 juil. 2006, 17:56

Mais le soucis c'est que l'on ne peut pas mettre du php dans la fonction mail
Du php si, des balises html en direct non :)
Il faut que tu concatène le html que tu veux mettre dans ton mail dans une variable php. C'est cette variable que tu dois ensuite passer à la fonction mail()
// au lieu de
<?php do {?>
  <td>...</td>
  <td>...</td>
<?php } while (...); ?> 

// il te faut
<?php 
$str = "";
do {
  $str.= '<td>...</td>';
  $str.= '<td>...</td>';
} while (...); 
?> 
et tu passes $str dans ton message
Et aussi si il serait possible de mettre le code que je vais mettre dessous dans une fonction et dans un fichier à part et si oui comment on fait.
C'est possible :)
Tu colles le code dans ton autre fichier et tu utilises les fonctions include(), include_once(), require() ... pour appeller ce fichier dans les pages où tu en as besoin :)
Et pour la transformer en fonction, tu ajoutes function nomDeLaFonction(arguments, de, la, fonction) { ... } autour et le tour est joué ;)

Eléphant du PHP | 258 Messages

12 juil. 2006, 18:49

merci autrement dit comme ca si j'ai bien compris :
<?php do { 
    $str.='<tr>';
    $str.='<td><div align="center">'.$nomArti.'</div></td>';
    $str.='<td><div align="center">'.$prixUni.'</div></td>';
    $str.='<td><div align="center">'.$Qu.'</div></td>';
    $str.='<td><div align="center">'.$prixArti.'</div></td>';
    $str.='</tr>';
   } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?>
Avec sa j'obtient une erreur unexpected '>' ligne 123
Ceci est la ligne 123 $str.='<tr>';


Est ca je le mais direct dans le message ou je fais
$Var='<?php do { 
   $str.='<tr>';
    $str.='<td><div align="center">'.$nomArti.'</div></td>';
    $str.='<td><div align="center">'.$prixUni.'</div></td>';
    $str.='<td><div align="center">'.$Qu.'</div></td>';
    $str.='<td><div align="center">'.$prixArti.'</div></td>';
    $str.='</tr>';
   } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?>';
et je met $Var dans le message.

Pour les arguments de la fonctions je met quoi ?

Eléphant du PHP | 258 Messages

13 juil. 2006, 10:54

Bon je suis toujour avec ma fonction mail

Et ca ca ne marche pas

<?php do { 
    $str.='<tr>'; 
    $str.='<td><div align="center">'.$nomArti.'</div></td>'; 
    $str.='<td><div align="center">'.$prixUni.'</div></td>'; 
    $str.='<td><div align="center">'.$Qu.'</div></td>'; 
    $str.='<td><div align="center">'.$prixArti.'</div></td>'; 
    $str.='</tr>'; 
   } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?> 
J'obtient une erreur unexpected '>' ligne 123
Ceci est la ligne 123 $str.='<tr>';

Tout le reste de mon mail passe bien mais je n'arrive pas à affiché tous mes articles avec la boucle do while

Mais article sont dans une base de donnée mais je peut aussi les récupérer d'une session est ce que ca ne serait pas plus facile?

Et j'ai toujour pas trouvé se que je met comme argument dans ma fonction evoimail

Avatar du membre
ViPHP
ViPHP | 3008 Messages

13 juil. 2006, 11:04

As-tu bien mis
$str = "";
avant ton do ?

Eléphant du PHP | 258 Messages

13 juil. 2006, 11:14

Je l'ai mis avant

voila mon code:

<?php
function EnvoiMailClient()
{
$nomf=$row_Recordset2['nomfac'];
$prenomf=$row_Recordset2['prenomfac'];
$adressef=$row_Recordset2['adressefac'];
$villef=$row_Recordset2['villefac'];
$cpf=$row_Recordset2['cpfac'];
$telf=$row_Recordset2['telfac'];
$mailf=$row_Recordset2['mailfac'];

$nom=$row_Recordset1['nom'];
$prenom=$row_Recordset1['prenom'];
$adress=$row_Recordset1['adresse'];
$ville=$row_Recordset1['ville'];
$cp=$row_Recordset1['cp'];
$tel=$row_Recordset1['tel'];
$mail=$row_Recordset1['mail'];

$clientID=$row_Recordset3['ID'];
$date=$row_Recordset3['date'];
$total=$row_Recordset3['totalcom'];

$nomArti=$row_Recordset4['nomArti'];
$prixUni= $row_Recordset4['prixUni'];
$Qu=$row_Recordset4['Qu'];
$prixArti=$row_Recordset4['prixArti']; 

$str="";



$adresse = "mon mail.fr";
	$sujet = "Confirmation de votre commande\n\n";



$texte=' <html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>&nbsp; '.$etatC.'  '.$nom.' '.$prenom.',</p>
<p>Merci d\'avoir pass&eacute; commande chez <strong>ATLANTIC BARS<strong>
<strong><br>Conservez ce mail car il tient lieu de facture. </strong></p>
<table width="429" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="225">Commande n&deg; '.$clientID.'</td>
    <td width="204">du '.$date.' </td>
  </tr>
</table>
<br>
<table width="559" border="0" cellspacing="0">
  <tr>
    <td width="166"><div align="center">D&eacute;signation</div></td>
    <td width="138"><div align="center">Prix Unitaire </div></td>
    <td width="112"><div align="center">Quantit&eacute;</div></td>
    <td width="135"><div align="center">Prix Article </div></td>
  </tr>
  <tr>
    <td>---------------------------</td>
    <td>-----------------------</td>
    <td>------------------</td>
    <td>----------------------</td>
  </tr>
 <?php do { 
    $str.='<tr>'; 
    $str.='<td><div align="center">'.$nomArti.'</div></td>'; 
    $str.='<td><div align="center">'.$prixUni.'</div></td>'; 
    $str.='<td><div align="center">'.$Qu.'</div></td>'; 
    $str.='<td><div align="center">'.$prixArti.'</div></td>'; 
    $str.='</tr>'; 
   } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?> </table>
---------------------------------------------------------------------------------------------------------------<br>
<table width="581" border="0" cellspacing="0">
  <tr>
    <td width="202">&nbsp;</td>
    <td width="375"><table width="284" border="0" align="right" cellpadding="0" cellspacing="0">
      <tr>
        <td width="118"><div align="center">Total T.T.C : </div></td>
        <td width="166"><div align="center">'.$totalc.'  </div></td>
      </tr>
    </table></td>
  </tr>
</table>
<p><strong>Adresse de livraison :</strong></p>
<p><strong>Nom :</strong> '.$nom.'<br>
  <strong>Prenom :  </strong>'.$prenom.'<br>
 <strong> Adresse : </strong>'.$adress.'<br>
  <strong>Code postal : </strong>'.$cp.' <br>
  <strong>Ville : </strong>'.$ville.'<br>
  <strong>T&eacute;l&eacute;phone : </strong>'.$tel.'<br>
  <strong>Mail :</strong> '.$mail.'</p>
<p><strong>Adresse de facturation :</strong><br> 
  <br>
  <strong>Nom :</strong> '.$nomf.'<br>
  <strong>Prenom : </strong>'.$prenomf.'<br>
  <strong>Adresse : </strong>'.$adressef.'<br>
  <strong>Code postal :</strong> '.$cpf.' <br>
  <strong>Ville : </strong>'.$villef.'<br>
  <strong>T&eacute;l&eacute;phone : </strong>'.$telf.'<br>
  <strong>Mail :</strong> '.$mailf.' <br>
<p>Merci de votre visite, en esp&eacute;rant vous revoir bient&ocirc;t.</p>
</body>
</html>
'; 
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: '.mailf.'\n";

mail ($adresse, $sujet, $texte,$headers);

}
?>

Avatar du membre
ViPHP
ViPHP | 3008 Messages

13 juil. 2006, 11:28

Heu...tu mets une balise php dans une balise php ?
<?php code <?php code ?> code ?>
Je ne suis pas sûr que ça fonctionne moi...

Eléphant du PHP | 258 Messages

13 juil. 2006, 11:35

C'est justement ce que je demande depuis le debut

Car moi non plus je suis pas sur mais c'est la methode que l'on ma donnée plus haut.

Car j'ai besoin de ca, car si tu veut cette boucle marche tres bien quand il ne s'agit pas de l'envoyer par mail, en faite elle creer une ligne à chaque article se trouvant dans la base de donnée pour le meme client.

Voila je ne sais pas comment faire pour faire aparaitre tous ses articles dans mon mail

Avatar du membre
ViPHP
ViPHP | 3008 Messages

13 juil. 2006, 11:48

As-tu essayé de fermer la balise php juste avant ton do ? De le ré-ouvrir ensuite et de faire une concaténation de $texte ?

Eléphant du PHP | 258 Messages

13 juil. 2006, 11:56

Non je vais essayer mais je suis pas sur que sa marche;

Eléphant du PHP | 258 Messages

13 juil. 2006, 12:06

Ca ne marche pas du tout, du tout.

Aucun article s'affiche dans mon mail .

Et sur la page ou j'ai mis la fonction sa affiche mes variables.

Avatar du membre
ViPHP
ViPHP | 3008 Messages

13 juil. 2006, 12:08

Montres nous ton code après rectification

Eléphant du PHP | 258 Messages

13 juil. 2006, 12:59

voila mon code apres rectification
<?php

$nomf=$row_Recordset2['nomfac'];
$prenomf=$row_Recordset2['prenomfac'];
$adressef=$row_Recordset2['adressefac'];
$villef=$row_Recordset2['villefac'];
$cpf=$row_Recordset2['cpfac'];
$telf=$row_Recordset2['telfac'];
$mailf=$row_Recordset2['mailfac'];

$nom=$row_Recordset1['nom'];
$prenom=$row_Recordset1['prenom'];
$adress=$row_Recordset1['adresse'];
$ville=$row_Recordset1['ville'];
$cp=$row_Recordset1['cp'];
$tel=$row_Recordset1['tel'];
$mail=$row_Recordset1['mail'];

$clientID=$row_Recordset3['ID'];
$date=$row_Recordset3['date'];
$total=$row_Recordset3['totalcom'];

$nomArti=$row_Recordset4['nomArti'];
$prixUni= $row_Recordset4['prixUni'];
$Qu=$row_Recordset4['Qu'];
$prixArti=$row_Recordset4['prixArti']; 

$str="";



$adresse = "monmail.fr";
	$sujet = "Confirmation de votre commande\n\n";



$texte=' <html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>&nbsp; '.$etatC.'  '.$nom.' '.$prenom.',</p>
<p>Merci d\'avoir pass&eacute; commande chez <strong>ATLANTIC BARS<strong>
<strong><br>Conservez ce mail car il tient lieu de facture. </strong></p>
<table width="429" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="225">Commande n&deg; '.$clientID.'</td>
    <td width="204">du '.$date.' </td>
  </tr>
</table>
<br>
<table width="559" border="0" cellspacing="0">
  <tr>
    <td width="166"><div align="center">D&eacute;signation</div></td>
    <td width="138"><div align="center">Prix Unitaire </div></td>
    <td width="112"><div align="center">Quantit&eacute;</div></td>
    <td width="135"><div align="center">Prix Article </div></td>
  </tr>
  <tr>
    <td>---------------------------</td>
    <td>-----------------------</td>
    <td>------------------</td>
    <td>----------------------</td>
  </tr>;' ?>
 <?php do { ?> 
  <tr> 
    <td><div align="center">'.$nomArti.'</div></td> 
    <td><div align="center">'.$prixUni.'</div></td> 
    <td><div align="center">'.$Qu.'</div></td> 
    <td><div align="center">'.$prixArti.'</div></td> 
  </tr> 
  <?php } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?> 
<?php $texte.='</table>
---------------------------------------------------------------------------------------------------------------<br>
<table width="581" border="0" cellspacing="0">
  <tr>
    <td width="202">&nbsp;</td>
    <td width="375"><table width="284" border="0" align="right" cellpadding="0" cellspacing="0">
      <tr>
        <td width="118"><div align="center">Total T.T.C : </div></td>
        <td width="166"><div align="center">'.$totalc.'  </div></td>
      </tr>
    </table></td>
  </tr>
</table>
<p><strong>Adresse de livraison :</strong></p>
<p><strong>Nom :</strong> '.$nom.'<br>
  <strong>Prenom :  </strong>'.$prenom.'<br>
 <strong> Adresse : </strong>'.$adress.'<br>
  <strong>Code postal : </strong>'.$cp.' <br>
  <strong>Ville : </strong>'.$ville.'<br>
  <strong>T&eacute;l&eacute;phone : </strong>'.$tel.'<br>
  <strong>Mail :</strong> '.$mail.'</p>
<p><strong>Adresse de facturation :</strong><br> 
  <br>
  <strong>Nom :</strong> '.$nomf.'<br>
  <strong>Prenom : </strong>'.$prenomf.'<br>
  <strong>Adresse : </strong>'.$adressef.'<br>
  <strong>Code postal :</strong> '.$cpf.' <br>
  <strong>Ville : </strong>'.$villef.'<br>
  <strong>T&eacute;l&eacute;phone : </strong>'.$telf.'<br>
  <strong>Mail :</strong> '.$mailf.' <br>
<p>Merci de votre visite, en esp&eacute;rant vous revoir bient&ocirc;t.</p>
</body>
</html>
'; 
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: '.mailf.'\n";

mail ($adresse, $sujet, $texte,$headers);
?>

Avatar du membre
ViPHP
ViPHP | 3008 Messages

13 juil. 2006, 13:49

Tu as sorti la boucle mais tu as laissé des variables en dehors du php, normal que ça ne fonctionne pas.

A tester, je ne sais pas si ça marche :
<?php 
function EnvoiMailClient()
{
$nomf=$row_Recordset2['nomfac'];
$prenomf=$row_Recordset2['prenomfac'];
$adressef=$row_Recordset2['adressefac'];
$villef=$row_Recordset2['villefac'];
$cpf=$row_Recordset2['cpfac'];
$telf=$row_Recordset2['telfac'];
$mailf=$row_Recordset2['mailfac'];

$nom=$row_Recordset1['nom'];
$prenom=$row_Recordset1['prenom'];
$adress=$row_Recordset1['adresse'];
$ville=$row_Recordset1['ville'];
$cp=$row_Recordset1['cp'];
$tel=$row_Recordset1['tel'];
$mail=$row_Recordset1['mail'];

$clientID=$row_Recordset3['ID'];
$date=$row_Recordset3['date'];
$total=$row_Recordset3['totalcom'];

$nomArti=$row_Recordset4['nomArti'];
$prixUni= $row_Recordset4['prixUni'];
$Qu=$row_Recordset4['Qu'];
$prixArti=$row_Recordset4['prixArti'];

$adresse = "mon mail.fr";
$sujet = "Confirmation de votre commande\n\n";


$texte='<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>&nbsp; '.$etatC.'  '.$nom.' '.$prenom.',</p>
<p>Merci d\'avoir pass&eacute; commande chez <strong>ATLANTIC BARS<strong>
<strong><br>Conservez ce mail car il tient lieu de facture. </strong></p>
<table width="429" border="0" cellspacing="0" cellpadding="0">
<tr> 
	<td width="225">Commande n&deg; '.$clientID.'</td>
	<td width="204">du '.$date.' </td>
</tr>
</table>
<br>
<table width="559" border="0" cellspacing="0">
<tr> 
	<td width="166"><div align="center">D&eacute;signation</div></td>
	<td width="138"><div align="center">Prix Unitaire </div></td>
	<td width="112"><div align="center">Quantit&eacute;</div></td>
	<td width="135"><div align="center">Prix Article </div></td>
</tr> 
<tr> 
    <td>---------------------------</td>
    <td>-----------------------</td>
    <td>------------------</td>
    <td>----------------------</td>
</tr>';
?>
<?php
$str="";
do {
    $str.='<tr>';
    $str.='<td><div align="center">'.$nomArti.'</div></td>';
    $str.='<td><div align="center">'.$prixUni.'</div></td>';
    $str.='<td><div align="center">'.$Qu.'</div></td>';
    $str.='<td><div align="center">'.$prixArti.'</div></td>';
    $str.='</tr>'; 
   } while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?>
<?php
$texte. ='
</table> 
---------------------------------------------------------------------------------------------------------------<br>
<table width="581" border="0" cellspacing="0">
<tr>
	<td width="202">&nbsp;</td>
	<td width="375">
	
	<table width="284" border="0" align="right" cellpadding="0" cellspacing="0">
	<tr>
		<td width="118" align="center">Total T.T.C :</td>
		<td width="166" align="center">'.$totalc.'</td>
	</tr>
	</table>
	
	</td>
</tr>
</table>
<p><strong>Adresse de livraison :</strong></p>
<p><strong>Nom :</strong> '.$nom.'<br>
<strong>Prenom :  </strong>'.$prenom.'<br>
<strong> Adresse : </strong>'.$adress.'<br>
<strong>Code postal : </strong>'.$cp.'<br>
<strong>Ville : </strong>'.$ville.'<br>
<strong>T&eacute;l&eacute;phone : </strong>'.$tel.'<br>
<strong>Mail :</strong> '.$mail.'</p>
<p><strong>Adresse de facturation :</strong><br>
<br>
<strong>Nom :</strong> '.$nomf.'<br>
<strong>Prenom : </strong>'.$prenomf.'<br>
<strong>Adresse : </strong>'.$adressef.'<br>
<strong>Code postal :</strong> '.$cpf.'<br>
<strong>Ville : </strong>'.$villef.'<br>
<strong>T&eacute;l&eacute;phone : </strong>'.$telf.'<br>
<strong>Mail :</strong> '.$mailf.'<br>
<p>Merci de votre visite, en esp&eacute;rant vous revoir bient&ocirc;t.</p>
</body>
</html>';
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: '.mailf.'\n";

mail ($adresse, $sujet, $texte,$headers);
}
?>

Eléphant du PHP | 258 Messages

13 juil. 2006, 16:05

bon j'ai essayer il me met l'erreur suivante :

Parse error: parse error, unexpected '=' in c:\program files\easyphp1-7\www\boutmoran2\phpcommande\catalogueremerciement.php on line 130

} while ($row_Recordset4 = mysql_fetch_assoc($Recordset4)); ?>
<?php
$texte. =' c'est la ligne 130
</table>