[2] [[MIME]] の [DFN[[CODE(MIME)[multipart/alternative]] [[媒体型]]]]は、
同じ情報の複数の[Q[代替]]となる[[表現]]を1つにまとめるために使うことができる書式です。

[3] 例えば、[[電子メイル]]の[[宛先]]が正しくないことを伝える [[DSN]]
の[[メッセージ]]で、人間向けの説明文の[[日本語]]版と[[英語]]版を同時に含めることができます。
あるいは[[アニメーション]]画像を提供する時に、
[[MNG]] 版と [[GIF]] 版を提供し、受け取った側で使える方を使ってもらうということができます。

[4] 
本質的には同じものを表す複数の[[表現]]を提供する技術としては [[HTTP]]
の[[内容折衝]]が有名ですが、 [[HTTP]] は提供側 ([[鯖]])
と利用側 ([[利用者エージェント]]) が直接通信するので、
[[利用者エージェント]]が用意した情報を使って[[鯖]]側で折衝
([[表現]]の選択) を行います。それに対して[[送信者]]と[[受信者]]が時間的・空間的に離れている[[電子メイル]]では、
[[送信者]]が用意した選択肢を 
[CODE(MIME)[[[multipart/alternative]]]] 形式に詰め込み、
[[受信者]]がその中から好きなものを選ぶという方法を採っています。

* 仕様書

[REFS[
- [5] '''[CITE@en[RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types]] ([TIME[2015-03-22 13:14:46 +09:00]] 版) <http://tools.ietf.org/html/rfc2046#section-5.1.4>'''
- [26] [CITE@en[RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types]] ([TIME[2015-03-22 13:14:46 +09:00]] 版) <http://tools.ietf.org/html/rfc2046#section-5.2.3.7>
- [36] [CITE@en[[[RFC 1766]] - Tags for the Identification of Languages]], [TIME[2021-04-11T08:15:48.000Z]], [TIME[2021-04-22T08:55:41.728Z]] <https://tools.ietf.org/html/rfc1766#section-4>
]REFS]

* 意味

[11] 各[[本体部分]]が同じ情報の異なる版であることを表します [SRC[>>5]]。

[20] [[本体部分]]の順序は、元の内容を忠実に表していないものから順に、
最後を最も好ましいものとしなければなりません。 [SRC[>>5]]

;; [23] 非 [[MIME]] [[利用者エージェント]]で表示する時はこの順序が好ましかろう
[SRC[>>5]] という配慮のようです。

* 構文

[6] [CODE(MIME)[multipart/alternative]] の構文は、
他の [CODE(MIME)[[[multipart/[VAR[*]]]]]] [[媒体型]]と共通です [SRC[>>5]]。
[[本体]]の部分に複数の[[本体部分]]を [CODE(MIME)[[[boundary]]]]
[[引数]]で区切って並べます。

[8] 含まれる各[[本体部分]]の順序は、
後の方程[Q[オリジナル]]に近いものにします。例えば元々の [[PDF]]
版、それから作成した [[HTML]] 版、[RUBY[[[平文]]] @en[プレイン・テキスト]]版と
3つあれば、[[平文]]、 [[HTML]]、 [[PDF]] の順で並べます。

このような順序にするのは、 [[MIME]] に対応していない[[利用者エージェント]]で見た時に
(つまり[[平文]]として見た時に) できるだけ[Q[ごみ]]が[[利用者]]に見えないようにと配慮されたことによります。

[39] 
[[AMP Email]]
は、
最後の[[本体部分]]だけを[[レンダリング]]する
[[MUA]]
が存在することを理由に、
[CODE[text/x-amp-html]]
より後に
[CODE[text/html]]
を置くことを推奨 (recommend) しています。
[SRC[>>38]]

[40] 
そのように表示する [[MUA]] は仕様違反です (仕様は適合性の規定が曖昧ですが、
少なくても意図した挙動でないことは明らかです)。
[[AMP]] の推奨する動作は明確な仕様違反です。


[41] 
[[AMP]] の推奨が[[意図的違反]]なのか、 [[MIME]] 
仕様への無理解に由来するものなのかは不明です。

[42] 
表示の仕様違反の [[MUA]] が多数なら、現行 [[MIME]] 仕様が実効性を失っていることになりますから、
改正されるべきですし、[[意図的違反]]もやむなしでしょう。
そうでもないのなら、
仕様違反を推奨するのはいただけません。


[REFS[
- [38] [CITE@en[Structure and rendering of AMP emails - amp.dev]], [TIME[2022-06-29T13:51:46.000Z]] <https://amp.dev/documentation/guides-and-tutorials/learn/email-spec/amp-email-structure/?format=email>
]REFS]

* 引数

