PHP邮件在包含标头时发送失败


PHP mail failed to send when included header

所以我试图用附件邮寄表格,但当我在邮件中包含$header时,它无法发送:mail("mohd.gadiwala@techmatters.com", $subject, $message, $headers)当我从我的代码邮件中删除$header时,正在发送邮件,但发送的图像附件是文本数据,而不是实际的附件图像png形式。所有东西都被塞满了,没有边界。我尝试了从这个网站上执行代码:点击我
我在下面的代码中做错了什么?:

<?php
 if(isset($_POST['submit']) && $_POST['submit']=='Submit')
{
$to="siva.garre@livait.net";
$subject="File sent by ".$_POST['name'];
// get the sender's name and email address
 // we'll just plug them a variable to be used later
 $from = stripslashes($_POST['name'])."<".stripslashes($_POST['email']).">";
$name = $_POST['name'];
 $email_address = $_POST['email'];
 $message = $_POST['comment'];
// generate a random string to be used as the boundary marker
 $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 if($_FILES['filename']['tmp_name'] != ''){
 // store the file information to variables for easier access
 $tmp_name = $_FILES['filename']['tmp_name'];
 $type = $_FILES['filename']['type'];
 $file_name = $_FILES['filename']['name'];
 $size = $_FILES['filename']['size'];
 }
 // here we'll hard code a text message
 // again, in reality, you'll normally get this from the form submission
if($tmp_name != ''){
 $message = "nn Name: $name nn Email: $email_address nnMessage: nn $message nnHere is your file: $file_name";
 }
 else{
 $message = "nn Name: $name nn Email: $email_address nnMessage: nn $message.";
 }
 // if the upload succeded, the file will exist
 if($tmp_name != ''){
 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)){
// open the file for a binary read
 $file = fopen($tmp_name,'rb');
// read the file content into a variable
 $data = fread($file,filesize($tmp_name));
// close the file
 fclose($file);
// now we encode it and split it into acceptable length lines
 $data = chunk_split(base64_encode($data));
 }
 }
 }
// now we'll build the message headers
  $headers = "From: $fromrn";
if( $tmp_name != '' ){
$headers .= "MIME-Version: 1.0rn" .
"Content-Type: multipart/mixed;rn" ;
// next, we'll build the message body
 // note that we insert two dashes in front of the
 // MIME boundary when we use it
$message = "This is a multi-part message in MIME format.nn" .
 "Content-Type:text/plain;charset=iso-8859-1" .
 "Content-Transfer-Encoding: 7bitnn" .
 $message . "nn";
// 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 .=
 "Content-Type: ".$type."" .
 " name=".$file_name."n" .
 //"Content-Disposition: attachment;n" .
 //" filename="{$fileatt_name}"n" .
 "Content-Transfer-Encoding: base64nn" .
 $data . "nn" ;
 }
 // now we just send the message
 if (mail("mohd.gadiwala@techmatters.com", $subject, $message, $headers))
 echo "<div class='msg msg-ok'><p><strong>Message Sent</strong></p></div><br><br>";
 else
 echo "<div class='msg msg-ok'><p><strong>Message sending failed</strong></p></div><br><br>";
 }
 ?>
<html>
<body>
<form id="comment" action="atta.php" method="post" enctype="multipart/form-data">
<label>Name <span></span></label>
<input type="text" name="name" id="name">
<label>Email <span></span></label>
<input type="text" name="email" id="email">
<label>Comment <span></span></label>
<input type="text" name="comment" id="email">
<label>Upload file <span></span></label>
<input type="file" name="filename" id="file">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
$headers .= "MIME-Version: 1.0'r'n"; //use 'r'n 

希望有帮助:)