我知道如何將圖像嵌入到電子郵件中作爲附件,但這不是我想要做的。我想要做的是在圖像中嵌入一個圖像,並在電子郵件本體中使用它。是否有可能做到這一點,或者可能引用附件以某種方式在消息中使用?使用PHP嵌入圖像以用於電子郵件消息?
我應該甚至擔心這個嗎?只需通過鏈接到我的網絡上的圖像加載它們會更有益處?我一直在想,這會讓我的服務器承受一些負擔,因此無需在每個打開的電子郵件中直接從我的站點下載,從而減少一點帶寬。
注:我不找任何電子郵件框架或圖書館等,只是一個簡單的方法來完成這項任務。
我知道如何將圖像嵌入到電子郵件中作爲附件,但這不是我想要做的。我想要做的是在圖像中嵌入一個圖像,並在電子郵件本體中使用它。是否有可能做到這一點,或者可能引用附件以某種方式在消息中使用?使用PHP嵌入圖像以用於電子郵件消息?
我應該甚至擔心這個嗎?只需通過鏈接到我的網絡上的圖像加載它們會更有益處?我一直在想,這會讓我的服務器承受一些負擔,因此無需在每個打開的電子郵件中直接從我的站點下載,從而減少一點帶寬。
注:我不找任何電子郵件框架或圖書館等,只是一個簡單的方法來完成這項任務。
人會附和告訴你使用SwiftMailer或其他一些庫。但並不難。這真的很挑剔,但並不難。以下是我使用的功能。我對它進行了匿名處理,希望沒有破壞任何東西。在HTML郵件(這是我從另一個函數獲得),鏈接到你附加的圖像是這樣的:
$output .= '<p><img src="cid:' . $linkID . '" alt="graph" /></p>';
凡$linkID
是在下面的函數中使用相同的值。
public function sendEmail($emailAddress)
{
$random_hash = md5(date('r', time()));
$htmlString = $this->getHTML($random_hash);
$message = "--mixed-$random_hash\n";
$message .= 'Content-Type: multipart/alternative; boundary="alt-' . $random_hash . '"' . "\n\n";
$message .= 'MIME-Version: 1.0' . "\n";
$message .= '--alt-' . $random_hash . "\n";
$message .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
// text message
$message .= strip_tags($htmlString) . "\n\n";
$message .= '--alt-' . $random_hash . "\n";
$message .= 'Content-Type: text/html; charset="iso-8859-1"' . "\n";
// $message .= 'MIME-Version: 1.0' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
// html message
$message .= $htmlString . "\n\n";
$message .= '--alt-' . $random_hash . '--' . "\n";
// graph image
if($this->selectedGraph != 0)
{
$graphString = $this->getGraph();
$graphString = chunk_split(base64_encode($graphString));
$linkID = 'graph-' . $random_hash . '-image';
$message .= '--mixed-' . $random_hash . "\n";
$message .= 'MIME-Version: 1.0' . "\n";
$message .= 'Content-Transfer-Encoding: base64' . "\n";
$message .= 'Content-ID: ' . $linkID . "\n";
$message .= 'Content-Type: image/gif; name="graph.gif"' . "\n";
$message .= 'Content-Disposition: attachment' . "\n\n";
$message .= $graphString;
$message .= '--mixed-' . $random_hash . '--' . "\n";
}
else
{
$message .= '--mixed-' . $random_hash . '--' . "\n";
}
$headers = 'From: ' . $this->from. "\r\nReply-To: " . $this->replyto;
$headers .= "\r\nContent-Type: multipart/related; boundary=\"mixed-" . $random_hash . "\"\r\nMIME-Version: 1.0";
$flags = '-f ' . BOUNCED_EMAIL_ADDRESS;
return mail($emailAddress, $this->subject, $message, $headers, $flags);
}
是否值得去做是另一個問題。如果您附加圖片,則使用帶寬將信息發送出去。也許最好是一次性使用帶寬而不是分散使用帶寬?也許不會。
我沒有使用附加圖像,直到我必須向每個收件人發送不同的圖像。我不打算在我們的服務器上存儲和管理所有這些圖像(並嘗試唯一命名它們)。
我認爲要做到這一點的唯一方法是通過URL來引用一個像這樣的:
<img src="http://www.youwebsite.com/yourimage.jpg" alt="This is a description" />
Content-Type: multipart/related;
type="text/html";
boundary="Boundary1"
--Boundary1
Content-Type: multipart/alternative;
boundary="Boundary2"
--Boundary2
Content-Type: text/html; charset = "utf-8"
Content-Transfer-Encoding: 8bit
<img src="cid:content_id_from_attachment">
--Boundary2--
--Boundary1
Content-Type: image/jpeg; name="somefilename.jpg"
Content-Transfer-Encoding: base64
Content-ID: <content_id_from_attachment>
Content-Disposition: inline; filename="somefilename.jpg"
//binary data
--Boundary1--
邊界,字符集,Content-Transfer-Encoding當然都是可以選擇的。該PHPMailer的包可以自動爲你做到這一點:http://www.ustrem.org/en/article-send-mail-using-phpmailer-en/
是'$這個 - > getGraph()'只是加載圖像的內容,比如'的file_get_contents('會)? – animuson 2010-06-22 22:54:36
在我的情況下,getGraph()實際上使用圖表庫創建圖像,但是,這是同一個想法。 – 2010-06-22 22:58:10
在郵件中發送圖像而不是作爲鏈接的原因可能是某些郵件客戶端不會在遠程服務器上打開圖像,因爲這可能會揭示用戶已打開該郵件。或者至少郵件客戶端會詢問用戶是否會遠程查看圖像。 – 2016-07-16 15:03:00