PHP订单表单-电子邮件与附件


PHP Order Form - Email with Attachment

我试图创建一个订单表单,当完成时,传递给你到order.php页面(发送电子邮件),在那里你被传递到贝宝。一切工作正常,直到我试图添加附件,当我添加附件的代码,电子邮件不再发送。

<?php 
$to = 'hidden' ; 
$from = $_REQUEST['email'] ; 
$name = $_REQUEST['name'] ; 
$headers = "From: $from"; 
$subject = "Distinctive Writers - Contact Form"; 
$tprice = $_REQUEST['tprice'] ;
$fields = array(); 
$fields{"name"} = "name";  
$fields{"email"} = "email"; 
$fields{"number"} = "number"; 
$fields{"subject"} = "subject"; 
$fields{"doctype"} = "doctype";
$fields{"spec"} = "spec";
$fields{"grade"} = "grade";
$fields{"days"} = "days";
$fields{"due"} = "due";
$fields{"pages"} = "pages";
$fields{"price"} = "price";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$file_name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
if (file_exists($tmp_name)){
  // Check to make sure that it is an uploaded file and not a system file
  if(is_uploaded_file($tmp_name)){
     // Now Open the file for a binary read
     $file = fopen($tmp_name,'rb');
     // Now read the file content into a variable
     $data = fread($file,filesize($tmp_name));
     // close the file
     fclose($file);
     // Now we need to encode it and split it into acceptable length lines
     $data = chunk_split(base64_encode($data));
 }
// Now we'll build the message headers
  $headers = "From: $from'r'n" .
     "MIME-Version: 1.0'r'n" .
     "Content-Type: multipart/mixed;'r'n" .
     " boundary='"{$mime_boundary}'"";
   // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached
  $message .= "--{$mime_boundary}'n" .
     "Content-Type: {$type};'n" .
     " name='"{$file_name}'"'n" .
     //"Content-Disposition: attachment;'n" .
     //" filename='"{$fileatt_name}'"'n" .
     "Content-Transfer-Encoding: base64'n'n" .
     $data . "'n'n" .
     "--{$mime_boundary}--'n";

$body = "We have received the following information:'n'n"; foreach($fields as $a => $b){    $body .= sprintf("%20s: %s'n",$b,$_REQUEST[$a]); } 
$headers2 = "From: noreply@YourCompany.com"; 
$subject2 = "Thank you for contacting us"; 
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
if($from == '') {print "You have not entered an email, please go back and try again";} 
else { 
if($name == '') {print "You have not entered a name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers, $message); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
}
}
if(!mail($to, $subject, $body, $headers, $message)){
print "ERROR!!";
}
}
?> 

我也推荐pc-shooter所提到的PHPMailer

则可以使用

$mail = new PHPMailer();

那么你可以使用这两个

中的任何一个
$mail->AddStringAttachment($string,$filename)
$mail->AddAttachment($path);

要记住的是,其他东西可能会阻止电子邮件。如果邮件函数返回TRUE,则邮件已发送。