2011-11-30 57 views
1

我有一個Thunderbird的小問題。我試圖從一個HTML版本和一個純文本版本和附件發送一封郵件。郵件在Yahoo,Gmail和Roundcube中正確顯示,但不在Thunderbird中顯示。我希望任何人都能看到問題所在。這是生成我的郵件的腳本。 $ HTML = HTML內容和$純=純文本內容雷鳥沒有正確顯示郵件(PHP郵件())

function preparehtmlmail($html, $plain) { 

preg_match_all('~<img.*?src=.([\/.a-z0-9:_-]+).*?>~si',$html,$matches); 
$i = 0; 
$paths = array(); 

foreach ($matches[1] as $img) { 
$img_old = $img; 

if(strpos($img, "http://") == false) { 
    $uri = parse_url($img); 
    $paths[$i]['path'] = $_SERVER['DOCUMENT_ROOT'].$uri['path']; 
    $content_id = md5($img); 
    $html = str_replace($img_old,'cid:'.$content_id,$html); 
    $paths[$i++]['cid'] = $content_id; 
} 
} 

$boundary = "--".md5(uniqid(time())); 
$headers .= "MIME-Version: 1.0\n"; 
$headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\n"; 
$headers .= "From: [email protected]\r\n"; 
$multipart = ''; 
$multipart .= "--$boundary\n"; 
$kod = 'utf-8'; 
$multipart .= "Content-Type: text/plain; charset=$kod\n"; 
$multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n"; 
$multipart .= "$plain\n\n"; 
$multipart .= "--$boundary\n"; 
$kod = 'utf-8'; 
$multipart .= "Content-Type: text/html; charset=$kod\n"; 
$multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n"; 
$multipart .= "$html\n\n"; 

foreach ($paths as $path) { 
if(file_exists($path['path'])) 
    $fp = fopen($path['path'],"r"); 
    if (!$fp) { 
    return false; 
    } 

$imagetype = substr(strrchr($path['path'], '.'),1); 
$file = fread($fp, filesize($path['path'])); 
fclose($fp); 

$message_part = ""; 

switch ($imagetype) { 
    case 'png': 
    case 'PNG': 
     $message_part .= "Content-Type: image/png"; 
     break; 
    case 'jpg': 
    case 'jpeg': 
    case 'JPG': 
    case 'JPEG': 
     $message_part .= "Content-Type: image/jpeg"; 
     break; 
    case 'gif': 
    case 'GIF': 
     $message_part .= "Content-Type: image/gif"; 
     break; 
} 

$message_part .= "; file_name = \"$path\"\n"; 
$message_part .= 'Content-ID: <'.$path['cid'].">\n"; 
$message_part .= "Content-Transfer-Encoding: base64\n"; 
$message_part .= "Content-Disposition: inline; filename = \"mail_logo.jpg\"\n\n"; 
$message_part .= chunk_split(base64_encode($file))."\n"; 
$multipart .= "--$boundary\n".$message_part."\n"; 

    } 

    $multipart .= "--$boundary--\n"; 
    return array('multipart' => $multipart, 'headers' => $headers); 

}

+0

我只得到顯示的圖像,但沒有文字。我將發佈php代碼。 – BebliucGeorge

+0

你確實應該使用[PHPMailer](http://phpmailer.worxware.com)或[Swiftmailer](http://swiftmailer.org)來生成MIME消息。它們使用起來要容易得多,並且可以將整個腳本降低到大約10行代碼(不包括電子郵件本身的內容)。 –

回答

2

您應該使用多MIME部分正確撰寫郵件。一個用於text/html版本的多部分,另一個用於HTML和圖像的多部分。

對於HTML和文本版本,您應該使用multpart/alternative,對於HTML和圖像,您應該使用multipart/related。您的電子郵件應該喜歡這樣(爲便於閱讀,我縮進):

Content-Type:multipart/alternative; boundary ---01 
    Content-Type:text/plain; boundary ---02 
    Your text version content 
    ---02 
    Content-Type:multipart/related; boundary ---03 
    Content-Type:text/html; boundary ---04 
     Your HTML version content 
    ---04 
    Content-Type: image/jpeg; boundary ---05 
     Your image content 
    ---05 
    --- 03 
---01 

您可以使用PEAR::Mail建立你的郵件

+0

不錯的答案:)還有其他一些庫,比如PHPMailer和Swift Mailer來命名一對夫婦 – Sam

相關問題