Upload form with email attachment
Posted: Sun Jul 17, 2011 12:42 pm
Hi Guys
I have got my hands on a great bit of code that works, see below. I have edited it a bit to meet my needs but there is still a few things I would like to do and I don't know how so I wonder if someone can amend this for me.
Would be very grateful if you could help with this.
Thanks, Adrian
I would like to add in a validation to only allow upload of .doc, .docx and .txt files.
I've now found this validation code to put in my own, but where do I put it??
I would like to add some text to display in the textarea to describe the type of thing I want the user to type about.
And finally, I would like it to redirect to a page saying thanks for the file, rather than just staying on the same page and displaying a message saying thanks.
I have got my hands on a great bit of code that works, see below. I have edited it a bit to meet my needs but there is still a few things I would like to do and I don't know how so I wonder if someone can amend this for me.
Would be very grateful if you could help with this.
Thanks, Adrian
Code: Select all
<?php
/* Mailer with Attachments */
$action = "";
$action = $_REQUEST['action'];
global $action;
function showForm() {
?>
<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<p>Name: <input name="from_name" size="50" /></p>
<p>Email: <input name="from_email" size="50" /></p>
<p>Subject: <input name="subject" size="50" /></p>
<p>Message: <textarea name="body" rows="10" cols="50"></textarea></p>
<p>Attachment: <input type="file" name="attachment" size="50" /></p>
<p><input type="submit" value="Send Email" /></p>
<?php
}
function sendMail() {
$from_name = stripslashes($_POST['from_name']);
$subject = stripslashes($_POST['subject']);
$body = stripslashes($_POST['body']);
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
$fp = fopen($attachment, "rb"); //Open it
$data = fread($fp, filesize($attachment)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
fclose($fp);
}
//Let's start our headers
$headers = "From: $from_name<" . $_POST['from_email'] . ">\n";
$headers .= "Reply-To: <" . $_POST['from_email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['from_email'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= "$body\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
// send the message
mail("admin@primarycarecommunity.net", $subject, $message, $headers);
print "Mail sent. Thank you for using the MyNewName5333 Mailer.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<?php
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
</body>
</html>I've now found this validation code to put in my own, but where do I put it??
Code: Select all
// check for valid file types
$allowedExtensions = array("doc","docx","txt");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions)) {
die($file['name'].' is an invalid file type!
'.
''.
'<< Go Back');
}
}
}And finally, I would like it to redirect to a page saying thanks for the file, rather than just staying on the same page and displaying a message saying thanks.