2024年5月8日 星期三

multipart Email 的 Content-Disposition 差異

如果我今天要寄一封 E-Mail,內文要有圖片,那整封信的內容可能如下:

Subject: This is a test mail!
From: test@example.com
Content-Type: multipart/mixed; boundary="qwertyuio"

--qwertyuio
Content-Type: text/html; name="example.html"

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title>Example</title>
	</head>
	<body>
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
		<img alt="" src="cid:28ce9652-4476-4e3c-8e9a-5d1bbae4cd05" />
	</body>
</html>

--qwertyuio
Content-Type: image/jpeg; name="example.jpg"
Content-Disposition: attachment; filename="example.jpg"
Content-ID: 28ce9652-4476-4e3c-8e9a-5d1bbae4cd05

<image content>

--qwertyuio--

關於 CID 的介紹,可參閱 w3c 的說明:Universal Resource Locators: cid scheme.,這邊就不詳述。

這封 E-Mail 的問題是,送出去的 E-Mail 的圖片除了內文顯示外,還會被列入附件清單中。

如果不想要讓其列入附件清單中,就需要修改 Content-Disposition header,改成 inline


即:

--qwertyuio
Content-Type: image/jpeg; name="example.jpg"
Content-Disposition: inline; filename="example.jpg"
Content-ID: 28ce9652-4476-4e3c-8e9a-5d1bbae4cd05

<image content>

參考資料

Universal Resource Locators: cid scheme. (n.d.). https://www.w3.org/Addressing/URL/4_1_Cid.html

Content-Disposition - HTTP | MDN. (2023, December 16). MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

沒有留言:

張貼留言