通过php表单将文件附件发送到电子邮件

时间:2014-08-10 07:49:24

标签: php html forms

我正在尝试使用php发送带有html的文件上传。提交表单后我一直收到错误。我似乎可以弄清楚这里的问题是什么。我在代码中做错了什么吗?或者我错过了任何PHP库?

Html:     

    <input type="radio" name="frame" id="rectanglerounded" value="rectanglerounded"  />
    <label for="rectanglerounded"><img src="img/custom-1.png" alt="rectanglerounded" /></label>
    <input type="radio" name="frame" id="rectangle" value="rectangle" />

    <label for="rectangle"><img src="img/custom-2.png"  alt="rectangle" /></label>

    <input type="radio" name="frame" value="oval" id="oval" />

    <label for="oval"><img src="img/custom-3.png" alt="oval" /></label>

    <img id="blah" src="img/upload.png" alt="your image" />

    <input  type="file" name="attachment" id="imgcustom">

    <h4>Name</h4>

    <input type="text"  name="name" value="" placeholder="Name*" />

     <h4>Email Address</h4>

    <input type="text"  name="email" value="" placeholder="Email*" />
    <h4>Telephone Number</h4>

    <input type="text"  name="telephone" value="" placeholder="Telephone" />
    <h4>Address</h4>

    <input type="text"  name="address_1" value="" placeholder="Address Line 1*" />

    <input type="text"  name="address_2" value="" placeholder="Address Line 2*" />

    <h4>Area Code</h4>

    <input type="text"  name="area_code" value="" placeholder="Area Code*" />

    <h4>City</h4>

    <input type="text"  name="city" value="" placeholder="City*" />

    <h4>Country</h4>

    <input type="text"  name="country" value="" placeholder="Country*" />

    <input class="btn-orange" type="submit" value="Submit"  name="action" />

</form>

php:     

$mailto = 'admin@gmail.com';
$emailSubject = 'Customer Has a inquiry';
$fromEmail = $_POST['email']; 
$fromName = $_POST['name']; 
$message = $_POST['frame'];
$address_1 = $_POST['address_1'];
$address_2 = $_POST['address_2'];
$area_code = $_POST['area_code'];
$city = $_POST['city'];
$country = $_POST['country'];

/* 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.0\n"; 
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\""; 

/* 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: base64\n\n" . 
  $data . "\n\n" . 
  "--{$mimeBoundary}--\n"; 
  } 

    $flgchk=mail("$mailto","$emailSubject","$address_1","$address_2","$area_code","$city","$country","$message","$headers"); 

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

1 个答案:

答案 0 :(得分:1)

为什么你用逗号分隔你拥有的所有文件?我不认为这个电话存在吗?

不应该是:

mail($mailto,$emailSubject,$message,$headers); 

当需要标题后的附加参数?