2011-10-27 96 views
0

我有一些代碼發送帶附件的電子郵件。我收到附件中的電子郵件,但它顯示了html元素。我正在使用Content-Type:multipart/mixed;邊界= ...當我發送帶有附件的電子郵件時,我會看到電子郵件中的html元素,但是如果我沒有附件發送它,電子郵件將正常顯示。 下面是代碼:用PHP發送html郵件

if(is_uploaded_file($_FILES['attachment']['tmp_name'])){ 
      //die("uploaded"); 
      copy($_FILES['attachment']['tmp_name'],"attachments/" . $_FILES['attachment']['name']); 
      $file = $_FILES['attachment']['tmp_name']; 
      $file_type = $_FILES['attachment']['type']; 
      $file_name = $_FILES['attachment']['name']; 
      $fopen = fopen($file, 'rb'); 
      $data = fread($fopen,filesize($file)); 
      fclose($fopen); 

      $semi_rand = md5(time()); 
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

      $headers = "From: " .EMAIL_FROM_ADDR. "\r\n"; 
      $headers .= "Reply-To: ".EMAIL_FROM_ADDR."\r\n"; 
      $headers .= "CC: [email protected]\r\n";  
      $headers .= "MIME-Version: 1.0\r\n"; 
      $headers .= "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\""; 
      $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". 
         "<table border='1' cellspacing='1' cellpadding='1'>". 
         "<tr><td>Vendor Name:</td><td>".$_POST['vendor_name']."</td></tr>". 
         "<tr><td>Internet Agreement No:</td><td>".$_POST['internet_agreement_no']."</td></tr>". 
         "<tr><td>Vendor Duns No:</td><td>".$_POST['vendor_duns_no']."</td></tr>". 
         "<tr><td>Fee to NEXCOM:</td><td>".$_POST['fee_to_nexcom']."%</td></tr>". 
         "<tr><td>Settlement Worksheet For:</td><td>".$_POST['settlement_worksheet_date']."</td></tr>". 
         "<tr><td colspan='2'>&nbsp;</td></tr>". 
         "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_1']."</td></tr>". 
         "<tr><td>Deductions:</td><td>$".$_POST['deductions']."</td></tr>". 
         "<tr><td>Refunds/Credits(Attach Backup):</td><td>$".$_POST['refunds_credits']."</td></tr>". 
         "<tr><td>Sales Tax:</td><td>$".$_POST['sales_tax']."</td></tr>". 
         "<tr><td>State Tax:</td><td>$".$_POST['state_tax']."</td></tr>". 
         "<tr><td>Other(Explain):".$_POST['explain_1']."</td><td>$".$_POST['other_1']."</td></tr>". 
         "<tr><td>Other(Explain):".$_POST['explain_2']."</td><td>$".$_POST['other_2']."</td></tr>". 
         "<tr><td>Total Deductions:</td><td>$".$_POST['total_deductions']."</td></tr>". 
         "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_2']."</td></tr>". 
         "<tr><td>Less Deductions:</td><td>$".$_POST['less_deductions']."</td></tr>". 
         "<tr><td>Net Sales Subject to Commission:</td><td>$".$_POST['net_sales_subject']."</td></tr>". 
         "<tr><td>Net Commission due to NEXCOM:</td><td>$".$_POST['net_commission_due']."</td></tr>". 
         "<tr><td colspan='2'>Comments:</td></tr>". 
         "<tr><td colspan='2'>".$_POST['comments']."</td></tr>". 
         "</table>\n\n"; 

      $data = chunk_split(base64_encode($data)); 
      $message .= "--{$mime_boundary}\n". 
         "Content-Type: {$file_type}; name=\"{$file_name}\"\n". 
         "Content-Disposition: attachment; filename=\"{$file_name}\"\n". 
         "Content-Transfer-Encoding: base64\n\n". 
         $data."\n\n". 
         "--{$mime_boundary}--\n";  
     } 
     else{ 
      //die("not uploaded"); 
      $headers = "From: " .EMAIL_FROM_ADDR. "\r\n"; 
      $headers .= "Reply-To: ".EMAIL_FROM_ADDR."\r\n"; 
      $headers .= "CC: [email protected]\r\n"; 
      $headers .= "MIME-Version: 1.0\r\n"; 
      $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
      $message = "<table border='1' cellspacing='1' cellpadding='1'>". 
         "<tr><td>Vendor Name:</td><td>".$_POST['vendor_name']."</td></tr>". 
         "<tr><td>Internet Agreement No:</td><td>".$_POST['internet_agreement_no']."</td></tr>". 
         "<tr><td>Vendor Duns No:</td><td>".$_POST['vendor_duns_no']."</td></tr>". 
         "<tr><td>Fee to NEXCOM:</td><td>".$_POST['fee_to_nexcom']."%</td></tr>". 
         "<tr><td>Settlement Worksheet For:</td><td>".$_POST['settlement_worksheet_date']."</td></tr>". 
         "<tr><td colspan='2'>&nbsp;</td></tr>". 
         "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_1']."</td></tr>". 
         "<tr><td>Deductions:</td><td>$".$_POST['deductions']."</td></tr>". 
         "<tr><td>Refunds/Credits(Attach Backup):</td><td>$".$_POST['refunds_credits']."</td></tr>". 
         "<tr><td>Sales Tax:</td><td>$".$_POST['sales_tax']."</td></tr>". 
         "<tr><td>State Tax:</td><td>$".$_POST['state_tax']."</td></tr>". 
         "<tr><td>Other(Explain):".$_POST['explain_1']."</td><td>$".$_POST['other_1']."</td></tr>". 
         "<tr><td>Other(Explain):".$_POST['explain_2']."</td><td>$".$_POST['other_2']."</td></tr>". 
         "<tr><td>Total Deductions:</td><td>$".$_POST['total_deductions']."</td></tr>". 
         "<tr><td>Total Gross Sales:</td><td>$".$_POST['total_gross_sales_2']."</td></tr>". 
         "<tr><td>Less Deductions:</td><td>$".$_POST['less_deductions']."</td></tr>". 
         "<tr><td>Net Sales Subject to Commission:</td><td>$".$_POST['net_sales_subject']."</td></tr>". 
         "<tr><td>Net Commission due to NEXCOM:</td><td>$".$_POST['net_commission_due']."</td></tr>". 
         "<tr><td colspan='2'>Comments:</td></tr>". 
         "<tr><td colspan='2'>".$_POST['comments']."</td></tr>". 
         "</table>"; 
     }   

回答

1

你在你的郵件正文寫Content-Type: text/plain;。將其設置爲text/html應該有所斬獲。