PHP email form with attachments?
AuthorityAction
Missouri Member
Could you make a php email form that can send attachments?
0
Comments
the form would look something like this:
to: < addy box >
subject: < box >
message: <bigger box >
attachment: < yet another box >
< send >
something like that...
edit: oops forgot to add some spaces...
awesome! thanks a lot. I'm in no hurry so just whenever you have time.
~Cyrix
[php]
<?php
if ($do != "sendmail") {
?>
<form name="sendmail" action="$PHP_SELF" method="post">
<font face="Verdana,Arial" size="2">
<input type="hidden" name="do" value="sendmail">
E-Mail: <input type="text" name="email" size="60">
Subject: <input type="text" name="subject" size="60">
Comments:<textarea name="comments" rows="10" cols="30"></textarea>
<input type="submit" value="submit">
</font>
</form>
<?php
} elseif ($do == "sendmail") {
mail("your@email.com",$subject,$email,$comments) or die("Error: the message could not be sent");
} // endif
?>
[/php]
or http://www.short-media.com/forum/showthread.php?s=&threadid=6803.
Sure!
Without further ado, I give you:
[php]
<?php
/* Mailer with Attachments */
$action = $_REQUEST;
global $action;
function showForm() {
?>
<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<p>Recipient Name: <input name="to_name" size="50" /><br />
Recipient Email: <input name="to_email" size="50" /><br />
From Name: <input name="from_name" size="50" /><br />
From Email: <input name="from_email" size="50" /><br />
Subject: <input name="subject" size="50" /><br />
Message: <textarea name="body" rows="10" cols="50"></textarea><br />
Attachment: <input type="file" name="attachment" size="50" /><br />
<input type="submit" value="Send Email" /></p>
<?php
}
function sendMail() {
if (!isset ($_POST)) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$to_name = stripslashes($_POST);
$from_name = stripslashes($_POST);
$subject = stripslashes($_POST);
$body = stripslashes($_POST);
$to_email = $_POST;
$attachment = $_FILES;
$attachment_name = $_FILES;
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 . ">\n";
$headers .= "Reply-To: <" . $_POST . ">\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 . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST . ">\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("$to_name<$to_email>", $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>
****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style="css" type="text/css">
<!--
body {
margin: 0px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
a {color: #0000ff}
-->
</style>
</head>
<body>
<?php
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
</body>
</html>
[/php]//EDIT: added some additional comments. I realized after posting that it's kind of silly to add the MIME boundary if you're not neccessarily sending an attachment, so the encoding routine plus the boundaries for the attachment should really be conditional. Then again, the whole purpose of this script is so you can send attachments, right?
Offending line: $action = $_REQUEST;
[php]
$action = "";
$action = $_REQUEST;
[/php]
7: $action = "";
8: $action = $_REQUEST;
Where you see
add
Still haven't fixed the $action error, but I'm not very handy w/ PHP. I guess I should check to see if $_REQUEST exists before trying to use it.
NS
Okay, I think the problem is that error_reporting is tweaked too high. Go to your php.ini and look for error_reporting, then set it to
It's a warning, not an error, right? The script still works?
OMG, how did I miss $subject? The best thing to do would be to make it
[php]
$subject = stripslashes($_POST);
[/php]
So you get Mr. Kwitko's Script instead of Mr. Kwitko\'s Script
It would be a good idea to "massage" the names, message body, and subject with stripslashes. I think I'll go do that now!
That's if it were a $_GET. All the form variables here are $_POST.
and it no longer gripes.