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] Envoi d'une image créée par du code en pièce jointe


Sujet :

Langage PHP

  1. #1
    Membre à l'essai
    Inscrit en
    Avril 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 20
    Points : 10
    Points
    10
    Par défaut [Mail] Envoi d'une image créée par du code en pièce jointe
    Bonjour à tous,

    je souhaite créer un script qui permet de récupérer une image existante, de modifier l'image grâce à la librairie GD et d'envoyer l'image modifiée par mail en pièce jointe.

    Le problème c'est que je n'arrive pas à recevoir l'image modifiée en pièce jointe.


    Voilà le code en question:

    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
    <?php
     
    header ("Content-type: image/png");
    //je récupére l'image sur laquelle je veux mettre du texte
    		$nom_image = "images/mon_image.jpg";  
    //le texte que je veux mettre		
    		$texte = "test texte";  
     
    $image = imagecreatefromjpeg($nom_image);
    $noir = imagecolorallocate($image, 0, 0, 0);
    imagestring($image, 5, 150, 150,$texte, $noir);
     
    //envoi du mail
     
     
    //define the receiver of the email 
    $to = 'destinataire@blabla.com'; 
    //define the subject of the email 
    $subject = 'test envoi'; 
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 
    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From: expediteur@blabla.fr\r\nReply-To: expediteur@blabla.fr"; 
    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment = chunk_split(base64_encode(file_get_contents($nom_image))); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
     
    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
     
    Hello World!!! 
    This is simple text email message. 
     
    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset= UTF-8" 
    Content-Transfer-Encoding: 7bit
     
     
    <?php
     
     
     
    echo "Bonjour";
    ?>
     
    --PHP-alt-<?php echo $random_hash; ?>-- 
     
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: image/jpeg; name="mon_image.jpg"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  
     
    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 
     
    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail( $to, $subject, $message, $headers ); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
     
     
    ?>
    Merci d'avance pour la réponse.

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Tu recois autre chose ? tu ne recois rien ? il se passe quoi ?

  3. #3
    Membre à l'essai
    Inscrit en
    Avril 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 20
    Points : 10
    Points
    10
    Par défaut
    Je reçois mon mail avec la pièce jointe mon_image.jpg mais quand je veux l'ouvrir, on me dit que l'image est corrompu ou vide.

  4. #4
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Si tu ouvres ton fichier avec un editeur de texte, tu vois quoi ?

  5. #5
    Membre à l'essai
    Inscrit en
    Avril 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 20
    Points : 10
    Points
    10
    Par défaut
    J'ai ouvert le fichier et tout ce qu'il y a c'est : 1 .

  6. #6
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Il faudrait déjà corriger ton code.
    Comme tu peux le voir avec la colorisation du code dans ton message, a partir de la :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Content-Type: text/html; charset= UTF-8" 
    Content-Transfer-Encoding: 7bit
    ca part en cacahuete a cause du guillemet qui se ballade.

  7. #7
    Membre à l'essai
    Inscrit en
    Avril 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 20
    Points : 10
    Points
    10
    Par défaut
    C'est corrigé mais je n'arrive toujours pas à recevoir mon image modifié en pièce jointe.

  8. #8
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155

  9. #9
    Membre à l'essai
    Inscrit en
    Avril 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 20
    Points : 10
    Points
    10
    Par défaut
    Merci, mais mon code pour envoyer un mail en pièce jointe marche sans problème avec une image simple.
    C'est juste qu'au lieu d'utiliser l'image simple, je veux utiliser l'image modifiée par la librairie GD.

    Script pour envoyer un mail en pièce jointe avec une image simple qui marche:
    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
    <?php
     
    //envoi d'un mail en pièce jointe avec une image simple
     
    //define the receiver of the email 
    $to = 'destinataire@blabla.com'; 
    //define the subject of the email 
    $subject = 'test envoi'; 
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 
    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From: expediteur@blabla.fr\r\nReply-To: expediteur@blabla.fr"; 
    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment = chunk_split(base64_encode(file_get_contents('images/mon_image.jpg'))); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
     
    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
     
    Hello World!!! 
    This is simple text email message. 
     
    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset= "UTF-8" 
    Content-Transfer-Encoding: 7bit
     
     
    <?php
     
     
     
    echo "Bonjour";
    ?>
     
    --PHP-alt-<?php echo $random_hash; ?>-- 
     
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: image/jpeg; name="mon_image.jpg"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  
     
    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 
     
    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail( $to, $subject, $message, $headers ); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
     
     
    ?>
    Script pour modifier l'image simple:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    header ("Content-type: image/png");
    		$nom_image = "mon_image.jpg"; 
     $texte = "test texte"
    $image = imagecreatefromjpeg($nom_image);
    $noir = imagecolorallocate($image, 0, 0, 0);
    imagestring($image, 5, 150, 150,$texte, $noir);
    imagejpeg($image);
    Comment je pourrai mettre l'image modifiée en pièce jointe?

  10. #10
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Je ne vois pas pourquoi ca generait le fonctionnement, mais ton header() est inutile.
    Il manque egalement un ; mais je pense que tu as mal recopié ici.

  11. #11
    Membre à l'essai
    Inscrit en
    Avril 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 20
    Points : 10
    Points
    10
    Par défaut
    T'aurais une idée de comment je pourrai faire ce que je veux stp? Parce que je bloque un peu...

  12. #12
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Ba commence par corriger ce que je te dis.

  13. #13
    Membre à l'essai
    Inscrit en
    Avril 2010
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 20
    Points : 10
    Points
    10
    Par défaut
    Bonjour,

    donc j'ai enlevé le header et je n'arrive toujours pas à recevoir mon image modifiée en pièce jointe.

Discussions similaires

  1. Réponses: 4
    Dernier message: 11/01/2010, 14h46
  2. Envoi d'une image par e-mail depuis un formulaire
    Par caradhras dans le forum Langage
    Réponses: 2
    Dernier message: 25/09/2008, 19h13
  3. [GD] pb affichage d'une image créée par GD depuis ma base MySQL
    Par freddy92110 dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 30/12/2005, 15h50

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