2016-12-05 137 views
3

我在PHP發送電子郵件時事通訊。在PHP裏面我有類似PHP嵌入圖像不顯示在Web瀏覽器

<?php 
//define the receiver of the email 
set_time_limit(0); 
$to='[email protected]'; 
$subject = 'test'; 
//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='MIME-Version: 1.0' . "\r\n"; 
$headers .= "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: text/html; boundary=\"PHP-mixed-".$random_hash."\""; 

ob_start(); //Turn on output buffering 

include ("contents.php"); 

$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"; 
?> 

,這是contents.php文件

<?php 
// A few settings 
$img_file = 'tes.jpg'; 

// Read image path, convert to base64 encoding 
$imgData = base64_encode(file_get_contents($img_file)); 

// Format the image SRC: data:{mime};base64,{data}; 
$src = 'data: '.mime_content_type($img_file).';base64,'.$imgData; 

// Echo out a sample image 
echo '<img src="'.$src.'">'; 
?> 

當我打開與Android智能手機的電子郵件,正在顯示的圖像。但是,當我用網絡瀏覽器打開它時,沒有圖像顯示。我不確定它是關於Gmail出於安全原因使用的代理,還是其他內容。無論哪種方式,我想知道是否有人遇到過這種情況,如果是的話,它是如何解決的。

+0

嗯,簡單的答案是: 主機在某些服務器中的圖像,並把完整的URL到文件中圖像的src。 –

+0

我有上傳圖片到谷歌驅動器和創建共享的鏈接,還是老樣子沒有wortk。任何想法@AntonioAlexandreAlonsodeSi –

+0

一些電子郵件程序隱藏電子郵件的圖像,並顯示一個鏈接,顯示的圖像。檢查看看你是否看到這樣的鏈接。 在我的電子郵件我已經習慣了在影像和鏈接ALT描述白衣,如「請點擊此鏈接,如果你無法看到此電子郵件圖像」的說明。如果您不希望電子郵件程序將您的電子郵件置於垃圾郵件文件夾中,請注意不要使用「單擊」一詞。 –

回答

0

如何調查

Gmail可能會在其CDN終端前添加img標籤的src屬性。爲了驗證什麼是真正的src屬性中,選擇圖像元素和開發工具檢查它:

Image in Chrome Developer Tools

它可能看起來像https://ci6.googleusercontent.com/proxy/...#data: image/gif...這不是一個有效的URL。

計劃A

如果Gmail正在查找指向該文件的URL而不是數據URI,則可以使用upload the file to Amazon S3並指向它的src屬性。

B計劃

試試這個:

echo '<div style="background: url('.$src.') no-repeat left center;">'; 

相反的:

echo '<img src="'.$src.'">';