Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 22/01/2012, 09h48   #1
Invité régulier
 
Inscription : juin 2010
Messages : 61
Détails du profil
Informations personnelles :
Âge : 23

Informations forums :
Inscription : juin 2010
Messages : 61
Points : 7
Points : 7
Envoyer un message via MSN à andaman
Par défaut Plusieurs pièces jointe avec classe PHPMAILER

Bonjour,

Ayant la contrainte de ne pas pouvoir exploiter la fonction mail de PHP, je passe par la classe de PHP MAILER pour envoyer des mails.

Tout fonctionne sauf que quand je veux joindre plusieurs fichiers, ils s'enregistrent bien dans le dossier mais seulement le premier arrive correctement par mail. Pourriez-vous m'aider?

Merci d'avance.

CODE PHP:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
if(isset($_POST['send_mail'])){  
    $error = '';
    $mail = new PHPmailer();
    $mail->IsSMTP();
    $mail->IsHTML(true);
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Port = TXT_PORT; 
    $mail->Username = TXT_EMAIL_ADDRESS;
    $mail->Password = TXT_PASSWORD; 
    $mail->From = TXT_EMAIL_ADDRESS;//mon adresse mail
    $mail->FromName = 'Ads Cambodia';
    $mail->AddAddress($_POST['email_address']);//Destinataire
    $mail->AddReplyTo(TXT_EMAIL_ADDRESS);//mon adresse mail
    $mail->CharSet = 'utf-8';
    $mail->ConfirmReadingTo = TXT_EMAIL_ADDRESS;
 
    foreach($_FILES as $file){
        $directory = "./upload_mail/";
        $target_path = $directory .basename($file['name']);
        if(move_uploaded_file($file['tmp_name'], $target_path)){
            echo "the file ".basename($file['name'])." has been uploaded";
        }else{
            echo "there was an error";
        }
        $mail->AddAttachment($target_path);          
    }    
    $mail->Subject = $_POST['subject'];
    $mail->Body  = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    $mail->Body .= '<html xmlns="http://www.w3.org/1999/xhtml">';
    $mail->Body .= '<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Document sans nom</title></head>';
    $mail->Body .= '<body>';
    $mail->Body .= $_POST['message'];
    $mail->Body .= '<p><hr />H/P : +855 (0) 97 981 02 74<br />Website : www.ads-cambodia.com<br />Please consider the environment before printing this e-mail</p>';
    $mail->Body .= '</body></html>';
 
    if(!$mail->Send()){ //Teste si le return code est ok.
      echo $mail->ErrorInfo; //Affiche le message d'erreur (ATTENTION:voir section 7)
    }
    else{	  
      echo 'Mail Sent !';
 
    }
    $mail->SmtpClose();
    unset($mail);    
}
CODE HTML:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 
<script>
function create_champ(i) {
    var i2 = i + 1;
    document.getElementById('leschamps_'+i).innerHTML = '<input type="file" name="file'+i+'"></span>';
    document.getElementById('leschamps_'+i).innerHTML += (i <= 5) ? '<br /><span id="leschamps_'+i2+'"><a href="javascript:create_champ('+i2+')">Add attachments</a></span>' : '';
}
</script>
<form name="send_mail" action="?section=sendmail" enctype="multipart/form-data" method="post">
    <table width="500" border="0">
      <tr>
        <td colspan="2"><h1>Send an email at : <?php echo $row_mail['u_first_name']." ".$row_mail['u_last_name'];?></h1></td>        
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Subject :</td>
        <td>
            <input type="hidden" name="email_address" value="<?php echo $row_mail['u_email'];?>" />
            <input style="width:220px" type="text" name="subject" />
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td valign="top">Message :</td>
        <td><textarea name="message" cols="45" rows="20">Dear <?php echo $row_mail['u_first_name']." ".$row_mail['u_last_name'];?>,</textarea></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Attachment :</td>
        <td><input type="file" name="file" /></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="hidden" name="max_file_size" value="<?php echo $max_file_size;?>" /></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><span id="leschamps_4"><a href="javascript:create_champ(4)">Add attachments</a></span><br /></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" align="center"><input type="submit" name="send_mail" value="Send" /></td>
      </tr>
    </table>
</form>
andaman est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/01/2012, 10h59   #2
Invité régulier
 
