PHP订单 - 带附件的电子邮件

时间:2014-05-28 14:42:25

标签: php email attachment

我正在尝试创建一个订单表单,在完成后,将您传递到order.php页面(发送电子邮件),然后将其传递给paypal。一切正常,直到我尝试添加附件,当我添加附件代码时,不再发送电子邮件。

<?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!!";
}
}
?> 

1 个答案:

答案 0 :(得分:0)

我也推荐使用pcM射手

提到的PHPMailer

然后你可以使用

$mail = new PHPMailer();

然后你可以利用这两个中的任何一个

$mail->AddStringAttachment($string,$filename)
$mail->AddAttachment($path);

要记住一些事情,其他可能会阻止电子邮件。如果mail函数返回TRUE,则调度邮件。