Précédent   Forum du club des développeurs et IT Pro > PHP > Langage > Débuter
Débuter Forum d'entraide pour débuter en PHP. Avant de poster -> Cours PHP, FAQ PHP, Outils PHP, etc.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 02/01/2013, 22h59   #1
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
Par défaut mail pièce attachée

Bonsoir,

voici un code de mail avec pièce attachée qui marche bien pour la plupart des fichiers mais par pour tous (avec l'exemple que j'ai, il envoit bien le mail, mais le fichier que je reçois n'est pas bon (c'est une image, ce qu'il reconnaît bien, mais il m'affiche une croix rouge si je veux visualiser celle-ci ; pourquoi ?)

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
$nom=$_POST['nom'];
$prenom=$_POST['prenom'];
$msg="test";
	//-----------------------------------------------
	//DECLARE LES VARIABLES
	//-----------------------------------------------
	$email_expediteur="---";
	$email_reply="---";
	$destinataire="---";
 
	$frontiere = '-----=' . md5(uniqid(mt_rand()));
 
	//-----------------------------------------------
	//HEADERS DU MAIL
	//-----------------------------------------------
 
	$headers = 'From: "'.$nom." ".$prenom.'" <'.$email_expediteur.'>'."\n";
	$headers.= 'Return-Path: <'.$email_reply.'>'."\n";
	$headers.= 'MIME-Version: 1.0'."\n";
	$headers.= 'Content-Type: multipart/mixed; boundary="'.$frontiere."\"\n\n";
 
	//-----------------------------------------------
	//MESSAGE
	//-----------------------------------------------
 
	$message ='This is a multi-part/mixed message in MIME format.'."\n";
	$message.='--'.$frontiere."\n";
	$message.='Content-Type: text/html; charset="ISO-8859-1"'."\n";
	$message.='Content-Transfer-Encoding: base64'."\n\n";
 
	$message.=chunk_split(base64_encode(nl2br($msg)))."\n";
 
	//-----------------------------------------------
	//PIECE JOINTE
	//-----------------------------------------------
 
	if (isset($_FILES['piece']['name'])) {
 
	$adr_piece=$_FILES['piece']['name'];
 
	$base1=basename($adr_piece);
 
	$mime=$_FILES['piece']['type'];
 
	$message.='--'.$frontiere."\n";
	$message.= 'Content-Type: '.$mime.'; name='.$base1."\n";
	$message.= 'Content-Transfer-Encoding: base64'."\n";
	$message.= 'Content-Disposition:attachement; filename='.$base1."\n\n";
	$message.= chunk_split(base64_encode(file_get_contents($adr_piece)))."\n";
 
	}
 
 
 echo "envoi mail; dest=".$destinataire."<br>";
	if(mail($destinataire,$sujet,$message,$headers)){
       echo "mail envoye";
}
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/01/2013, 23h11   #2
sabotage
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 16 510
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 16 510
Points : 21 349
Points : 21 349
Est-ce que le type mime est bien reconnu ?
sabotage est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/01/2013, 23h35   #3
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
Oui ; mon exemple est un fichier image et il le prend bien pour une image ; pour en avoir le coeur net, j'ai rajouté un echo :
Citation:
image/gif
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2013, 09h06   #4
selmouadin
Membre régulier
 
Homme Said ELMOUADIN
Développeur Web
Inscription : mai 2012
Messages : 57
Détails du profil
Informations personnelles :
Nom : Homme Said ELMOUADIN
Localisation : Maroc

Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : mai 2012
Messages : 57
Points : 91
Points : 91
Salut,

Je pense que tu dois d'abord placer ton fichier piece jointe qlq part avec move_uploaded_file
selmouadin est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2013, 12h36   #5
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
J'ai fait ça :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
	if (isset($_FILES['piece']['name'])) {
	$uploads_dir = '/';
    move_uploaded_file($_FILES['piece']['tmpname'], $uploads_dir/$_FILES['piece']['name']);
 
	$adr_piece=$uploads_dir/$_FILES['piece']['name'];
 
	$base1=basename($adr_piece);
 
	$mime=$_FILES['piece']['type'];
 
	$message.='--'.$frontiere."\n";
	$message.= 'Content-Type: '.$mime.'; name='.$base1."\n";
	$message.= 'Content-Transfer-Encoding: base64'."\n";
	$message.= 'Content-Disposition:attachement; filename='.$base1."\n\n";
	$message.= chunk_split(base64_encode(file_get_contents($adr_piece)))."\n";
 
	}
