email with attachment
Posted: Thu Aug 31, 2006 12:31 pm
I have been trying to get a form on my site that will allow the user to send an email with an attachment. I located a script thorugh a search that is supposed to allow me to do just that with php. Unfortunately the script seems to be doing everything right except sending the email. I have played with it a bit and found that the problem is somewhere in the $hdr variable, because if I remove that variable from the mail() function, it goes through fine. Of course I kind of need the $hdr variable. Here is the script. Can anyone look at how $hdr is built and see if there are any glaring issues with the code that would cause this problem?
Code: Select all
<?php
if(!isset($_REQUEST['submit'])){
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
<table>
<tr>
<td>To:</td>
<td><input type="text" name="to" size="40"/></td>
</tr>
<tr>
<td>From:</td>
<td><input type="text" name="from" size="40" /></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="re" size="40" /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea cols="30" rows="5" name="comments"></textarea></td>
</tr>
<tr>
<td>Attachment:</td>
<td><input type="file" name="att" size="26" /></td>
</tr>
<td colspan="2"><input type="submit" name="submit" value="Send Form" /></td>
</tr>
</table>
</form>
<?php
}else{
extract($_POST);
$fp = fopen( $att, "r");
$file = fread( $fp, $att_size );
/*
Encode The Data For Transition using base64_encode();
And get a 32-character hexadecimal number
*/
$file = chunk_split(base64_encode($file));
$num = md5( time() );
/*
Define the main message headers
*/
$hdr = "From:".$_REQUEST['from']."\r\n";
$hdr .= "MIME-Version: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed; ";
$hdr .= "boundary=".$num."\r\n";
$hdr .= "--$num\r\n";
/*
Define message section
*/
$hdr .= "Content-Type: text/plain\r\n";
$hdr .= "Content-Transfer-Encoding: 8bit\r\n\n";
$hdr .= "".$_REQUEST['comments']."\r\n";
$hdr .= "--".$num."\n";
/*
Define the attachment section
*/
$hdr .= "Content-Type:". $att_type." ";
$hdr .= "name=\"".$att_name."\"r\n";
$hdr .= "Content-Transfer-Encoding: base64\r\n";
$hdr .= "Content-Disposition: attachment; ";
$hdr .= "filename=\"".$att_name."\"\r\n\n";
$hdr .= "".$file."\r\n";
$hdr .= "--".$num."--";
/*
Send the email
*/
$to = $_REQUEST['to'];
$re = $_REQUEST['re'];
$comments = $_REQUEST['comments'];
$from = "From: me@mysite.com\r\n";
if(mail( $to, $re, $comments, $from, $hdr)){
echo "Email Sent";}
else{
echo "Email Sending Failed";}
/*
Close the attachment
*/
fclose( $fp );
}
?>