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

Langage PHP Discussion :

[Mail] Problème avec pièce jointe


Sujet :

Langage PHP

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Février 2007
    Messages
    219
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 219
    Points : 61
    Points
    61
    Par défaut [Mail] Problème avec pièce jointe
    Bonjour,
    J'ai un problème, je voudrais envoyer un mail avec une pièce jointe, ça j'y arrive. Mon problème est que le message arrive bien avec une pièce jointe, mais elle est vide et le nom est le chemin de la prièce jointe.
    Voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    require "mime_mail.class.php";
    $fichier_attache = fread(fopen("C:/Documents and Settings/Gibralta Coralie/Mes documents/Coralie/rapport.doc"));
    $mail = new mime_mail();
    $mail->to = "co250587@hotmail.com"; // Adresse email de reception
    $mail->subject = "Test"; // Sujet
    $mail->body = "Ceci est un test."; // Corps du message
    $mail->from = "co250587@hotmail.com"; // Adresse email de l'expediteur (optionnel)
    $mail->headers = "Date: "; // Entetes supplementaires (optionnel)
    $mail->attach("$fichier_attache"); // fichier attache (optionnel)
    // envoi du message
     
    $mail->send();

  2. #2
    Membre éclairé Avatar de Korko Fain
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    632
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 632
    Points : 718
    Points
    718
    Par défaut
    montre ta class mime_mail

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Février 2007
    Messages
    219
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 219
    Points : 61
    Points
    61
    Par défaut
    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    class mime_mail
     {
     
     var $parts;
     var $to;
     var $from;
     var $headers;
     var $subject;
     var $body;
     
     // constructeur
     function mime_mail()
     {
     
     $this->parts = array();
     $this->to = "";
     $this->from = "";
     $this->subject = "";
     $this->body = "";
     $this->headers = "";
     
     }
     
     // attache un fichier au message
     function attach($message,$name,$ctype = '')
     {
     
     // type de contenu non defini
     if(empty($ctype)){
     // on essaie de reconnaitre l'extension
     switch(strrchr(basename($name), ".")){
     case ".gz": $ctype = "application/x-gzip"; break;
     case ".tgz": $ctype = "application/x-gzip"; break;
     case ".zip": $ctype = "application/zip"; break;
     case ".pdf": $ctype = "application/pdf"; break;
     case ".png": $ctype = "image/png"; break;
     case ".gif": $ctype = "image/gif"; break;
     case ".jpg": $ctype = "image/jpeg"; break;
     case ".txt": $ctype = "text/plain"; break;
     case ".htm": $ctype = "text/html"; break;
     case ".html": $ctype = "text/html"; break;
     default: $ctype = "application/octet-stream"; break;
     }
     }
     
     $this->parts[] =
     array (
     "ctype" => $ctype,
     "message" => $message,
     //"encode" => $encode,
     "name" => $name
     );
     
     // fin de fonction
     }
     
     // fonction utilisee pour contruire le message MIME
     // utilisee depuis build_multipart()
     function build_message($part)
     {
     
     $message = $part[ "message"];
     $message = chunk_split(base64_encode($message));
     $encoding = "base64";
     
     return "Content-Type: ".$part[ "ctype"].
     ($part[ "name"]? "; name = \"".$part[ "name"]. "\"" : "").
     "\nContent-Transfer-Encoding: $encoding\n\n$message\n";
     
     }
     
     // compose le message MIME
     // utilisee depuis send()
     function build_multipart()
     {
     
     $boundary = "b".md5(uniqid(time()));
     $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
     
     for($i = sizeof($this->parts) - 1; $i >= 0; $i--)
     {
     $multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary";
     }
     
     return $multipart.= "--\n";
     
     }
     
     // envoie le message
     // derniere fonction a appeler
     function send()
     {
     
     $mime = "";
     // parametres optionnels
     if (!empty($this->from)) $mime .= "From: ".$this->from. "\n";
     if (!empty($this->headers)) $mime .= $this->headers. "\n";
     if (!empty($this->body)) $this->attach($this->body, "", "text/plain");
     // entete MIME
     $mime .= "MIME-Version: 1.0\n".$this->build_multipart();
     // envoi du message
     mail($this->to, $this->subject, "", $mime);
     
     }
     
    };

  4. #4
    Membre éclairé Avatar de Korko Fain
    Profil pro
    Étudiant
    Inscrit en
    Août 2005
    Messages
    632
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2005
    Messages : 632
    Points : 718
    Points
    718
    Par défaut
    Ta classe mail est un peu étrangement faite...

    Essaye avec celle-la :

    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    <?php
    class MIME_Mail
    {
     var $from;
     var $to;
     var $subject;
     var $message;
     var $attached;
     
     function MIME_Mail($from, $to=NULL, $subject=NULL, $message=NULL)
     {
      $this->from = $from;
      $this->to = $to;
      $this->subject = $subject;
      $this->message = $message;
      $this->attached = array();
     }
     
     function set_from($from)
     {
      $this->from = $from;
     }
     
     function set_to($to)
     {
      $this->to = $to;
     }
     
     function set_subject($subject)
     {
      $this->subject = $subject;
     }
     
     function set_message($message)
     {
      $this->message = $message;
     }
     
     function attach_file($path)
     {
      switch( strrchr( $path, '.') )
      {
       case '.gz':
        $ctype = "application/x-gzip";
        break;
     
       case ".tgz":
        $ctype = "application/x-gzip";
        break;
     
       case ".zip":
        $ctype = "application/zip";
        break;
     
       case ".pdf":
        $ctype = "application/pdf";
        break;
     
       case ".png":
        $ctype = "image/png";
        break;
     
       case ".gif":
        $ctype = "image/gif";
        break;
     
       case ".jpg":
        $ctype = "image/jpeg";
        break;
     
       case ".txt":
        $ctype = "text/plain";
        break;
     
       case ".htm":
        $ctype = "text/html";
        break;
     
       case ".html":
        $ctype = "text/html";
        break;
     
       default:
        $ctype = "application/octet-stream";
        break;
      }
     
      $handle = fopen($path, "r");
      $code = fread($f, sizeof($path));
      $code = chunk_split(base64_encode($code));
      fclose($handle);
     
      $this->attached[] = array('ctype' => $ctype, 'code' => $code, 'name' => basename($path));
     }
     
     function send_mail()
     {
      $boundary = md5(uniqid(time() ));
     
      $entete  = "From: {$this->from}\r\n";
      $entete .= "Reply-To: {$this->from}\r\n";
      $entete .= "MIME-Version: 1.0\r\n";
      $entete .= "X-Mailer: PHP\r\n";
      $entete .= "Content-Type: multipart/mixed;boundary=$boundary\r\n\r\n";
     
      $entete_message = "--$boundary\r\n";
      $entete_message .= "Content-type:text/plain;charset=us-ascii\r\n";
      $entete_message .= "Content-transfer-encoding:8bit\r\n\r\n";
      $entete_message .= "{$this->message}\r\n";
     
      $entete_attached = '';
     
      foreach( $this->attached as $attach )
      {
       $entete_attached .= "--$boundary\r\n";
       $entete_attached .= "Content-type:{$attach['ctype']};name={$attach['name']}\r\n";
       $entete_attached .= "Content-transfer-encoding:base64\r\n\r\n";
       $entete_attached .= "{$attach['code']}\r\n";
       $entete_attached .= "--$boundary--";
      }
      return mail($this->to, $this->subject,"",$entete . $entete_message . $entete_attached);
     }
    }
    ?>
    Usage :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $mail = new MIME_Mail($expediteur, $recepteur, $sujet, $message);
    $mail->attach_file($chemin_vers_fichier);
    $mail->send_mail();

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Février 2007
    Messages
    219
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 219
    Points : 61
    Points
    61
    Par défaut
    ok je vais essayer merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec pièce jointe lors de la réception du mail
    Par viejo dans le forum Général Java
    Réponses: 2
    Dernier message: 25/12/2011, 02h14
  2. [HTA] SMTP Mail Client avec pièce jointe
    Par hackoofr dans le forum Vos Contributions VBScript
    Réponses: 6
    Dernier message: 24/11/2011, 20h25
  3. [AC-2007] Problème avec pièce jointe dans mail
    Par Daniel-Gérald dans le forum VBA Access
    Réponses: 1
    Dernier message: 19/05/2011, 21h26
  4. Problème avec pièce jointe .jpg
    Par javelot69 dans le forum Outlook Express / Windows Mail
    Réponses: 14
    Dernier message: 25/10/2007, 13h55
  5. [Mail] problème mail html avec pièce jointe
    Par stars333 dans le forum Langage
    Réponses: 2
    Dernier message: 12/06/2007, 19h44

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