...
Mais toujours rien...
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2013, 16h45   #6
L0rD59
Membre à l'essai
 
Inscription : février 2005
Messages : 38
Détails du profil
Informations forums :
Inscription : février 2005
Messages : 38
Points : 24
Points : 24
Envoyer un message via AIM à L0rD59 Envoyer un message via MSN à L0rD59 Envoyer un message via Yahoo à L0rD59
Par défaut Zend_Mail

Tu peux utiliser le composant Zend_Mail pour une utilisation plus intuitive.

http://framework.zend.com/manual/1.12/fr/zend.mail.html
L0rD59 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/01/2013, 09h38   #7
selmouadin
Membre régulier
 
Homme Said ELMOUADIN
Développeur Web
Inscription : mai 2012
Messages : 57
Détails du profil
Informations personnelles :
Nom : Homme Said ELMOUADIN
Localisation : Maroc

Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : mai 2012
Messages : 57
Points : 91
Points : 91
Bonjour,

personnellement j'utilise la class phpmailer

http://code.google.com/a/apache-extras.org/p/phpmailer/

exemple d'utilisation :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
 
$mail->From     = "from@example.com";
$mail->AddAddress("myfriend@example.net");
$mail->AddAttachment($path_to_file);
 
$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
 
if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>
selmouadin est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/01/2013, 09h49   #8
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
Merci pour vos réponses ; quand j'aurai le temps, j'essaierai l'une de ces classes...
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/01/2013, 11h25   #9
beegees
Membre Expert
 
Avatar de beegees
 
Homme David Non communiqué
Développeur Web
Inscription : mars 2004
Messages : 3 348
Détails du profil
Informations personnelles :
Nom : Homme David Non communiqué
Âge : 38
Localisation : Belgique

Informations professionnelles :
Activité : Développeur Web
Secteur : Enseignement

Informations forums :
Inscription : mars 2004
Messages : 3 348
Points : 1 220
Points : 1 220
Salut,

Voici un exemple de code qui fonctionne chez moi :

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
 // email fields: to, from, subject, and so on
    $to = "TEST@lit.gmail.com";
    $subject ="My subject";
    $from = $_POST['netID']."@gmail.com <".$_POST['netID']."@gmail.com>";
     $message = '<div style="text-align:center";><img id="logo_TEST" width="102px" height="120px" src="http://TEST.gmail.com/dokuwiki/lib/exe/fetch.php?cache=&media=TEST-logo.png" /></div>';
    $message.=  "<p><b>".$_POST['matricule'] . " " . $_POST['prenom'] . " " . $_POST['nom'] . " (" . $_POST['netID']."</b>) a laissé ce message </p>";
    $message.= "\n\n";
 
     if(isset($_SESSION['$path']))
    {
	$fichier_joint = explode('\\',$_POST["piece_jointe"]);
	$message.= "<p>Pièce jointe : <a href='TEST.gmail.com/help/upload/".$_POST['piece_jointe']."'>TEST.gmail.com/help/upload/".$fichier_joint[sizeof($fichier_joint)-1]."</a></p>";
    }
    $headers = "From: $from";
 
    if(isset($_SESSION['$path']))
    {
	$path = $_SESSION['$path'];
    }
 
    // boundary
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
 
    // headers for attachment
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
 
    // multipart boundary
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    $message .= "--{$mime_boundary}\n";
     if(isset($_SESSION['$path']))
    {
	// preparing attachments
	$file = fopen($path,"rb");
	$data = fread($file,filesize($path));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$path\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$path\"\n" .     "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
    }
    // send
 
    $ok = @mail($to, $_POST['campus']." ".$_POST['faculte']." ".$_POST['local']." ".$_POST['type_probleme'], $message, $headers);
    unset($_SESSION['$path']);
    print_r($_SESSION);
    if ($ok)
    {
        echo "<p>mail sent to $to!</p>";
    }
    else
    {
        echo "<p>mail could not be sent!</p>";
    }
