2011-08-30 17 views
0

我在我的網頁有這個問題,我需要通過電子郵件發送文件給某人,我的問題是郵件到達時沒有附件和很多奇怪的字符,我離開你我的代碼:在PHP中發送帶附件的電子郵件,只顯示字符沒有附加文件

$boundary='Datos Adjuntos'; 
$boundary.=" ".md5(time()); 
//Cabeceras del email    

$headers ="From: Example <[email protected]>\r\n"; 
$headers .= "Reply-To: <[email protected]>\r\n"; 
// $headers .="MIME-Version: 1.0\r\n"; 
$headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\n"; 

$body="--". $boundary ."\n"; 
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n\n"; 

$archivo=file_get_contents($dir."/".$handle->file_dst_name); 

$archivo=chunk_split(base64_encode($archivo)); 

//Escritura del archivo adjunto 
$body .= $body .$ContenidoString. "--" .$boundary. "\n"; 
//Content-Type: application/msword; name=\"nombre_archivo\"\r\n 
$body .= "Content-Transfer-Encoding: base64\r\n"; 
$body .= "Content-Disposition: attachment; filename=\"".$handle->file_dst_name."\"\r\n\n$archivo"; 

$body = $body . "--" . $boundary ."--"; 

Correo::Enviar("OPERACION","[email protected]", $body,$headers); 

$ContentString是電子郵件的HTML,我用的是上傳類將文件上傳到服務器,然後發送它,我離開你一塊我收到的電子郵件地址是: 這是畢竟其他事情,比如電子郵件的名稱和## e內容。

--Datos Adjuntos 1c436ca78c5925e7096267f0eae3a7d3 Content-Transfer-Encoding:base64 Content-Disposition:attachment;文件名=「9cbdf187_3251_42e5_aeaa_84df343a227d_4.pdf」 JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxMTA4MTYxNTEyNDIt MDUnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjMuMTY4NC1nIFwod3d3LnBk

+1

爲什麼你想要重新發明輪子,當有很多經過試驗和測試的代碼可以成功地完成你想要實現的目標?只需使用[Zend_Mail](http://framework.zend.com/manual/en/zend.mail.html)或類似的組件,並專注於解決您的獨特問題,而不是那些一遍又一遍解決的問題。 .. – wimvds

+0

你在信息的'text/html'部分似乎沒有任何HTML ... – DaveRandom

+0

不,我沒有把HTML – apz2000

回答

5

雖然你會更好地使用一個預建的庫,如人們所說,如果由於某種原因你不能/不想,你可以試試這個:

EDITED !!

// Make a MIME boundary 
$boundary = 'Datos_Adjuntos_'.md5(time()); 

// Message headers 
$headers = "From: Example <[email protected]>\r\n"; 
$headers .= "Reply-To: <[email protected]>\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\r\n"; 

// For email clients that don't understand MIME messages 
$body = "This is a multi-part message in MIME format. If you can read this, something went wrong or your mail client doesn't understand MIME messages."; 

// HTML content 
$body .= "\r\n--$boundary\r\n"; 
$body .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\n\r\n"; 
$body .= $ContenidoString; 

// Attachment 
$body .= "\r\n--$boundary\r\n"; 
$body .= "Content-Transfer-Encoding: base64\r\n"; 
$body .= "Content-Type: application/msword\r\n"; 
$body .= "Content-Disposition: attachment; filename=\"$handle->file_dst_name\"\r\n\r\n"; 
$body .= chunk_split(base64_encode(file_get_contents("$dir/$handle->file_dst_name"))); 

// Finish the MIME message 
$body .= "\r\n--$boundary--"; 

// (Presumably) send the email 
Correo::Enviar("OPERACION","[email protected]",$body,$headers); 
+0

我試過了,它的工作,所以你可以解釋我什麼錯了,請理解y的錯誤,不要再這樣做,謝謝 – apz2000

+0

有兩件大事:你的'\ r \ n'(CRLF)順序錯了 - 有的錯過了,有的錯了,有的錯了只有'\ n'(LF)你需要一個完整的'\ r \ n'。但也是這一行:'$ body。= $ body。$ ContenidoString。 「 - 」。$ boundary。 「\ n」;'把整個身體追加到自己身上,而你想要的是'$ body。= $ ContenidoString。 「\ r \ n--」。$ boundary。 「\ r \ n」;'或'$ body = $ body。$ ContenidoString。 「\ r \ n--」。$ boundary。爲 「\ r \ n」 個;'。如果你一次把你的代碼放在一行(如上),它可以更容易地發現像這樣的東西......:-D – DaveRandom

+0

哦,非常感謝你,問題是我使用的是一個人的代碼我的工作,我可以問你一件事,什麼意思\ r和\ n,因爲我從來沒有得到它 – apz2000

1

我的建議是使用SwiftMailer http://swiftmailer.org/ 這PHP包會爲你節省大量的時間,而且讓你的生活真的是發送郵件更容易。 以下是您的代碼的樣子:

require_once 'lib/swift_required.php'; 
$message = Swift_Message::newInstance() 
->setSubject('Your subject') 
->setFrom(array('[email protected]' => 'John Doe')) 
->setTo(array('[email protected]', '[email protected]' => 'A name')) 
->setBody('Here is the message itself') 
->addPart('<q>Here is the message itself</q>', 'text/html') 
->attach(Swift_Attachment::fromPath('my-document.pdf')) 
; 

您將完成! 試試吧,我一直都在使用它

相關問題