【问题标题】:Coldfusion CFMAIL & CfMailPart Custom TagsColdfusion CFMAIL 和 CfMailPart 自定义标签
【发布时间】:2016-05-30 20:51:23
【问题描述】:

我正在为 CFMAIL 编写自定义标签。我需要以不同于标准 CFMAIL 的方式处理某些电子邮件。我正在使用以下帖子作为起点 - http://www.cfhero.com/2011/11/creating-environment-safe-wrapper-for.html

之前

<cfmail to="no@example.com" from="no@test.com" subject="This is only a test">
<cfmailparam name="Importance" value="high">
hello world</cfmail>

自定义标签之后

<cf_email to="no@example.com" from="no@test.com" subject="This is only a test">
<cf_emailparam name="Importance" value="high">
hello world</cf_email>

自定义 CFMAIL 和 CFMAILPARAM 一切正常,但我的问题是如何处理“CFMAILPART”。在新标签中使用现有的 CFMAILPART 时,CF 会抛出错误。

<cf_email to="no@example.com" from="no@test.com" subject="This is only a test">
<cf_emailparam name="Importance" value="high">
    <cfmailpart type="text">Text</cfmailpart>
    <cfmailpart type="html">HTML</cfmailpart></cf_email>

详细信息:标记必须嵌套在 cfmail 标记内。

消息:标签 cfmailpart 的上下文验证错误。

我正在提出自己的想法,但找不到任何帮助文档。任何帮助或起点将不胜感激。

提前致谢。

【问题讨论】:

  • 您使用的是什么版本的 ColdFusion?我看到代码是 2011 年的。这个人可能一直在 CF 9 上这样做
  • cfmail编写自定义标签的用例是什么?看起来你并没有真正做任何不同的事情。
  • 对自定义标签说不。
  • 我正在使用自定义标记点一些 CFMAIL 到第 3 方来处理点击率、丢弃等。我们有几十个 CFMAIL 实例,计划现在将一些实例移至第 3 方,然后最终再移一些。并非所有 CFMAIL 都会指向第 3 方,这就是我想使用 CFMAIL 和 CF_EMAIL 的原因。

标签: coldfusion cfmail


【解决方案1】:

编辑:虽然这将使它使用自定义标签工作,但 John Whish 使用 cfc 的解决方案更加灵活。

您可能需要创建 cf_emailpart 自定义标签,原因与创建 cf_emailparam 标签的原因相同。 cfemailparam 必须包含在 cfmail 标记中

<cf_email to="no@example.com" from="no@test.com" subject="This is only a test">
    <cf_emailparam name="Importance" value="high">
    <cf_emailpart type="text">Text</cf_emailpart>
    <cf_emailpart type="html">HTML</cf_emailpart>
</cf_email>

cf_emailpart

几乎与cf_emailparam 相同。最后一部分将标签与 cf_email 相关联

<cfif CompareNoCase(thisTag.hasEndtag,"YES") EQ 0 AND CompareNoCase(thisTag.executionMode,"END") EQ 0 >
    <cfexit method="exittag" />
</cfif>

<cfassociate basetag="cf_email" datacollection="emailPart">

然后,您需要将其添加到您的 cf_email 自定义标记文件中,在您输出 cfmailparam 标记的部分之后,以及 cfmail 标记内。

<cfif isDefined("thisTag.emailPart")>
    <cfloop array="#thisTag.emailPart#" index="attrCol">
        <cfmailpart attributecollection="#attrCol#" >
    </cfloop> 
 </cfif>

我还没有测试过这段代码,但我认为这是你必须走的方向。

【讨论】:

    【解决方案2】:

    它必须是自定义标签吗?在我看来,这会更简单地包含在 CFC 中。比如:

    <cfcomponent output="false">
    
        <cffunction name="init" access="public" returntype="any" output="false">
            <cfreturn this>
        </cfunction>
    
        <cffunction name="sendEmail" access="public" returntype="void" output="false">
            <cfargument name="to" required="true">
            <cfargument name="from" required="true">
            <cfargument name="subject" required="true">
            <cfargument name="importance" required="true">
            <cfargument name="htmlContent" required="true">
            <cfargument name="textContent" required="true">
    
            <!--- do a check here to see if in production something like--->
            <cfif ListFirst(CGI.SERVER_NAME, ".") neq "www">
                <!--- override the to email address or whatever here --->
                <cfset to = "foo@development.local">
            </cfif>
    
            <cfmail to="#to#" from="#from#" subject="#subject#">
                <cfmailparam name="Importance" value="#importance#">
                <cfmailpart type="text/plain">#textContent#</cfmailpart>
                <cfmailpart type="text/html">#htmlContent#</cfmailpart>
            </cfmail>
        </cffunction>
    
    </cfcomponent>
    

    然后你可以通过以下方式调用它:

    <cfset Mailer = new Path.To.Component()>
    <cfset Mailer.sendEmail(
        to="foo@somewhere.com",
        from="bar@somewhere.com",,
        subject="An Email",
        importance="high",
        htmlContent="<p>Hello!</p>,
        textContent="Hello!"
    )>
    

    Mailer 实例可以是单例(每个应用程序只需要一个实例),然后您可以执行一些操作,例如将配置设置注入其中,而不是在实际 CFC 中进行环境检测。

    【讨论】:

    • 我比我自己更喜欢这个解决方案。 cfcs 比自定义标签灵活得多。
    • 不一定。 CFC 和自定义标签只是解决不同的问题。有时一个人比另一个人更适合。在这种情况下,CFC 会更好。
    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2012-07-13
    • 2014-09-14
    • 1970-01-01
    • 2019-08-23
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多