通过HTML表单在PHP中发送带附件的邮件

时间:2015-04-21 09:34:42

标签: php html

我在使用附件发送邮件时遇到问题。 问题是它没有表现出类似“成功”的信息。或者'失败'我也没有收到任何邮件。

我无法理解这是什么问题?因为它没有显示任何东西。

这是我的代码

<form action="emailSend.php" method="post" name="mainform" enctype="multipart/form-data">
<table width="500" border="0" cellpadding="5" cellspacing="5">
   <tr>
    <th>Your Name</th>
    <td><input name="fieldFormName" type="text"></td>
</tr>
<tr>
<tr>
    <th>Your Email</th>
    <td><input name="fieldFormEmail" type="text"></td>
</tr>
<tr>
    <th>To Email</th>
    <td><input name="toEmail" type="text"></td>
</tr>

<tr>
    <th>Subject</th>
    <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
</tr>
<tr>
    <th>Comments</th>
    <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
</tr>
<tr>
  <th>Attach Your File</th>
  <td><input name="attachment" type="file"></td>
</tr>
<tr>
    <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>

这是我的PHP脚本

<?php

$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail']; 
$fromName = $_POST['fieldFormName']; 
$subject = $_POST['fieldSubject']; 
$message = $_POST['fieldDescription'];

/* GET File Variables */ 
$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']; 
$fileName = $_FILES['attachment']['name']; 

/* Start of headers */ 
$headers = "From: $fromName"; 

if (file($tmpName)) { 
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb'); 
  $data = fread($file,filesize($tmpName)); 
  fclose($file); 

  /* a boundary string */
  $randomVal = md5(time()); 
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

  /* Header for File Attachment */
  $headers .= "nMIME-Version: 1.0n"; 
  $headers .= "Content-Type: multipart/mixed;n" ;
  $headers .= " boundary="{$mimeBoundary}""; 

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.nn" . 
  "--{$mimeBoundary}n" . 
  "Content-Type: text/plain; charset="iso-8859-1"n" . 
  "Content-Transfer-Encoding: 7bitnn" . 
  $message . "nn"; 

  /* Encoding file data */
  $data = chunk_split(base64_encode($data)); 

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}n" . 
  "Content-Type: {$fileType};n" . 
  " name="{$fileName}"n" . 
  "Content-Transfer-Encoding: base64nn" . 
  $data . "nn" . 
  "--{$mimeBoundary}--n"; 
} 

$flgchk = mail ("$to", "$subject", "$message", "$headers"); 

if($flgchk){
  echo "A email has been sent to: $to";
 }
else{
  echo "Error in Email sending";
}
?>

1 个答案:

答案 0 :(得分:0)

试试这个,

 <?php

 $to = $_POST['toEmail'];
 $fromEmail = $_POST['fieldFormEmail']; 
 $fromName = $_POST['fieldFormName']; 
 $subject = $_POST['fieldSubject']; 
 $message = $_POST['fieldDescription'];

 /* GET File Variables */ 
 $tmpName = $_FILES['attachment']['tmp_name']; 
 $fileType = $_FILES['attachment']['type']; 
 $fileName = $_FILES['attachment']['name']; 

 /* Start of headers */ 
 $headers = "From: $fromName"; 

 if (file($tmpName)) { 
   /* Reading file ('rb' = read binary)  */
   $file = fopen($tmpName,'rb'); 
   $data = fread($file,filesize($tmpName)); 
   fclose($file); 

   /* a boundary string */
   $randomVal = md5(time()); 
   $mimeBoundary = "==Multipart_Boundary_x".$randomVal."x"; 

   /* Header for File Attachment */
   $headers .= "nMIME-Version: 1.0n"; 
   $headers .= "Content-Type: multipart/mixed;n" ;
   $headers .= " boundary=".$mimeBoundary.""; 

   /* Multipart Boundary above message */
   $message = "This is a multi-part message in MIME format.nn".$mimeBoundary."n" . 
   "Content-Type: text/plain; charset='iso-8859-1'n" . 
   "Content-Transfer-Encoding: 7bitnn" . 
   $message . "nn"; 

   /* Encoding file data */
   $data = chunk_split(base64_encode($data)); 

   /* Adding attchment-file to message*/
   $message .= $mimeBoundary."n" . 
   "Content-Type: ".$fileType.";n" . 
   " name=".$fileName."n" . 
   "Content-Transfer-Encoding: base64nn" . 
   $data . "nn" .$mimeBoundary."n"; 
 } 

 $flgchk = mail ("$to", "$subject", "$message", "$headers"); 

 if($flgchk){
   echo "A email has been sent to: $to";
  }
 else{
   echo "Error in Email sending";
 }
 ?>