2010-12-13 75 views
1

我想發送一個Word文件作爲使用PHP的附件,並且正在發送電子郵件,但是沒有正文並且發送了非匿名附件。用PHP發送的電子郵件,沒有任何內容,也沒有附件

下面是代碼:

$website=getwebsite($id); 
    //define the receiver of the email 
$to = $email; 
//define the subject of the email 
$subject = "order- ".getorderid($id); 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]$website\r\nReply-To: [email protected]$website"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string, 
//encode it with MIME base64, 
//and split it into smaller chunks 
$attachment = chunk_split(base64_encode(file_get_contents($file))); 
//define the body of the message. 
//echo $file; 
ob_start(); //Turn on output buffering 
?> 


--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<p>Dear Customer,</p> 

<p>Please find attached your requested paper.</p> 
<p> 

Thanks & Regards</p> 

<?php echo $website?> Papers Team 

</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 



--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/vnd.ms-word; name="plagerism.doc"; 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

誰能解釋什麼,我做錯了什麼?

回答

1

如果您查看電子郵件的發送源你會看到你正在使用的邊界哈希已被破壞。這裏有一個例子:

--PHP-alt-add441e2865420b692613d7fContent-Type: text/plain; charset="iso-8859-1" 

你需要確保邊界散列是在自己的行。

那麼,你有這樣的:

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

無論是在代碼中添加一個硬換行是這樣的:

--PHP-alt-<?php echo $random_hash; ?> 

Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

或將其添加到打印出的邊界散,像這樣的代碼:

--PHP-alt-<?php echo $random_hash."\n" ?>