0

Trying to use the simplest method of File Upload on form and sending the file to my email as an attachment. Only need this for photos. This is becoming a nightmare, I have been searching endlessly online and there's far too many extensive php coding that throws me off. Any help or advice is greatly appreciated. The form submits successfully and I receive the email, BUT NO ATTACHMENT! Please point out what I'm doing wrong here?

Here's the simple HTML.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="sendit.php" method="post" name="mainform" enctype="multipart/form-data">
Name
<br />
<input name="name" type="text">

<br />

Email
<br />
<input name="email" type="text">

<br />
Attach File
<input name="photofile" type="file">

<br />
<br />
<input type="submit" name="Submit" value="Send">

</form>
</body>
</html>

Here's the lovely PHP.

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Variables
$name    = $_POST['name'];
$email    = $_POST['email'];

$to  = '[email protected]' . ', ';
$to .= $Email; 
$subject = 'This Is Becoming A Headache';

// 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.0\n" .
"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.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// 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: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

// Additional Headers
$headers = "From: $name <$email>\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: ".$name." <".$email.">\r\n";
$headers .= "Reply-to: ".$name." <".$email.">\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>
1
  • No, sorry. That's to shoot out an existing attachment. I need user to be able to File Upload and PHP send as an attachment to my email. Commented Feb 10, 2014 at 5:46

2 Answers 2

2

You are overwriting your previous headers here in this below line

$headers = "From: $name <$email>\r\n";
      //^------- You need to add the concatenate operator as shown in the below code.

It should be

$headers .= "From: $name <$email>\r\n";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that fixes the headers. But still no attachment.
Check your $message variable.. You have commented the two lines.
I have the input name as photofile. Should I be calling it anywhere in my PHP?
0

This could be helpful... Send attachments with PHP Mail()?

and this even more if I actually understand correctly what you are looking for... http://www.excellentwebworld.com/send-file-in-email-attachment-on-form-submit-using-php/

1 Comment

I have tried excellentwebworld and it's just not working for me. Im getting back "Error in Sending Email."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.