bee
__________________
beegees est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/01/2013, 17h10   #10
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
Coucou, je viens de tester la classe PHPMailer. Pour commencer, j'ai créé un fichier test où je mettais en dur le nom du fichier à transmettre, et ça marche !
Pour me rapprocher du besoin, j'ai créé un formulaire qui permet de sélectionner sur le disque un fichier à transmettre, mais ça ne marche plus (aucun mail n'arrive) ; où est mon erreur ?

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
<?php
require("class.phpmailer.php");
$mail = new PHPMailer("---"); /*j'ai modifié la classe où j'ai créé un constructeur qui permet de passer en paramètre le nom de l'émetteur (et ça, ça marche) */
 
$mail->From     = "---";
$mail->AddAddress("---");
if (isset($_FILES['piece']['tmpname'])){
  $piece=$_FILES['piece']['tmpname'];
$mail->AddAttachment($piece);}
 
$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
 
if((isset($_FILES['piece']['tmpname']))&&(!$mail->Send())) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title></title>
</head>
<body>
<style type="text/css">
 .label{ display: block; width:100px;float:left; } #pane { width:400px; margin-top: 50px; } 
.droite {width: 100px; } 
.boutons { margin-top: 30px; text-align: center; }
.erreur { text-align:right; color: red; padding-bottom: 15px; padding-right: 11px; font-size: 12px; } 
.champs { text-align:center; }</style>
 
<form action="" method="post" enctype="multipart/form-data">
 <table id="pane">
    <tr>
        <td>
          <label class="label">Pièce jointe</label>
        </td>
        <td class="champs">
          <input type="hidden" name="MAX_FILE_SIZE"
          value="4194304" />
          <input type="file" size="26" name="piece" />
        </td>
        <td></td>
      </tr>
     <tr>
        <td></td>
        <td class="champs">
          <div class="boutons">
            <input type="submit" name="valider" value="Envoyer" />
            <input type="submit" name="raz" value="Effacer" />
          </div>
        </td>
        <td></td>
      </tr>
    </table>
</form>
</body>
</html>
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 15/01/2013, 00h00   #11
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
Bonsoir, toujours avec PHPmailer, on est censé passer à AddAttachment le chemin du fichier à attacher, mais comment faire pour éviter d'uploader le fichier (donc y a pas d'adresse physique) ?
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/02/2013, 18h48   #12
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
Bonsoir, je réactive longtemps après ce sujet.
Certes, j'ai réussi avec PHPmailer à envoyer une pièce attachée par mail :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer("---"); /*j'ai modifié la classe où j'ai créé un constructeur qui permet de passer en paramètre le nom de l'émetteur (et ça, ça marche) */
 
$mail->From     = "---";
$mail->AddAddress("---");
 
  $piece="---.jpg";
$mail->AddAttachment($piece);
 
$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
 
 
if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>
mais le fichier joint est en dûr sur le serveur. S'il est juste sur le disque dûr, comment l'attacher sans l'uploader ?
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/02/2013, 19h51   #13
sabotage
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 16 510
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 16 510
Points : 21 349
Points : 21 349
Ce n'est pas possible : le serveur ne peux pas envoyer un fichier s'il ne l'a pas.
sabotage est actuellement connecté   Envoyer un message privé Réponse avec citation 10
Vieux 12/02/2013, 21h06   #14
laurentSc
Débutant
 
Homme Laurent
Webmaster débutant
Inscription : octobre 2006
Messages : 3 497
Détails du profil
Informations personnelles :
Nom : Homme Laurent
Âge : 49
Localisation : France, Isère (Rhône Alpes)

Informations professionnelles :
Activité : Webmaster débutant
Secteur : Industrie

Informations forums :
Inscription : octobre 2006
Messages : 3 497
Points : 1 695
Points : 1 695
Ca ne m'étonne pas vraiment, mais je préférais qu'un spécialiste me le confirme.
__________________
Il vaut mieux viser la perfection et la manquer que viser l'imperfection et l'atteindre. - Bertrand Russell
laurentSc est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 11h22.


 
 
 
 
Partenaires

Hébergement Web