表格邮寄附件

时间:2014-01-09 14:41:08

标签: php forms attachment

我正在尝试邮寄带附件的图片(图片)。 但我无法让它发挥作用。 我想我已经尝试了几乎所有的东西,但是要做的就是让它工作的一件事:) 并搜索所有谷歌和youtube。

我得到了图片,但没有任何用户名或城市。

请有人帮助我。  不知道我做错了什么或错过了什么?

<?php
if(isset($_POST['terms'])){
  $email_to = "mail@mail.com";
  $email_subject = "Subject";
  $user = $_POST['user']; // required
  $city = $_POST['city']; // required

  /*********Creating Uniqid Session*******/

  $txtSid = md5(uniqid(time()));

  $headers = "";
  $headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormEmail"].">\nReply-To: ".$_POST["fieldFormEmail"]."";

  $headers .= "MIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
  $headers .= "This is a multi-part message in MIME format.\n";

  $headers .= "--".$txtSid."\n";
  $headers .= "Content-type: text/html; charset=utf-8\n";
  $headers .= "Content-Transfer-Encoding: 7bit\n\n";
  $headers .= $email_message."\n\n";

  /***********Email Attachment************/
  if($_FILES["attachment"]["name"] != "")
  {
    $txtFilesName = $_FILES["attachment"]["name"];
    $txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"]))); 
    $headers .= "--".$txtSid."\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n"; 
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
    $headers .= $txtContent."\n\n";
  }


  $email_message = "Message.\n\n";

  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }

  $email_message .= "User: ".clean_string($user)."\n";
  $email_message .= "City: ".clean_string($city)."\n";

  // Send
  @mail($email_to, $email_subject, $email_message, $headers); 
}else{
  echo "You need to accept our terms";
}
?>

1 个答案:

答案 0 :(得分:0)

看看PHPMailer,它比使用自己的html / headers / etc更容易使用。