Inscription : juin 2010
Messages : 61
Détails du profil
Informations personnelles :
Âge : 23

Informations forums :
Inscription : juin 2010
Messages : 61
Points : 7
Points : 7
Envoyer un message via MSN à andaman
Je me réponds ;-) car j'ai trouvé.

Ma boucle est correct. Il faut ouvrir le fichier "class.phpmailer.php" et chercher la fonction EncodeFile et la modifier. Depuis la version 5.3, get_magic_quotes est déprécié.

Je cloture le sujet.

Maintenant je sais envoyer mes mail avec PJ à volonté
andaman est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/01/2012, 11h07   #3
Membre éprouvé
 
Avatar de Gecko
 
Homme Antoine B
Développeur Web
Inscription : février 2011
Messages : 106
Détails du profil
Informations personnelles :
Nom : Homme Antoine B
Âge : 27
Localisation : France, Nord (Nord Pas de Calais)

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : février 2011
Messages : 106
Points : 410
Points : 410
Envoyer un message via MSN à Gecko Envoyer un message via Skype™ à Gecko
Salut

1ère chose, tu n'es pas dans le bon forum, ici c'est pour PHP + MySQL

Pour cerner plus rapidement ton problème je te propose de faire deux choses :

Dans un premier temps il faudrait que tu check l'existence du fichier qui viens d'être upload:
Code php :
1
2
3
 
if(file_exists($target_path)) { $mail->AddAttachment($target_path); }
else { echo $target_path.' inexistant'; }

le second point, qui est clairement indiqué dans la doc, c'est que tu ne catch pas les exceptions, donc forcément si une exception est lancée et que tu ne l'intercepte pas tu la verra pas

Code php :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
if(isset($_POST['send_mail'])){  
    $error = '';
    $mail = new PHPmailer();
    try {
	$mail->IsSMTP();
	$mail->IsHTML(true);
	$mail->Host = 'smtp.gmail.com';
	$mail->SMTPAuth = true;
	$mail->SMTPSecure = 'ssl';
	$mail->Port = TXT_PORT; 
	$mail->Username = TXT_EMAIL_ADDRESS;
	$mail->Password = TXT_PASSWORD; 
	$mail->From = TXT_EMAIL_ADDRESS;//mon adresse mail
	$mail->FromName = 'Ads Cambodia';
	$mail->AddAddress($_POST['email_address']);//Destinataire
	$mail->AddReplyTo(TXT_EMAIL_ADDRESS);//mon adresse mail
	$mail->CharSet = 'utf-8';
	$mail->ConfirmReadingTo = TXT_EMAIL_ADDRESS;
	foreach($_FILES as $file){
	    $directory = "./upload_mail/";
	    $target_path = $directory .basename($file['name']);
	    if(move_uploaded_file($file['tmp_name'], $target_path)){
		echo "the file ".basename($file['name'])." has been uploaded";
	    }else{
		echo "there was an error";
	    }
	    if(file_exists($target_path)) { $mail->AddAttachment($target_path); }
	    else { echo $target_path.' inexistant'; }
	}    
	$mail->Subject = $_POST['subject'];
	$mail->Body  = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
	$mail->Body .= '<html xmlns="http://www.w3.org/1999/xhtml">';
	$mail->Body .= '<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Document sans nom</title></head>';
	$mail->Body .= '<body>';
	$mail->Body .= $_POST['message'];
	$mail->Body .= '<p><hr />H/P : +855 (0) 97 981 02 74<br />Website : www.ads-cambodia.com<br />Please consider the environment before printing this e-mail</p>';
	$mail->Body .= '</body></html>';
	$mail->Send();
    } catch (phpmailerException $e) {
	echo $e->getMessage(); // Exceptions de PHPMailer
    } catch (Exception $e) {
	echo $e->getMessage(); // Toutes les autres exceptions
    }
    $mail->SmtpClose();
    unset($mail); 
}

A voir si addAttachment ne retourne pas une exception si le fichier indiqué n'est pas valide.

N'hésite pas à revenir avec un éventuel message d'erreur ^^
__________________
Si ce message vous a aidé, pensez à voter pour lui!
Développe vos scripts E-Commerce et autres en PHP
N'hésitez pas à me contacter par MP, E-Mail et MSN
Gecko est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 16h34.


 
 
 
 
Partenaires

Hébergement Web