/    Sign up×
Community /Pin to ProfileBookmark

Email Form with File Attachemnt

Hi There i’m trying to allow my user to submit a form to send to me with a file attachemt. Everthing works great except the attachemt of the file.

Can anyone help me? here is my form code:

[code=php]
<form name=”form” enctype=”multipart/form-data” method=”post” action=”send-coments1.php”>
<?php
if ($error == 1)
{
echo “<p>Sorry, we were unable to process your registration.<br>
<b>Some fields were not filled in. Please Try Again</b>.</p>”;
}
?>
Name:
<input name=”Name” type=”text” size=”40″ value=<? echo $Name; ?>>
Email Address:
<input name=”Email” type=”text” size=”40″ value=<? echo $Email; ?>>
Add Image:
<input name=”fileatt” type=”file” id=”fileatt” size=”40″>
Commentsan:
<textarea name=”Comments” rows=”5″ cols=”50″ value=<? echo $Comments; ?>></textarea>
<input type=”submit” name=”Submit” value=”Submit”>
<input type=”reset” name=”Reset” value=”Clear Form”>
</form>
[/code]

Here is my email script:

[code=php]
<?php
$error = 0;

if (($Name == “”) ||($Email == “” )|| ($Comments == “”))
{
$error = 1;
}
if ($error == 1)
{
$l = “Location: [url]http://www.websiteaddress.com/contact1.php?error=1[/url]”;
$l .=”&Name=$Name&Email=$Email&Comments=$Comments”;
header($l);
exit;
}

//Define some variables
foreach($_FILES as $value) {
foreach($value as $k => $v) {
echo $k.’ => ‘.$v.'<br>’;
}
}

if($_FILES[‘fileatt’][‘name’] == ”) {
echo ‘You did not select a photo to upload’;
}
elseif($_FILES[‘fileatt’][‘size’] == 0) {
echo ‘There appears to be a problem with the photo your are uploading’;
}
elseif($_FILES[‘fileatt’][‘size’] > $MAX_FILE_SIZE) {
echo ‘The photo you selected is too large’;
}
elseif(!getimagesize($_FILES[‘fileatt’][‘tmp_name’
])) {
echo ‘The photo you selected is not a valid image file’;
}
else {
$uploaddir = ‘uploads/’; // remember the trailing slash!
$uploadfile = $uploaddir . $_FILES[“fileatt”][‘name’];
if(move_uploaded_file($_FILES[‘fileatt’][‘tmp_name
‘], $uploadfile)) {
echo ‘Upload file success!’;
}
else {
echo ‘There was a problem uploading your file.<br>’;
print_r($_FILES);
}
}

$youremail=”[email protected]”;

$emailsubject=”Contact!”;

$from_who=”<[email protected]>”;

$pagetitle=”Thank You!”;

if (getenv(HTTP_CLIENT_IP)){
$user_ip=getenv(HTTP_CLIENT_IP);
}
else {
$user_ip=getenv(REMOTE_ADDR);
}

$Name = $_POST[‘Name’];
$Email = $_POST[‘Email’];
$Comments = $_POST[‘Comments’];
$datafile = $_POST[‘datafile’];

$mailbody=”Senders Name:n=================n$Namenn”;
$mailbody.=”Senders Email:n=================n$Emailnn”;
$mailbody.=”Senders Comments:n=================n$Comments”;

mail(“$youremail”, “$emailsubject”, “$mailbody”, “From: $from_who”); // Send the email.

$comments = nl2br($comments);

$k = “Location: [url]http://www.websiteaddress.com/contact_thanks1.php?[/url]”;
header($k);

?>

[/code]

Please Help Me i’m going crazy
LOL Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@RedheadauthorDec 24.2004 — Hi everyone ok, i'm getting no help with this on.. So i search through some old threds and found something that would be helpful. but was not...

here is my new php code. like i sayid before the email part works it's just NOT attaching any files.... HELP ME PLEASE PLEASE
[code=php]
<?php

$error = 0;

if (($Name == "") ||($Email == "" )|| ($Comments == ""))
{
$error = 1;
}
if ($error == 1)
{
$l = "Location: http://www.mywebsite.com/contact1.php?error=1";
$l .="&Name=$Name&Email=$Email&Comments=$Comments";
header($l);
exit;
}

$youremail="myemail address";
// This is the address that the information submitted will be sent to.

$emailsubject="Contact form info!"; // This will the email's subject

$from_who="$Email";

$pagetitle="Thank You!"; // Title to be displayed on the sent info page.

if (getenv(HTTP_CLIENT_IP)){
$user_ip=getenv(HTTP_CLIENT_IP);
}
else {
$user_ip=getenv(REMOTE_ADDR);
}

// Read POST request params into global vars
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Comments = $_POST['Comments'];
$fileatt = $_POST['fileatt'];

// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "nMIME-Version: 1.0n" .
"Content-Type: multipart/mixed;n" .
" boundary="{$mime_boundary}"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.nn" .
"--{$mime_boundary}n" .
"Content-Type: text/plain; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" .
$message . "nn";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}n" .
"Content-Type: {$fileatt_type};n" .
" name="{$fileatt_name}"n" .
//"Content-Disposition: attachment;n" .
//" filename="{$fileatt_name}"n" .
"Content-Transfer-Encoding: base64nn" .
$data . "nn" .
"--{$mime_boundary}--n";
}

$mailbody="Senders Name:n=================n$Namenn";
$mailbody.="Senders Email:n=================n$Emailnn";
$mailbody.="Senders Comments:n=================n$Comments";

mail("$youremail", "$emailsubject", "$mailbody", "From: $from_who"); // Send the email.

$comments = nl2br($comments);

?>

[/code]
×

Success!

Help @Redhead spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...