PDA

View Full Version : Form to send mail+attachment



AdrielGreene
03-14-2009, 07:41 AM
I've been trying to create a html form which would allow the user to send an email and attachment wave file email. The email is defined by a simple .txt file. So far I can successfully upload the file into a folder and can send an email with just the text section of the form.

To send the attachment I threw out a google search. The page I chose to follow was http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php which has a nice guide. The final example (which includes the attachment script) is as follows:


<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//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
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//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/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--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
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

The example code does not send the message body. It sends the file but drops the name making the attachment "noname" with no extension. I have tried renaming the file to its original name but it will no longer be read as a wave file. I was pondering how to use this code and trying to understand how to make it work. I wanted to incorporate it into my own code so when I figured it out I'd have at least that step done. This is what I created: 2 mail() functions sending my data.


<?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 "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br />";
$samplestored = move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]) . "<br />";
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"] . "<br />";
$samplewav = "uploads/" . $_FILES["file"]["name"];
}
}
else
{
echo "Invalid file type or size";
}
?>

<?php
//sending the files
//-----------------------------------------------------------------
//Variables for body of message two
$name = $_POST['name'];
$phone = $_POST['phone'];
$question = $_POST['question'];
$mail = $_POST['mail'];
$messagetwo = "Someone has sent a sample through the website form.\n\n Name: $name \n Email: $mail \n Phone: $phone \n question: $question" ;
$headertwo = "From: " . $_POST['name'];

//variables for email one
$to = file_get_contents("email.txt");
$subject = 'Sample upload';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: " . $_POST['name'];
//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
$attachment = chunk_split(base64_encode(file_get_contents($samplewav)));
//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-mixed-<?php echo $random_hash; ?>
Content-Type: audio/wav; name=$samplewav
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
if ($samplestored) {
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
mail($to, $subject, $messagetwo, $headertwo );
}
?>
<br>
<a href="sample.php"><b>Return to step 3</b></a>

Here is the form:


<form action="submit_sampleO.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="900000"/>
Full name: <input type="text" name="name" tabindex="1">
<br>
E-Mailing address: <input type="text" name="mail" tabindex="2">
<br>
Phone number: <input type="text" name="phone" tabindex="3">
<br>
Any initial question: <textarea name="question" tabindex="4" rows="4" cols="23" WRAP=HARD></textarea>
<br>
Upload sample: <input type="file" name="file" tabindex="5">
<br>
<input type="submit" value="Submit Sample" name="submit" tabindex="6">
</form>

Now my code sends the information to the correct address, in two emails, with the correct information and a file around the same size as the one I input into the form. The file is named "noname" after it is sent.

So now I'm somewhat stuck. I am hoping someone can help me with my code or send me in the right direction. /bow