IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

Plusieurs pièces jointe avec classe PHPMAILER


Sujet :

PHP & Base de données

  1. #1
    Membre habitué
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2010
    Messages
    221
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cambodge

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Juin 2010
    Messages : 221
    Points : 144
    Points
    144
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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>

  2. #2
    Membre habitué
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2010
    Messages
    221
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cambodge

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Juin 2010
    Messages : 221
    Points : 144
    Points
    144
    Par défaut
    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é

  3. #3
    Membre éprouvé
    Avatar de Gecko
    Homme Profil pro
    Développeur décisionnel
    Inscrit en
    Décembre 2008
    Messages
    499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur décisionnel

    Informations forums :
    Inscription : Décembre 2008
    Messages : 499
    Points : 1 277
    Points
    1 277
    Par défaut
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 ^^
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    if ($toBe || !$toBe) echo 'That is the question';

    Mes projets: DVP I/O

Discussions similaires

  1. Réponses: 4
    Dernier message: 02/02/2009, 22h40
  2. Envoi d'e-mails avec plusieurs pièces jointes
    Par xnomad dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 4
    Dernier message: 16/07/2007, 15h02
  3. Réponses: 1
    Dernier message: 22/01/2007, 09h08
  4. Réponses: 6
    Dernier message: 14/11/2006, 10h35
  5. Envoi d'un mail avec plusieurs pièces jointes
    Par Wilco dans le forum Composants VCL
    Réponses: 3
    Dernier message: 04/04/2006, 10h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo