Should be pretty easy. I'm not familiar with the code for this but the box for "file" is pretty simple. Just a text box with browse and when you POST it should send whatever file you wrote in the box. You could look it up as an input type, I guess.
0
KwitkoSheriff of Banning (Retired)By the thing near the stuffIcrontian
well a teacher approached me with the question. he wanted kids to be able to send attachments to themselves since our tech dept blocked hotmail, yahoo, etc. if someone can just get the attachment part of the php for me i can do the rest of the form. i don't know enough about php to be able to do it yet. i _think_ i know the html to get a box for finding the file but from there i don't have the slightest clue.
Cyrix: It was after 11pm EST when he made that post, so it will probably be sometimes later today (as it is just after 6:30am EST now) before he posts it.
Just modify my mailform and add that in...not too hard.
[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.
0
KwitkoSheriff of Banning (Retired)By the thing near the stuffIcrontian
edited December 2003
CyrixInstead had this to say Will you be posting how to do it Mr. Kwitko?
~Cyrix
]
Sure!
0
KwitkoSheriff of Banning (Retired)By the thing near the stuffIcrontian
edited December 2003
Sending attachments through PHP's mail() function is difficult because the headers must be correct and the attachment has to be encoded into base64 before sending.
oo.. interesting. I'm waiting to see what you make too
0
KwitkoSheriff of Banning (Retired)By the thing near the stuffIcrontian
edited December 2003
As promised. Now I didn't add any error checking or formatting. My goal was simply to make sure the MIME encoding was done right. MIME headers are *very* finicky. If you want to add more attachments, you could foreach or while the chunk routine.
</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?
0
KwitkoSheriff of Banning (Retired)By the thing near the stuffIcrontian
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.
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!
NightShade737 had this to say If you set action then it needs to be present. I.e. if the line "action=something" isn't present in the URL then you are going to get an error....
NS
That's if it were a $_GET. All the form variables here are $_POST.
KwitkoSheriff of Banning (Retired)By the thing near the stuffIcrontian
edited December 2003
Yeah, but you shouldn't have to. Try doing that php.ini fix I mentioned then set the script back to the way I had it originally and report your results.
0
KwitkoSheriff of Banning (Retired)By the thing near the stuffIcrontian
I don't want to. That's like supressing warnings when compiling a C/C++ app . . . it warns for a reason. Perhaps in this particular case there is no harm, but I'd rather see the warning so I can fix it.
Mr. Kwitko had this to say Yeah, but you shouldn't have to. Try doing that php.ini fix I mentioned then set the script back to the way I had it originally and report your results.
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?
[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.