PHP联系表单 - 图像上传到临时但不附加到电子邮件

I was just wondering if anyone could help out with what should be a fairly simple fix (obviously not for me) for a contact form with image upload. The image DOES get uploaded to the temporary folder, but doesn't appear when I receive the email into my account.

I think the form is nearly there, just can't figure out why the image is not attatching to the email when i receive it - guessing the problem is somewhere in the last 10 lines of code.

I would really appreciate if someone could figure what's going wrong.

<?php

ini_set("sendmail_from", "darren@mywebsite.co.uk");
ini_set("SMTP", "smtp.myhostingcompany.co.uk");

if($name = filter_var($_POST['name']))
if($address = filter_var($_POST['address']))
if($postcode = filter_var($_POST['postcode']))
if($phone = filter_var($_POST['phone']))
if($email = filter_var($_POST['email']))
if($details = filter_var($_POST['details']))
if($contactby = filter_var($_POST['contactby']))

/* Subject and Email Destinations */
$emailSubject = 'Work Email!';
$webMaster = 'darren@mywebsite.co.uk';

/* Gathering Data Variables */
$nameField = $_POST['name'];
$addressField = $_POST['address'];
$postcodeField = $_POST['postcode'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$detailsField = $_POST['details'];
$contactbyField = $_POST['contactby'];

$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB)  
$upload_path = './uploads/'; 

$filename = $_FILES['userfile']['name']; 
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 

if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');

if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');

if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href=
"' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload.  Please try again.'; // It failed

$body = <<<EOD
Name: $name
Address: $address
Postcode: $postcode
Phone: $phone
Email: $email
Details: $details
Contactby: $contactby
EOD;

$headers = "From: $email
";
$headers .= "Contact-type: text/html
";
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];
$success = mail($webMaster, $attatchment, $body, $headers, "-fdarren@mywebsite.co.uk");

/* Results rendered as HTML */

$theResults = <<<EOD
SENT SENT SENT
EOD;
echo "$theResults"
?>

Your mail headers are wrong, you need to use MIME to define the different content types (The body of the email and the attachment).

I have some code somewhere that I use for emailing uploads, if this isn't answered for you in about 10-20 minutes I'll try and find it for you.

Good luck!

I managed to dig up the code! It's not the cleanest code ever but it gets the job done. There will be a few tweaks you will have to do as this was originally written to email CSV files, but I've commented it all as much as I can for you.

//The recipient(s) who will receive this email.
$mail_recipients = 'example@email.com';

//This is the subject line of the email.
$mail_subject .= 'A new CVS has arrived!';

//This is the emails body.
$message .= 'A new CVS file has been generated and sent to you, please find it attached to this email. <br />';

//This is the full path to the file you want to attach.
$full_path = '/example/path/file.csv';

//The file type of the attachment.
$file_type = 'text/csv';

//Create a file handle to open the file.
$file = fopen($full_path,'rb'); 

//Read all of the files contents into a variable.
$data = fread($file, filesize($full_path)); 

//encode and split the contents.
$data = chunk_split(base64_encode($data)); 

//Close the file, we don't need it anymore!
fclose($file); 

//Create a unique boundary
$new_boundary = md5(time());

//This is your basic header information such as who the email is from and the date it was sent.
$mail_header .= 'From: CSV Admin <noreply@website.co.uk>' . "
";
$mail_header .= 'Date: ' . date('r') . "
";

//This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
$mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "

";
$mail_header .= 'MIME-Version: 1.0' . "
";
$mail_header .= 'This is a multi-part message in MIME format' . "
";
$mail_header .= '--' . $new_boundary . "
";
$mail_header .= 'Content-Type:text/html; charset="iso-8859-1"' . "
";
$mail_header .= 'Content-Transfer-Encoding: 7bit' . "

";
$mail_header .= $message . "

";

//This part of the header is for the attachment and includes the contents of the file, the files name and other information.
$mail_header .= '--' . $new_boundary . "
";
$mail_header .= 'Content-Type: ' . $file_type . '; name="' . $file_name . '"' . "
";
$mail_header .= 'Content-Transfer-Encoding: base64' . "
";
$mail_header .= 'Content-Disposition: attachment; filename="' . $file_name . '"' . "

";
$mail_header .= $data . "

";

//This is needed to stop any rendering errors by email clients and signifies the end of the emails header.
$mail_header .= '--' . $new_boundary  . '--' . "
";

//This mails out all of the above.
mail($mail_recipients, $mail_subject, '', $mail_header);

//And we should be done!

You have three distinct errors:

  1. The headers for multipart email are a bit more complex.
  2. The parameters to the mail function are incorrect.
  3. You’re passing an undefined variable to file_get_contents.
$mime_boundary = md5(time());

$headers = "From: $email
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: multipart/mixed;
";
$headers .= "--".$mime_boundary."--
";

$message .= "This is a multi-part message in MIME format.

";
$message .= "--".$mime_boundary."--
";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"
";
$message .= "Content-Transfer-Encoding: 7bit
";
$message .= "
";
$message .= "$body
";
$message .= "--".$mime_boundary."--
";

$message .= chunk_split(base64_encode(file_get_contents($_FILES['userfile']['tmp_name'])));

mail($webMaster, 'email subject text', $message, $headers);

SitePoint has an in-depth article on Advanced email in PHP Article that you may find beneficial.

Like the guys said your content type is incorrect and you have:

$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];

It should be

$tmp_file = $_FILES['userfile']['tmp_name'];
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));