2012-01-14 147 views
-2

重複:如何在HTML電子郵件中嵌入圖像?

使用PHP

How to embed images in email

How to embed images in html email

Embed images for use in email message using PHP?

我送HTML電子郵件。我想在HTML中使用嵌入式圖像。可能嗎?我嘗試了很多不同的方法,但都沒有工作。有人能幫我嗎?

感謝

+0

我想這一個http://www.phpeveryday.com/articles/PHP-Email-Using -Embedded-Images-in-HTML-Email-P113.html – 2012-01-14 12:42:19

+0

@BenSwinburne:無法發送郵件 – 2012-01-14 12:44:12

+2

@phpcochin:請勿自行編寫所有代碼。獲取一些處理構建多部分/ MIME電子郵件並支持文件附件的PHP類。它會讓你的生活變得更容易,可能會節省你的工作時間。 – ThiefMaster 2012-01-14 12:48:44

回答

3

您需要在您的映像所在 例如提供對整個URL:

<img src='http://www.mydomain.com/imagefolder/image.jpg' alt='my-image' width='' height=''> 
+0

正是我做的方式,除了1件事。我總是在我的所有圖像上使用style =「display:block」來阻止gmail的bug(在一些圖像下面添加空格)。考慮將其添加到您的答案? :) – Undefined 2012-01-16 09:37:35

+0

無論你想寫的CSS把他們作爲內聯的CSS。多數民衆贊成在 – iThink 2012-01-16 21:29:40

+0

這篇文章是嵌入一個圖像,而不是從互聯網上訪問它 – 2015-08-08 12:20:31

2

這是不是真的微不足道,但與一對夫婦嘗試可行的。

首先,瞭解如何構建多部分電子郵件,並附上正確的圖像。如果你不能附加圖片,他們顯然不會在電子郵件中。確保將類型設置爲multipart/related

其次,瞭解如何設置cid引用,特別是附件的Content-ID標頭。

第三,把它們粘在一起。

在每一個步驟,請執行下列操作:

  • 的結果
  • 發送的電子郵件給自己,並把它比作你收到什麼
  • 比較它一個工作示例電子郵件
2

我發現通過電子郵件發送圖像的最佳方式是使用base64對其進行編碼。

有關於這個鏈接和教程負荷,但這裏是這個代碼笨應該足夠了:

/* Load email library and file helper */ 
$this->load->library('email'); 
$this->load->helper('file'); 

$this->email->from('[email protected]', 'Who Ever'); // Who the email is from 
$this->email->to('[email protected]'); // Who the email is to 

$image = "path/to/image"; // image path 

$fileExt = get_mime_by_extension($image); // <- what the file helper is used for (to get the mime type) 

$this->email->message('<img src="data:'.$fileExt.';base64,'.base64_encode(file_get_contents($image)).'" alt="Test Image" />'); // Get the content of the file, and base64 encode it 

if(! $this->email->send()) { 
    // Error message here 
} else { 
    // Message sent 
} 
相關問題