[7]
,引数名	,引数値	,既定値	,説明	,状態	,出典
,[CODE(MIME)[[[boundary]]]]	,	,(必須)	,境界文字列	,[[IETF]] [[原案標準]]	,[[MIME]]
,[CODE(MIME)[[[differences]]]]	,	,	,違う点	,廃止 ([[IETF]] [[提案標準]])	,[[RFC 1766]]

** [CODE[differences]] 引数

[37] 
[DFN[[CODE[differences]]]] [[引数]]は
[[RFC 1766]] が定義していました [SRC[>>36]]。 multipart/alternative
で選択肢になっている各部分の差異について指定します。

RFC 1766 では値として、 one or more 個の "Content-Type" / "Content-Language"
/ iana-token が指定できるとされています。値は RFC
として出版されている頭領域名でなければなりません。
従って、値の大文字・小文字は区別しないのでしょう(明記されては
いません)。

「more」個の値を指定する例が示されていないので、どうするのか
わかりませんが、読点 (comma) 区切りと解するのが適当でしょうか。

RFC 1766 を廃止する RFC 3282 では、 differences
パラメーターは実際には使われなかったとして、
differences パラメーターも廃止しています。

値は IANA に登録することになってますが、それっぽい登録簿は
無いみたいです。

[REFS[
- [35] [CITE@en[[[RFC 3282]] - Content Language Headers]], [TIME[2021-04-11T09:58:17.000Z]], [TIME[2021-04-22T08:54:57.202Z]] <https://tools.ietf.org/html/rfc3282#page-7>
]REFS]

* 処理

[19] 実装は、各[[本体部分]]の内容が交換可能なものと認識するべきです。
環境に応じて、場合によっては[[利用者]]の指示により、
「最適」なものを選ぶべきです。 [SRC[>>5]]

[21] 一般に受信者の環境が対応する最後の[[本体部分]]が最善の選択です [SRC[>>5]]。 

