I had a lot of trouble with sending mail attachments when I was making a form that would email a member of my site a .wav file. This is my code in full - hope it helps.
ps. make a folder to hold the attachment. mines named 'uploads'. Also the email for my sites member was stored in a file called email.txt.
---
the form displayed to the user
---
Code:
<input type="hidden" name="MAX_FILE_SIZE" value="900000"/>
<table border="0" bgcolor="blue" width="350height="300" align="left">
<tr>
<td>Full name:
<td><input type="text" name="name" tabindex="1" size="28"></td>
</tr>
<tr>
<td>E-Mailing address:</td>
<td><input type="text" name="mail" tabindex="2" size="28"></td>
</tr>
<tr>
<td>Phone number:</td>
<td><input type="text" name="phone" tabindex="3" size="28"></td>
</tr>
<tr>
<td>Any initial question:<br><br><br><br></td>
<td><textarea name="question" tabindex="4" rows="4" cols="23" WRAP=HARD></textarea></td>
</tr>
<tr>
<td>Upload sample:</td>
<td><input type="file" name="file" tabindex="5"></td>
</tr>
</table>
<br><br><br><br><br><br><br><br><br><br><br><br>
<input type="submit" value="Submit Sample" name="submit" tabindex="6">
</form>
--------
submit_sample.php
--------
Code:
<?php
//uploading the files
//-----------------------------------------------------------------
if (($_FILES["file"]["type"] == "audio/wav") && ($_FILES["file"]["size"] < 900000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
$samplestored = move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]) . "<br />";
$samplewav = "uploads/" . $_FILES["file"]["name"];
}
}
else
{
echo "Invalid file type or size";
}
?>
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$question = $_POST['question'];
$mail = $_POST['mail'];
$message = "Someone has sent a sample through the website form.<br><br> Name: $name <br> Email: $mail <br> Phone: $phone <br> question: $question" ;
$fileatt = "$samplewav"; // Path to the file
$fileatt_type = "audio/wav"; // File Type
$fileatt_name = $_FILES["file"]["name"]; // Filename that will be used for the file as the attachment
$email_from = "mysite"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Thanks for useing mysite.com <br>";
$email_message .= "$message"; // Message that the email has in it
$email_to = file_get_contents("email.txt"); // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
unlink($samplewav);
if($ok) {
echo "<font face=verdana size=2><center>Your sample has been submitted.<br>
Click <a href=\"#\" onclick=\"history.back();\">here</a> to return.</center>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
Bookmarks