I have spent the day (quite literally) working on an email form that replicates to some degree the functionality of Gmail. The user will be able to enter as many attachments as they like. The form is fine, attachments are uploaded to a temporary directory, added to the email and sent. There are no errors thrown anywhere. However, the attachments arriving are corrupted (I cannot open them) even though their mime type and filenames are all working as expected. I have chopped and changed the code as much as I can and at this stage I'm blind from trying to resolve it. Any help that can be shed on this would be super.
I do not have access to the command line on the server to install PEAR to use that library. I have been told there is a way to use PEAR as an uploaded library, but I need to research that later. I'd consider my php very good, even if I do have an aversion to classes (never really got my head around oo).
The email attachment code I originally modified can be found here:
http://www.webcheatsheet.com/PHP/send_e ... attachment
I am using a named array to pass the variables from the form to the script:
Code: Select all
<script type="text/javascript">
function addAttach(){
if(document.getElementById('aRow').style.display == "none") document.getElementById('aRow').style.display="";
aDiv = document.getElementById('aCell');
aFile = "<input type=file name=[color=#FF0000]att[][/color] class=conBoxMail size=50/><br/>";
aDiv.innerHTML += aFile;
}
</script>
...
<tr id="aRow" style="display:none;"><td align="right" valign="top" class="con">attach:</td><td valign="top" align="left" class="con"><div id="aCell"></div></td></tr>
The form is submitted and processed by the following script (I am doing this for a client, so email and names have been changed):
Code: Select all
<?php #Email information
$i=0;
$msg = "";
foreach($_FILES["att"]["error"] as $key => $error) {
if($error == UPLOAD_ERR_OK){
$tmp_name = $_FILES["att"]["tmp_name"][$key];
$name = $_FILES["att"]["name"][$key];
$n[$i] = $name;
[color=#FF0000]$type = $_FILES["att"]["type"][$key];[/color]
[color=#4000FF]$content[$i]='Content-Type: '.$type.'; name="'.$name.'"';[/color]
if(move_uploaded_file($tmp_name, "attach/$name")){
// echo "1. File uploaded : {$name}<br/>"; Debugging
}else{
echo "Attachment not uploaded : {$name}<br/>";
}
$attachment[$i] = "attach/$name";
}
$i++;
}
$from = "me@me.com";
$subject = $_POST['subject'];
$email_msg = $_POST['content'];
$email_msg = stripslashes($email_msg);
$query = "select email from members";
$result = @mysql_query($query); // the form will eventually send the email to a list of people, taken from a db
$to = "test@me.com"; // my email address I'm using to test this at the moment
//------------- This bit is taken from the tutorial mentioned in the link above -------------
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: Client Sender <$from>";
//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
[color=#FF0000]// --------------- because there are an unknown number of attachments, I'm doing this bit on the fly in the code below[/color]
//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/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo $email_msg; ?>
--PHP-alt-<?php echo $random_hash; ?>--
[color=#FF0000]<?php $j=0; foreach($attachment as $attach){?>[/color]
--PHP-mixed-<?php echo $random_hash; ?>
[color=#4000FF]<?php echo $content[$j]; ?>[/color]
[color=#4000FF]Content-Disposition: attachment[/color]
[color=#4000FF]filename="<?php echo $n[$j]; ?>"[/color]
[color=#4000FF]Content-Transfer-Encoding: base64 [/color]
[color=#FF0000]<?php $attached = chunk_split(base64_encode(file_get_contents($attach))); echo $attached; $j++;}?>
[/color]
--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
if(@mail($to, $subject, $message, $headers)) $i++;
if($i>0){
$j=0;
foreach($attachment as $attached){
echo 'content type: '.$content[$j].'<br/>attachment: '.$attached.'<br/> <br/>';
$j++;
//this is just to make sure that some of the settings are being output correctly
}
echo "<div align=\"center\"><b>Your email has been sent.</b></div>";
} else {
echo "<div align=\"center\"><b>Your email has NOT been sent. Go back and try again.</b></div>";
}
?>As you can see I've (modestly!) got some pretty nifty code in there. It dynamically generates the attachment, including the correct mime type, etc. I'm pretty happy with the work I've done on it, but I'm just that final hurdle away from it. It's 2am now and apart from a break for lunch, I've been at this since 10am this morning. I don't think I'm going to be able to find the solution at this stage. I've been stumped on this final encoding issue since 8pm, so I can assure you I've Googled and searched for an answer but to no avail.
I'm off to bed! Thanks a million for any help...
T