[22] 選択肢のいずれかが [CODE(MIME)@en[[[multipart/*]]]] の場合で、
認識でいない[[本体部分]]が含まれている場合は、
それを表示することにしても構いませんし、より前の[[本体部分]]を表示しても構いませんし、
両方でも構いません。 [SRC[>>5]]

[24] [[利用者]]に選択肢を提示しても構いません [SRC[>>5]]。

[25] いずれにせよ、[[利用者]]に同じデータの複数の版を同時に提示しないことが重要です [SRC[>>5]]。

[44] 
具体的にどのように「最善」を選択するべきかは決められていません。

[45] 
仕様上は [CODE[Content-Language:]] の違いにも使えますし、
その他の[[ヘッダー]]で表される違いにも使えますが、
実際にそのような利用例があるのかは不明です。

[46] 
実際にはほとんどの場合には定型化したいくつかの選択肢から1つを選ぶ形で使われていて
(>>47)、
実装もその定型に従った選択しかしません。



* 用法

[47] いくつかの使われ方があります。

[28] [[HTMLメール]]では、 [CODE(MIME)@en[[[text/plain]]]] と
[CODE(MIME)@en[[[text/html]]]] の[[本体部分]]が含まれるのが基本です。

[29] [[HTMLメール]]の [[HTML]] 版に[[画像]]が埋め込まれている場合、
[CODE(MIME)@en[[[text/plain]]]] と [CODE(MIME)@en[[[multipart/related]]]]
で、 [CODE(MIME)@en[[[multipart/related]]]] 内に [CODE(MIME)@en[[[text/html]]]]
と[[画像]]が含まれることがあります。

[30] [[HTMLメール]]に[[添付ファイル]]が含まれる場合、 [CODE(MIME)@en[[[multipart/mixed]]]]
に [CODE(MIME)@en[[[multipart/alternative]]]] と[[添付ファイル]]が[[本体部分]]として含まれるのが一般的です。

;; [[HTMLメール]]参照。

;; [31] [CODE(MIME)@en[[[multipart/alternative]]]] を使わない [CODE(MIME)@en[[[text/html]]]]
だけの[[HTMLメール]]もあります。

[33] [CODE(MIME)@en[[[multipart/alternative]]]] は、
送信者がどの[[部分]]も同等同義の内容を含めていることを前提としていますが、
受信者がそれを確認するのは困難です。 (少なくても [[HTMLメール]]では)
この点送信者を信用できず、送信者は [[HTML]] [[部分]]は丁寧に作成しても、
[[平文]]は気にも留めず機械生成した値や情報を削った内容しか含まれていなかったり、
改訂時に忘れられて古い版のままだったり、蔑ろにされているのが現実です
([[HTMLメール]]参照)。本来の想定である
「受信者の装置や[[利用者エージェント]]の能力に合わせた形式の選択」
という実装戦略はここでは成立しません。

[43] [[AMP Email]] 
は
[CODE[text/plain]],
[CODE[text/html]],
[CODE[text/x-amp-html]]
を[[本体部分]]として含める形式です。

* 歴史

[REFS[
= [DEL[[[RFC 1341]] ([[MIME]] 1[SUP[st]]) ([[IETF]] [[提案標準]]) <urn:ietf:rfc:1341>]]
= [DEL[[[RFC 1521]] ([[MIME]] 2[SUP[nd]]) ([[IETF]] [[原案標準]]) <urn:ietf:rfc:1521>]]
= [DEL[[[RFC 1766]] ([CODE(MIME)[differences]] 引数) ([[IETF]] [[提案標準]]) <urn:ietf:rfc:1766>]] ([[RFC 3282]] により[[廃止]])
= [[RFC 2046]] ([[MIME]] 3[SUP[rd]]) ([[IETF]] [[原案標準]]) <urn:ietf:rfc:2046>
-- [CSECTION@en[5.1.4.  Alternative Subtype]]
]REFS]

[FIG(quote)[
[FIGCAPTION[
[10] RFC 2046 (MIME 3[SUP[rd]]) 5.1.4.  Alternative Subtype
]FIGCAPTION]

> The "multipart/alternative" type is syntactically identical to
"multipart/mixed", but the semantics are different.  In particular,
each of the body parts is an "alternative" version of the same
information.

[DFN[[CODE(MIME)[multipart/alternative]]]] 型は構文的には
[CODE(MIME)[[[multipart/mixed]]]] と同じですが、
意味的には異なります。特に、
各[[本体部分]]は同じ情報の[Q[代替]]版とします。

> Systems should recognize that the content of the various parts are
interchangeable.  Systems should choose the "best" type based on the
local environment and references, in some cases even through user
interaction.  As with "multipart/mixed", the order of body parts is
significant.  In this case, the alternatives appear in an order of
increasing faithfulness to the original content.  In general, the
best choice is the LAST part of a type supported by the recipient
system's local environment.

システムは各[[部分]]の[[内容]]は交換可能であることを認識するべきです。
システムは局所環境や場合によっては[[利用者]]との[[対話]]にも基づき、
[Q[最善]]な型を選ぶべきです。 [CODE(MIME)[[[multipart/mixed]]]]
と同じように[[本体部分]]の順序は意味を持ちます。この場合、
各選択肢は元の内容への忠実度の順に並べます。
通常、受信システムの局所環境で対応している型の'''最後'''の[[部分]]を選ぶのが最良です。

> "Multipart/alternative" may be used, for example, to send a message
in a fancy text format in such a way that it can easily be displayed anywhere:

[CODE(MIME)[multipart/alternative]] は例えば装飾付きの文章書式で[[メッセージ]]を送る時にどこでも簡単に表示できるようにするために使うことができます。

>
[PRE(822 example code)[
From: Nathaniel Borenstein <nsb@bellcore.com>
To: Ned Freed <ned@innosoft.com>
Date: Mon, 22 Mar 1993 09:41:09 -0800 (PST)
Subject: Formatted text mail
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=boundary42
--boundary42
Content-Type: text/plain; charset=us-ascii
  ... plain text version of message goes here ...
--boundary42
Content-Type: text/enriched
  ... RFC 1896 text/enriched version of same message
      goes here ...
--boundary42
Content-Type: application/x-whatever
  ... fanciest version of same message goes here ...
--boundary42--
]PRE]

[PRE[
   In this example, users whose mail systems understood the
   "application/x-whatever" format would see only the fancy version,
   while other users would see only the enriched or plain text version,
   depending on the capabilities of their system.
]PRE]

[PRE[
   In general, user agents that compose "multipart/alternative" entities
   must place the body parts in increasing order of preference, that is,
   with the preferred format last.  For fancy text, the sending user
   agent should put the plainest format first and the richest format
   last.  Receiving user agents should pick and display the last format
   they are capable of displaying.  In the case where one of the
   alternatives is itself of type "multipart" and contains unrecognized
   sub-parts, the user agent may choose either to show that alternative,
   an earlier alternative, or both.
]PRE]

[PRE[
   NOTE: From an implementor's perspective, it might seem more sensible
   to reverse this ordering, and have the plainest alternative last.
   However, placing the plainest alternative first is the friendliest
   possible option when "multipart/alternative" entities are viewed
   using a non-MIME-conformant viewer.  While this approach does impose
   some burden on conformant MIME viewers, interoperability with older
   mail readers was deemed to be more important in this case.
]PRE]

[PRE[
   It may be the case that some user agents, if they can recognize more
   than one of the formats, will prefer to offer the user the choice of
   which format to view.  This makes sense, for example, if a message
   includes both a nicely- formatted image version and an easily-edited
   text version.  What is most critical, however, is that the user not
   automatically be shown multiple versions of the same data.  Either
   the user should be shown the last recognized version or should be
   given the choice.
]PRE]

[PRE[
   THE SEMANTICS OF CONTENT-ID IN MULTIPART/ALTERNATIVE:  Each part of a
   "multipart/alternative" entity represents the same data, but the
   mappings between the two are not necessarily without information
   loss.  For example, information is lost when translating ODA to
   PostScript or plain text.  It is recommended that each part should
   have a different Content-ID value in the case where the information
   content of the two parts is not identical.  And when the information
   content is identical -- for example, where several parts of type
   "message/external-body" specify alternate ways to access the
   identical data -- the same Content-ID field value should be used, to
   optimize any caching mechanisms that might be present on the
   recipient's end.  However, the Content-ID values used by the parts
   should NOT be the same Content-ID value that describes the
   "multipart/alternative" as a whole, if there is any such Content-ID
   field.  That is, one Content-ID value will refer to the
   "multipart/alternative" entity, while one or more other Content-ID
   values will refer to the parts inside it.
]PRE]
]FIG]

** RFC 1766

[REFS[
- [14] [CITE@en[RFC 1766 - Tags for the Identification of Languages]]
<http://tools.ietf.org/html/rfc1766#section-4>
]REFS]

* 関連

[9] [CODE(MIME)[[[Content-ID]]:]] 
欄は[[実体]]の[[一意]]な識別子を与えるもので、
通常は[[実体]]ごとに異なるものとしなければなりませんが、
[CODE(MIME)[[[multipart/alternative]]]] 内の各[[本体部分]]の
[CODE(MIME)[Content-ID]] は同じものでも構わないとされています。

;; [CODE(MIME)@en[[[Content-ID:]]]] を参照。

[27] [CODE(MIME)@en[[[message/external-body]]]] と併用することで、
代替版をすべて含めて送らずとも、必要なものを[[受信者]]に取得させることができます [SRC[>>26]]。

* メモ

[1]
引数 [CODE(MIME)[[[differences]]]] 
と似たようなのが [CODE(HTTP)[[[URI:]]]] 欄にもあったっけ。

[12] [CITE@en[RFC 5621 - Message Body Handling in the Session Initiation Protocol (SIP)]]
([TIME[2009-09-12 07:37:40 +09:00]] 版)
<http://tools.ietf.org/html/rfc5621#section-4>

[13] [CITE@en[RFC 5621 - Message Body Handling in the Session Initiation Protocol (SIP)]]
([TIME[2009-09-12 07:37:40 +09:00]] 版)
<http://tools.ietf.org/html/rfc5621#section-6>

[15] [CITE@en[RFC 5621 - Message Body Handling in the Session Initiation Protocol (SIP)]]
( ([TIME[2014-09-14 07:55:49 +09:00]] 版))
<http://tools.ietf.org/html/rfc5621#section-4.1>

[16] [CITE@en[RFC 5621 - Message Body Handling in the Session Initiation Protocol (SIP)]]
( ([TIME[2014-09-14 07:55:49 +09:00]] 版))
<http://tools.ietf.org/html/rfc5621#section-8.2>

[17] [CITE@en[RFC 3459 - Critical Content Multi-purpose Internet Mail Extensions (MIME) Parameter]]
( ([TIME[2014-09-28 14:54:36 +09:00]] 版))
<http://tools.ietf.org/html/rfc3459#section-12.1>

[18] [CITE@en[draft-jennings-sipping-multipart-02 - Session Initiation Protocol (SIP) Offer/Answer with Multipart Alternative]]
( ([TIME[2014-10-17 16:44:51 +09:00]] 版))
<https://tools.ietf.org/html/draft-jennings-sipping-multipart-02>

[32] [CITE@en[RFC 5621 - Message Body Handling in the Session Initiation Protocol (SIP)]]
([TIME[2015-06-21 16:17:31 +09:00]] 版)
<https://tools.ietf.org/html/rfc5621>

[34] [CITE@en[RFC 2483 - URI Resolution Services Necessary for URN Resolution]]
([TIME[2017-04-16 16:20:23 +09:00]])
<https://tools.ietf.org/html/rfc2483#section-4.4>


[48] 
[CITE[Welcome to Netscape Navigator 3.0]], [TIME[2024-08-16T11:50:24.000Z]], [TIME[2002-06-30T20:13:53.276Z]] <https://web.archive.org/web/20020630200918/http://wp.netscape.com/eng/mozilla/3.0/relnotes/windows-3.0.html#MIME>
