FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Problem with attaching file (from a form) to email in PHP
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Problem with attaching file (from a form) to email in PHP [message #172661] Thu, 24 February 2011 02:22 Go to previous message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma:
Junior Member
Hi All,

I'm fairly new to PHP, and need some help with a script that I'm
trying to write, to send infos from a form by email (with a file
attched).

I've managed my way to write a script that sends the email in plain
text, but when I tryied to modify it, it seemed that I can't be able
to find the proper way to add the attachment to the email.

Basically, I have a form, that gets all pertinent infos (emails,
subject, ... and I just added the file browser, so the user can attach
the file (pictures) from his local drive).

here is the code I have right now...

If you could help me... it's been 2 weeks that I try to find the
solution, but every new solution I find is not working... I saw some
people use php mailer, or expertmailer (xpm4), but I can't seem to
understand, because t is not working either... Hellllp... pleaaaaasee

<?php

$UploadedFile_name=$_FILES["UploadedFile"]["name"];
$UploadedFile_type=$_FILES["UploadedFile"]["type"];
$UploadedFile_size=$_FILES["UploadedFile"]["size"];
$UploadedFile_temp=$_FILES["UploadedFile"]["tmp_name"];


// MAIL SUBJECT

$subject = $_POST['Sujet'] . " from site XYZ";
$subject = str_replace("\'", "'", $subject);



// MAIL ADDRESS

$to = "System Administrator <JohnDoe(at)gmail(dot)com>";

$nom = str_replace("'", "''", $_POST['Nom']);
$from = $nom . " <" . $_POST['Email'] . ">";



// HTML Message

$message= '


<table cellspacing="0" cellpadding="8" border="0" width="400">
<tr>
<td colspan="2"></td>
</tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Produit</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Produit'].", " . $_POST['Portions'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Date demandée</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'. $_POST['Date'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>'.$_POST['Livraison'].'</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Adresse'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Commentaires</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Commentaires'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Nom</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Nom'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Téléphone</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Telephone'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>

<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Commentaires</strong></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Commentaires'].'</td>
</tr>

<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
</table>

';


// TXT Message


$message = $_POST['Commentaires'] . "\n\n\n\n";
$message .= $nom . "\n";
$message .= $_POST['Telephone'] . "\n\n";
$message = str_replace("\'", "'", $message);
$message = str_replace('\"', '"', $message);




// MAIL HEADERS with attachment

$fp = fopen($UploadedFile_temp, "rb");
$file = fread($fp, $UploadedFile_size);

$file = chunk_split(base64_encode($file));
$num = md5(time());



//Normal headers

$headers = "From: " . $from . "\r\n";
$headers .= "CC: " . $from. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";

// This two steps to help avoid spam (apparently....)

$headers .= "Message-ID: <".gettimeofday()." TheSystem@".
$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";

$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";

// Attachment headers

$headers .= "Content-Type:".$UploadedFile_type." ";
$headers .= "name=\"".$UploadedFile_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$UploadedFile_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";


// SEND MAIL

$sent = mail($to, $subject, $message, $headers);
fclose($fp);

?>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: SOCIAL NETWORK FOR PHP PLATFORM..
Next Topic: HTML select field where the OPTION text is of different colors
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Oct 20 21:37:20 GMT 2024

Total time taken to generate the page: 0.03192 seconds