Salut à tous !
Alors voilà, je me suis lancé dans un script d´envoi de mail avec possibilité de pièce jointe.
J´ai déjà le fichier formulaire xHTML, mais je ne trouve pas comment faire pour envoyer la pièce jointe dans le message.
Merci de vos réponses ![]()
Je sais juste que c´est avec $_FILES mais après ...
La doc est tellement parfaite (ok, je sors --> [- ])
http://fr3.php.net/manual/fr/ref.mail.php#38832
J´ai cherché dans la doc deepblue !
t´as lu les commentaires ?
Oui mais j´ai pas trouvé...
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol;
C´est dans les comm. ...
et avec ça, ça envoie la pièce jointe avec ?
Ben les le commentaire ou se trouve ce bout de code !
![]()
celui la ? nielsvandenberge at hotmail dot com
parce qu´il ne marche pas ![]()
Il me met :
Warning: is_file() [function.is-file]: Unable to access ../../test.doc in /mnt/149/sdb/1/9/greensheep.ftp/test_db.php on line 25
Warning: is_file() [function.is-file]: Unable to access ../../123.pdf in /mnt/149/sdb/1/9/greensheep.ftp/test_db.php on line 25
mail send
Ne te contente pas de copie/collé, faut chercher et modifier !
Ok, faut modifier, mais là, il me dit qu´il manque une fonction ! mais je ne sais pas quoi mettre dans la fonction moi !
donne ton script
PHP ou HTML ??
CODE HTML =>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transition
al.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Send Mail</title>
<style type="text/css">
body
{
background-color:#333333;
color:#FFFFFF;
}
</style>
</head>
<body>
<form action="mail.php" method="post" enctype="multipart/form-data">
<div class="1">Destinataire : </div><input type="text" name="destinataire" width="45" /><br /><br />
<div class="2">Sujet : </div><input type="text" name="sujet" width="45" /><br /><br />
<div class="3">Message : </div><textarea rows="12" cols="30" name="message"></textarea><br /><br />
<div class="4">Pièce jointe : </div><input type="hidden" name="MAX_FILE_SIZE" value="100000" /><input type="file" name="PieceJointe" /><br /><br />
<input type="submit" value="Envoyer" />
</form>
</body>
</html>
CODE PHP => (celui de la doc)
<?php
function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false)
{
$eol="\r\n";
$mime_boundary=md5(time());
# Common Headers
$headers .= ´From: MyName<´.$fromaddress.´>´.$eol;
$headers .= ´Reply-To: MyName<´.$fromaddress.´>´.$eol;
$headers .= ´Return-Path: MyName<´.$fromaddress.´>´.$eol; // these two to set reply address
$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER[´SERVER_NAME´].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$headers .= ´MIME-Version: 1.0´.$eol;
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
$msg = "";
if ($attachments != = false)
{
for($i=0; $i < count($attachments); $i++)
{
if (is_file($attachments[$i]["file"]))
{
# File for Attachment
$file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
$handle=fopen($attachments[$i]["file"], ´rb´);
$f_contents=fread($handle, filesize($attachments[$i]["file"]));
$f_contents=chunk_split(base64_encode($f_contents)
); //Encode The Data For Transition using base64_encode();
fclose($handle);
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
}
}
}
# Setup for text OR html
$msg .= "Content-Type: multipart/alternative".$eol;
# Text Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= strip_tags(str_replace("<br>", "\n", $body)).$eol.$eol;
# HTML Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol´s for better security. see Injection.
# SEND THE EMAIL
ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used !
mail($emailaddress, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
echo "mail send";
}
$emailaddress="to@address.com";
$fromaddress = "from@address.com";
$emailsubject="This is a test mail with some attachments";
$attachments = Array(
Array("file"=>"../../test.doc", "content_type"=>"application/msword"),
Array("file"=>"../../123.pdf", "content_type"=>"application/pdf")
);
$body="This is a message with <b>".count($attachments)."</b> attachments and maybe some <i>HTML</i>!";
send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments);
?>
Je remet le code PHP avec la fonction du MOD :
[php]<?php
function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false)
{
$eol="\r\n";
$mime_boundary=md5(time());
# Common Headers
$headers .= ´From: MyName<´.$fromaddress.´>´.$eol;
$headers .= ´Reply-To: MyName<´.$fromaddress.´>´.$eol;
$headers .= ´Return-Path: MyName<´.$fromaddress.´>´.$eol; // these two to set reply address
$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER[´SERVER_NAME´].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$headers .= ´MIME-Version: 1.0´.$eol;
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
$msg = "";
if ($attachments != = false)
{
for($i=0; $i < count($attachments); $i++)
{
if (is_file($attachments[$i]["file"]))
{
# File for Attachment
$file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
$handle=fopen($attachments[$i]["file"], ´rb´);
$f_contents=fread($handle, filesize($attachments[$i]["file"]));
$f_contents=chunk_split(base64_encode($f_contents)
); //Encode The Data For Transition using base64_encode();
fclose($handle);
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
}
}
}
# Setup for text OR html
$msg .= "Content-Type: multipart/alternative".$eol;
# Text Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= strip_tags(str_replace("<br>", "\n", $body)).$eol.$eol;
# HTML Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol´s for better security. see Injection.
# SEND THE EMAIL
ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used !
mail($emailaddress, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
echo "mail send";
}
$emailaddress="to@address.com";
$fromaddress = "from@address.com";
$emailsubject="This is a test mail with some attachments";
$attachments = Array(
Array("file"=>"../../test.doc", "content_type"=>"application/msword"),
Array("file"=>"../../123.pdf", "content_type"=>"application/pdf")
);
$body="This is a message with <b>".count($attachments)."</b> attachments and maybe some <i>HTML</i>!";
send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments);
?> [/php]
Tu es sur de l´url de tes fichiers ?
Quelle url
?