2014-02-23 162 views
1

我有一個HTML電子郵件由PHP發送,通過base64編碼附加pdf。除了電子郵件以純文本形式出現之外,它都可以工作,因此您可以看到所有HTML標記和base64輸出 - 顯然不太理想。必須有一些我在這裏失蹤,以確保它被讀取爲HTML &附件。HTML電子郵件混合(html&attachchement)來作爲純文本

如果有人可以幫助,那太棒了!

我的PHP:

<?php $to = $email; 

$message = 'testing...'; 

$subject = 'Health Insurance Quote Request'; 

$headers = "From: Andy - CEO Health.com.au <" . strip_tags('[email protected]') . ">\r\n"; 
$headers .= "Reply-To: Andy - CEO Health.com.au <". strip_tags('[email protected]') . ">\r\n"; 

// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$email_message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$email_message .= "--{$mime_boundary}\n"; 

//get PDF URL 
$data = chunk_split(base64_encode(file_get_contents('http://example.com/doge.pdf'))); 

$email_message .= "Content-Type: {\"application/pdf\"};\n" . " name=\"$product\"\n" . 
"Content-Disposition: attachment;\n" . " filename=\"$product\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
$email_message .= "--{$mime_boundary}\n"; 

mail($email, $subject, $email_message, $headers); 

輸出的實施例:

MIME-Version: 1.0 
Content-Type: multipart/mixed; 
boundary="==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x" 

This is a multi-part message in MIME format. 

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

testing... 

--==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x 
Content-Type: {"application/pdf"}; 
name="HeartPlus65" 
Content-Disposition: attachment; 
filename="HeartPlus65" 
cwt/SGdVm9X6LXQwvq03c+afGdAA9NlSAk1G3NyG2S5pDSY+CycXKDnuBJ5gFRupvZva0H2CXQqA 
t9M6DlWx646bhhsx8NXdde0ES4z+8FP03XNEWTPMc/lWObbIG4ET4qxQ57eCm1V8RZOOzqG6+pu7 
mYERMqHobI26KpfZYx27x8ESi65xkqOYkdbFIJ1qm2HXUEGZC0B1Jr2c6qmHeo3VULGurdIKhERP 
Q6EJqtQ6rrWPbLiqzHgOlVLrfaIU8Zxe2SUhi4QTauK9G0/qbmjb2TD0HNkakoNlLbDzCql32d4A 
khPjiE9rBQZGOvRvtxnEaGAlXimw7Vp4tIsqkjkI2JQ1upHBRgTA31CjqPNBjdM9ISRKHl9MLvc0 
LeZUH8J7GNA1R+8mJvqkR0p5f7LZEbSkt7c2fopKX79LsEe2O7nv6qzJrc1xGreCsS 

.... 
+0

嘗試使用SwiftMailer該---它更優雅地處理附件。 –

回答

-1

它可以不具有 - 在最後邊界標籤

即$ email_message的末尾= 「 - {$ mime_boundary} \ n」;

to $ email_message。=「 - {$ mime_boundary} - \ n」;

我不知道它有多重/混合多重/替代之間的差異,但這個工作對我來說

# Setup mime boundary 
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x'; 

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n"; 
$headers .= "Content-Transfer-Encoding: 7bit\n"; 

$body = "This is a multi-part message in mime format.\n\n"; 

# Add in plain text version 
$body .= "--$mime_boundary\n"; 
$body .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n"; 
$body .= "Content-Transfer-Encoding: 7bit\n\n"; 
$body .= $text_content; 
$body .= "\n\n"; 

# Add in HTML version 
$body .= "--$mime_boundary\n"; 
$body .= "Content-Type: text/html; charset=\"UTF-8\"\n"; 
$body .= "Content-Transfer-Encoding: 7bit\n\n"; 
$body .= $html_content; 
$body .= "\n\n"; 

#Attachments 
    if ($path!='' && $filename!=''){ 
    $file_size = filesize($path.$filename); 
    $handle = fopen($path.$filename, "r"); 
    $filecontent = fread($handle, $file_size); 
    fclose($handle); 
    $filecontent = chunk_split(base64_encode($filecontent)); 

    $body .= "--$mime_boundary\n"; 
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here 
    $body .= "Content-Transfer-Encoding: base64\r\n"; 
    $body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
    $body .= $filecontent."\r\n\r\n"; 
    } 
# End email 
$body .= "--$mime_boundary--\n"; 
相關問題