Précédent   Forum des professionnels en informatique > PHP > Langage > Fonctions
Fonctions Forum d'entraide sur les fonctions PHP. Avant de poster -> FAQ fonctions et Sources diverses
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 03/01/2011, 11h59   #1
Invité de passage
 
Inscription : avril 2010
Messages : 20
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 20
Points : 4
Points : 4
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 :
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.
Kevinou974 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 12h39   #2
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
Tu recois autre chose ? tu ne recois rien ? il se passe quoi ?
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 12h52   #3
Invité de passage
 
Inscription : avril 2010
Messages : 20
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 20
Points : 4
Points : 4
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.
Kevinou974 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 14h43   #4
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
Si tu ouvres ton fichier avec un editeur de texte, tu vois quoi ?
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 18h00   #5
Invité de passage
 
Inscription : avril 2010
Messages : 20
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 20
Points : 4
Points : 4
J'ai ouvert le fichier et tout ce qu'il y a c'est : 1 .
Kevinou974 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 18h06   #6
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
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 :
1
2
Content-Type: text/html; charset= UTF-8" 
Content-Transfer-Encoding: 7bit
ca part en cacahuete a cause du guillemet qui se ballade.
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 18h43   #7
Invité de passage
 
Inscription : avril 2010
Messages : 20
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 20
Points : 4
Points : 4
C'est corrigé mais je n'arrive toujours pas à recevoir mon image modifié en pièce jointe.
Kevinou974 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 18h51   #8
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
Repars d'un code propre :
http://a-pellegrini.developpez.com/tutoriels/php/mail/
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 22h48   #9
Invité de passage
 
Inscription : avril 2010
Messages : 20
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 20
Points : 4
Points : 4
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 :
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 :
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?
Kevinou974 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2011, 23h12   #10
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
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.
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/01/2011, 18h00   #11
Invité de passage
 
Inscription : avril 2010
Messages : 20
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 20
Points : 4
Points : 4
T'aurais une idée de comment je pourrai faire ce que je veux stp? Parce que je bloque un peu...
Kevinou974 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/01/2011, 20h02   #12
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
Ba commence par corriger ce que je te dis.
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/01/2011, 11h32   #13
Invité de passage
 
Inscription : avril 2010
Messages : 20
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 20
Points : 4
Points : 4
Bonjour,

donc j'ai enlevé le header et je n'arrive toujours pas à recevoir mon image modifiée en pièce jointe.
Kevinou974 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 14h59.


 
 
 
 
Partenaires

Hébergement Web