在ColdFusion中的哪里存储自动交易电子邮件模板

时间:2019-02-06 18:16:44

标签: coldfusion cfmail coldfusion-2018

我正在开发一个电子商务应用程序,该应用程序通过电子邮件自然地与用户交流有关以下交易的信息:

  • 用户注册
  • 电子邮件验证
  • 密码重置
  • 订单确认
  • 发货确认
  • 评论通知

以此类推。

此刻,我仅发送用户注册电子邮件,因此我设法将它们全部保存在名为email.cfc的单个组件中,并将其实例保存在application范围内,例如{{1} }

<cfset APPLICATION.EmailSender = New email.cfc() />只有一堆发送不同电子邮件的方法,例如:

email.cfc

我刚刚意识到,不同类型的电子邮件数量将激增,最终我可能会收到大约50种不同类型的电子邮件,这些电子邮件必须根据正在发生的事件类型而发出。将所有这些电子邮件模板保留在Application范围内的单个CFC中似乎太容易了,这可能会填满服务器内存或引起其他可伸缩性问题(无论可能是/平均)

在ColdFusion中管理发送自动交易电子邮件的更好方法是什么?一些可能的解决方案:

  • 继续将所有电子邮件保存在一个CFC中,并从其他CFC / CFM页面访问它们
  • 在CFC中保留需要模板的模板,例如<cffunction name="NewUserRegistered"> <cfmail type="html" to="#useremail#" subject="Welcome" > <h1>Thanks for registering #username#!</h1> </cfmail> </cffunction> <cffunction name="PasswordReset"> <cfmail type="html" to="#useremail#" subject="Password Reset"> <h1>Dear #username#, you requested a password reset...</h1> </cfmail> </cffunction> <cffunction name="OrderConfirmation"> <cfmail type="html" to="#useremail#" subject="Order Confirmation #orderid#"> <h1>Your order: #order_id# has been received...</h1> </cfmail> </cffunction> ,这些模板将在购物会话结束时触发“订单确认”电子邮件

1 个答案:

答案 0 :(得分:0)

编辑:添加了占位符替换的示例。

我建议使用HTML编写模板,然后将该HTML保存到您的数据库中。然后,您只需创建一个函数即可查询您的数据库,然后填充并发送您的电子邮件。那会很轻。

<cfscript>
    // Our mail function.
    public Void function genEmail ( required Numeric templateTypeID, required String userEmail, required Struct placeholder ) {

        // Created fake query of templates.
        emailTemplateQuery = queryNew(
            "templatetypeid,templatesubject,templatetext",
            "integer,varchar,varchar", 
            [ 
                { templatetypeid=1,templatesubject='Welcome',templatetext='<h1>Thanks for registering!</h1><p>[[p1]]</p><p>[[p2]]</p>' },
                { templatetypeid=2,templatesubject='Password Reset',templatetext='<h1>You requested a password reset.</h1><p>[[p1]]</p><p>[[p2]]</p>' },
                { templatetypeid=3,templatesubject='Another Email',templatetext='<h1>You did something.</h1><p>[[p1]]</p><p>[[p2]]</p><p>[[p3]]</p>' }
            ]
        ) ;
        ///////////////////////////////////

        // Build the query.
        local.sql = "SELECT templatesubject, templatetext FROM emailTemplateQuery WHERE templateTypeID = :templateTypeID" ;
        // Which template?
        local.params = { templateTypeID = arguments.templateTypeID };   
        // Query options?
        local.queryoptions = { 
            dbtype="query" 
            // datasource="myDSN" << Use your DSN for final query.
        } ;

        // Create a new query and execute it.
        local.emailQ = QueryExecute(local.sql, local.params, local.queryoptions ) ;

        local.finalEmailString = local.emailQ.templatetext ;

        // Let's inject our placeholder info into our email template
        for ( var p IN arguments.placeholder ) {
            local.finalEmailString = local.finalEmailString.replaceNoCase(
                "[[" & p & "]]" ,
                arguments.placeholder[p] ,
                "All"
            ) ;
        }

        // Create a new mail object.
        local.sendEmail = new mail() ;
        // Save mail body to a variable.
        savecontent variable="emailBody" {
            writeOutput(local.finalEmailString);
        }
          // Set From, To, Type, etc.
          sendEmail.setFrom("fromMe@domain.com");
          sendEmail.setTo(arguments.userEmail);
          sendEmail.setType("html");
          sendEmail.setSubject(local.emailQ.templatesubject);
          // Send the email. So uncomment next line to send.
          //sendEmail.send(body=emailBody); 

        // We don't have to return anything, but for this demo, we want to see what email will be sent.
        writeDump(local.emailQ.templatesubject);
        writeDump(local.finalEmailString);
    }



    // To send an email, just call genEmail() and pass the type and who to.
    genEmail(1,"bill@beexcellent.com",{"p1":"This is my first replacement.","p2":"This is my second replacement."}) ;
    writeoutput("<br><br>");
    genEmail(2,"ted@beexcellent.com",{"p1":"This is my third replacement.","p2":"This is my fourth replacement."}) ;
    writeoutput("<br><br>");
    genEmail(3,"rufus@beexcellent.com",{"p1":"This is my fifth replacement.","p2":"This is my sixth replacement.","p3":"This is my seventh replacement."}) ;
</cfscript>

可以简化一点。我的大部分代码是设置测试数据并使用Query Of Query。您想对数据源使用常规调用。您还可以在cfmail标记内更有效地使用查询结果。我强烈建议您进行大量过滤和验证,然后再允许任何内容从您的系统发送电子邮件。您还可以从电子邮件功能返回状态代码以验证成功(或其他信息)。

您可以将电子邮件进程保存到其自己的CFC中,然后将其缓存以在整个应用程序中使用。

注意::对于大多数CF卡,我也更喜欢脚本而不是标签,但是如果需要,我上面的逻辑也可以转换回标签。

https://cffiddle.org/app/file?filepath=639e2956-a658-4676-a0d2-0efca81d7c23/ce5629c9-87e6-4bff-a9b8-86b608e9fc72/c8d38df7-f14d-481f-867d-2f7fbf3238f2.cfm

结果: 通过上述测试,您将收到包含以下HTML的电子邮件。

  

genEmail(1,"bill@beexcellent.com",{"p1":"This is my first replacement.","p2":"This is my second replacement."})   欢迎   

感谢您的注册!

这是我的第一个   

这是我的第二次替换。

     

genEmail(2,"ted@beexcellent.com",{"p1":"This is my third replacement.","p2":"This is my fourth replacement."})

     

密码重置   

您请求重设密码。

这是我的第三次   

这是我的第四次替换。

     

genEmail(3,"rufus@beexcellent.com",{"p1":"This is my fifth replacement.","p2":"This is my sixth replacement.","p3":"This is my seventh replacement."})

     

另一个电子邮件   

您做了什么。

这是我的第五次   

这是我的第六次替换。

这是我的第六次替换   第七次替换。