首页 > 解决方案 > 控制使用 cfmail 生成的电子邮件的 MIME 结构

问题描述

这个问题上,我询问了电子邮件的结构应该是什么。这个问题是关于如何使用 cfmail(以及 cfmailpart、cfmailparam 等)来生成正确的结构。

所需的结构是:

multipart/mixed
  multipart/alternative
    text/plain
    text/html
  image/jpeg
  application/pdf

我目前得到的代码:

 <cfmail from='"No Reply" <noreply@example.com>' subject="Test 123" to="my_outlook_address@example.com,my_gmail_address@gmail.com">

  <!--- Some code to get a query of attachment content here... --->

  <cfloop query="qAttachments">
      <!---
          Some code to get the attachment file data here and put it in a variable named `myfile`... 

          myfile structure:
          {
              fileName: <string>,
              fileContent: <base64 encoded file content>,
              mimeType: <image/jpeg for the one, application/pdf for the other>
          }
      --->
      <cfmailparam disposition="attachment" contentID="#myfile.fileName#" content="#myfile.fileContent#" file="#myfile.fileName#" type="#myfile.mimeType#" />

  </cfloop>

  <cfmailpart type="plain">
      My plain text
  </cfmailpart>

  <cfmailpart type="html">
      <strong>My fancypants text</strong>
  </cfmailpart>
</cfmail>

但是,这会产生这种结构:

multipart/mixed
  multipart/alternative
    text/plain
    multipart/related
      text/html
      image/jpeg
      application/pdf

我试过这样的代码:

<cfmail from='"No Reply" <noreply@example.com>' subject="Test 123" to="my_outlook_address@example.com,my_gmail_address@gmail.com">

  <!--- Some code to get a query of attachment content here... --->

  <cfloop query="qAttachments">       
      <cfmailparam disposition="attachment" contentID="#myfile.fileName#" content="#myfile.fileContent#" file="#myfile.fileName#" type="#myfile.mimeType#" />

  </cfloop>

  <cfmailpart type="multipart/alternative">
    <cfmailpart type="plain">
      My plain text
    </cfmailpart>

    <cfmailpart type="html">
      <strong>My fancypants text</strong>
    </cfmailpart>
  </cfmailpart>
</cfmail>

但随后它只会进入 cfadmin 中未发送的电子邮件列表。

对于两个版本的代码,我都尝试了标签本身的type属性值:cfmail

无济于事。

如何在 ColdFusion 中实现所需的 MIME 结构?

标签: coldfusion

解决方案


我采用的方法可能并不理想,但它适用于我所针对的所有 3 个邮件客户端。我最终做的是:

  • 对于任何图像附件,我会在标签上包含一个contentID属性并包含一个带有值的cfmailparam<img src="cid:...">contentID
  • 对于所有其他附件,我省略了标签contentID上的属性cfmailparam

最终结果是所有图像都内嵌在消息正文中,所有其他文件都显示为常规文件附件。

根据我假设是 CF 团队开发人员的讨论https://tracker.adobe.com/#/view/CF-4166939我的印象是 MIME 标头结构由 ColdFusion 控制并且不是不能由 ColdFusion 开发人员直接管理。不幸的是,但至少我有一些解决方法。希望这会对某人有所帮助。


推荐阅读