2016-03-30 12 views
-1

添付ファイル付きのフォームをメールしようとしましたが、私のメールに$ headerを含めると送信に失敗します:mail("[email protected]", $subject, $message, $headers) そして、私は?:PHPヘッダが添付されているとPHPメールを送信できませんでした

下の私のコードで間違って何をやっている Click Me
:しかし、添付されて送信がテキストデータではなく、実際の添付画像PNG form.EverythingはW/O境界まで詰め込まれている画像は、私はこのウェブサイトからコードを試してみました
<?php 
if(isset($_POST['submit']) && $_POST['submit']=='Submit') 

{ 

$to="[email protected]"; 
$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("[email protected]", $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> 

答えて

1
$headers .= "MIME-Version: 1.0\r\n"; //use \r\n 

希望します。

+0

ただし、添付ファイルに名前がなく、ファイルタイプが –

+0

$ headers。= "MIME-Version:1.0 \ r \ n"になっていません。 "Content-Type:multipart/mixed; \ r \ n"; –

+0

まだ添付ファイルには名前はなく、拡張子はありませんので、どのプログラムでも開くことはできません –

関連する問題